fix: dateFormat normalization middleware
This commit is contained in:
@@ -43,6 +43,7 @@ export const middleware: NextMiddleware = async (request, event) => {
|
||||
)
|
||||
}
|
||||
|
||||
// Note that the order of middlewares is important since that is the order they are matched by.
|
||||
const middlewares = [
|
||||
currentWebLogin,
|
||||
currentWebLoginEmail,
|
||||
@@ -51,9 +52,9 @@ export const middleware: NextMiddleware = async (request, event) => {
|
||||
handleAuth,
|
||||
myPages,
|
||||
webView,
|
||||
dateFormat,
|
||||
bookingFlow,
|
||||
cmsContent,
|
||||
dateFormat,
|
||||
]
|
||||
|
||||
try {
|
||||
|
||||
@@ -7,6 +7,7 @@ import { MiddlewareMatcher } from "@/types/middleware"
|
||||
YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D
|
||||
in the URL as parameters (toDate and fromDate)
|
||||
*/
|
||||
const legacyDatePattern = /^([12]\d{3}-(0[1-9]|1[0-2])-([1-9]))$/
|
||||
|
||||
function normalizeDate(date: string): string {
|
||||
const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/
|
||||
@@ -18,20 +19,32 @@ function normalizeDate(date: string): string {
|
||||
}
|
||||
|
||||
export const middleware: NextMiddleware = (request) => {
|
||||
const url = request.nextUrl.clone()
|
||||
const url = request.nextUrl
|
||||
const { searchParams } = url
|
||||
|
||||
if (searchParams.has("fromDate")) {
|
||||
if (
|
||||
!!(
|
||||
searchParams.has("fromDate") &&
|
||||
searchParams.get("fromDate")!.match(legacyDatePattern)
|
||||
) ||
|
||||
!!(
|
||||
searchParams.has("toDate") &&
|
||||
searchParams.get("toDate")!.match(legacyDatePattern)
|
||||
)
|
||||
) {
|
||||
const fromDate = searchParams.get("fromDate")!
|
||||
searchParams.set("fromDate", normalizeDate(fromDate))
|
||||
}
|
||||
url.searchParams.set("fromDate", normalizeDate(fromDate))
|
||||
|
||||
if (searchParams.has("toDate")) {
|
||||
const toDate = searchParams.get("toDate")!
|
||||
searchParams.set("toDate", normalizeDate(toDate))
|
||||
url.searchParams.set("toDate", normalizeDate(toDate))
|
||||
return NextResponse.redirect(url)
|
||||
} else {
|
||||
const headers = new Headers(request.headers)
|
||||
headers.set("x-continue", "1")
|
||||
return NextResponse.next({
|
||||
headers,
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.rewrite(url)
|
||||
}
|
||||
|
||||
export const matcher: MiddlewareMatcher = (request) => {
|
||||
|
||||
@@ -136,7 +136,7 @@ const nextConfig = {
|
||||
{
|
||||
key: "fromDate",
|
||||
type: "query",
|
||||
value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))$",
|
||||
value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01]))$",
|
||||
},
|
||||
],
|
||||
permanent: false,
|
||||
@@ -168,7 +168,7 @@ const nextConfig = {
|
||||
{
|
||||
key: "toDate",
|
||||
type: "query",
|
||||
value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))$",
|
||||
value: "^([12]\\d{3}-(0[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01]))$",
|
||||
},
|
||||
],
|
||||
permanent: false,
|
||||
|
||||
Reference in New Issue
Block a user