@@ -24,10 +24,17 @@ interface ItemsResponse {
24
24
/**
25
25
* Takes a date string, validates it, and parses it into a Date object.
26
26
* @params dateString: the date string to parse
27
- * @returns the parsed date string or null if the date string is invalid
27
+ * @returns undefined if the date string is undefined/null
28
+ * @returns null if the date string is defined but invalid
29
+ * @returns a Date object if the date string is valid
28
30
*/
29
- function parseDate ( dateString : string ) : Date | null {
31
+ function parseDateIfDefined (
32
+ dateString : string | null
33
+ ) : Date | null | undefined {
30
34
// see https://stackoverflow.com/questions/1353684/detecting-an-invalid-date-date-instance-in-javascript
35
+ if ( ! dateString ) {
36
+ return undefined ;
37
+ }
31
38
const date = new Date ( dateString ) ;
32
39
if (
33
40
Object . prototype . toString . call ( date ) === "[object Date]" &&
@@ -56,12 +63,12 @@ export async function GET(request: NextRequest) {
56
63
}
57
64
58
65
const params = request . nextUrl . searchParams ;
59
- const expirationDateBefore = params . has ( "expirationDateBefore" )
60
- ? parseDate ( params . get ( "expirationDateBefore" ) as string )
61
- : undefined ;
62
- const expirationDateAfter = params . has ( "expirationDateAfter" )
63
- ? parseDate ( params . get ( "expirationDateAfter" ) as string )
64
- : undefined ;
66
+ const expirationDateBefore = parseDateIfDefined (
67
+ params . get ( "expirationDateBefore" )
68
+ ) ;
69
+ const expirationDateAfter = parseDateIfDefined (
70
+ params . get ( "expirationDateAfter" )
71
+ ) ;
65
72
66
73
if ( expirationDateBefore === null ) {
67
74
return argumentError (
0 commit comments