Merged in fix/SW-449-booking-widget-url-date (pull request #891)
fix(SW-449): Moved normalizeDate outside middleware Approved-by: Chuma Mcphoy (We Ahead)
This commit is contained in:
@@ -3,23 +3,24 @@ import { NextMiddleware, NextResponse } from "next/server"
|
|||||||
import { MiddlewareMatcher } from "@/types/middleware"
|
import { MiddlewareMatcher } from "@/types/middleware"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Middleware function to normalize date formats to support
|
Middleware function to normalize date formats to support
|
||||||
YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D
|
YYYY-MM-D and YYYY-MM-DD since the current web uses YYYY-MM-D
|
||||||
in the URL as parameters (toDate and fromDate)
|
in the URL as parameters (toDate and fromDate)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
function normalizeDate(date: string): string {
|
||||||
|
const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/
|
||||||
|
if (datePattern.test(date)) {
|
||||||
|
const [year, month, day] = date.split("-").map(Number)
|
||||||
|
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`
|
||||||
|
}
|
||||||
|
return date
|
||||||
|
}
|
||||||
|
|
||||||
export const middleware: NextMiddleware = (request) => {
|
export const middleware: NextMiddleware = (request) => {
|
||||||
const url = request.nextUrl.clone()
|
const url = request.nextUrl.clone()
|
||||||
const { searchParams } = url
|
const { searchParams } = url
|
||||||
|
|
||||||
function normalizeDate(date: string): string {
|
|
||||||
const datePattern = /^\d{4}-\d{1,2}-\d{1,2}$/
|
|
||||||
if (datePattern.test(date)) {
|
|
||||||
const [year, month, day] = date.split("-").map(Number)
|
|
||||||
return `${year}-${String(month).padStart(2, "0")}-${String(day).padStart(2, "0")}`
|
|
||||||
}
|
|
||||||
return date
|
|
||||||
}
|
|
||||||
|
|
||||||
if (searchParams.has("fromDate")) {
|
if (searchParams.has("fromDate")) {
|
||||||
const fromDate = searchParams.get("fromDate")!
|
const fromDate = searchParams.get("fromDate")!
|
||||||
searchParams.set("fromDate", normalizeDate(fromDate))
|
searchParams.set("fromDate", normalizeDate(fromDate))
|
||||||
|
|||||||
Reference in New Issue
Block a user