Merged in fix/refactor-booking-flow-search-params (pull request #2148)

Fix: refactor booking flow search params

* wip: apply codemod and upgrade swc plugin

* wip: design-system to react 19, fix issues from async (search)params

* Prepare new parse function for booking flow search params

* Prepare serialize function for booking flow search params

* Improve handling of comma separated arrays

* Slightly refactor for readability

* Next abstracts URLSearchParams so handle the abstraction instead

* Refactor booking widget to use new search params parsing

* Rename search param functions

* Refactor select-hotel to use new search param parser

* Use new search params parser in select-rate and details

* Fix hotelId type

* Avoid passing down search params into BookingWidget components

* More updates to use new types instead of SearchParams<T>

* Remove types SelectHotelSearchParams and AlternativeSelectHotelSearchParams

* Fix parseBookingWidgetSearchParams return type

* Add error handling to booking search param parsers

* Fix modifyRateIndex handling in details page

* Clean up

* Refactor booking widget search param serializing to util function

* Move start page booking widget search param parsing to page

* Use new search param serializer in HandleErrorCallback

* Delete convertSearchParamsToObj & convertObjToSearchParams


Approved-by: Michael Zetterberg
This commit is contained in:
Anton Gunnarsson
2025-06-02 13:38:01 +00:00
parent 81887c83ff
commit 03468ad824
49 changed files with 1257 additions and 444 deletions
+30 -37
View File
@@ -5,16 +5,9 @@ import { getLocations } from "@/lib/trpc/memoizedRequests"
import { generateChildrenString } from "@/components/HotelReservation/utils"
import { safeTry } from "@/utils/safeTry"
import { type SelectHotelParams } from "@/utils/url"
import type {
AlternativeHotelsSearchParams,
SelectHotelSearchParams,
} from "@/types/components/hotelReservation/selectHotel/selectHotelSearchParams"
import type {
Child,
SelectRateSearchParams,
} from "@/types/components/hotelReservation/selectRate/selectRate"
import type { BookingSearchType } from "@/types/components/hotelReservation/booking"
import type { Child } from "@/types/components/hotelReservation/selectRate/selectRate"
import {
type HotelLocation,
isHotelLocation,
@@ -24,41 +17,44 @@ import {
export type ChildrenInRoom = (Child[] | null)[] | null
export type ChildrenInRoomString = (string | null)[] | null
interface HotelSearchDetails<T> {
interface HotelSearchDetails {
adultsInRoom: number[]
bookingCode?: string
childrenInRoom: ChildrenInRoom
childrenInRoomString: ChildrenInRoomString
city: Location | null
cityName: string | undefined
hotel: HotelLocation | null
noOfRooms: number
redemption?: boolean
selectHotelParams: SelectHotelParams<T> & { city: string | undefined }
}
export async function getHotelSearchDetails<
T extends
| SelectHotelSearchParams
| SelectRateSearchParams
| AlternativeHotelsSearchParams,
>(
selectHotelParams: SelectHotelParams<T>,
export async function getHotelSearchDetails(
params: {
hotelId?: string
city?: string
rooms?: {
adults: number
childrenInRoom?: Child[]
}[]
bookingCode?: string
searchType?: BookingSearchType
},
isAlternativeHotels?: boolean
): Promise<HotelSearchDetails<T> | null> {
): Promise<HotelSearchDetails | null> {
const [locations, error] = await safeTry(getLocations())
if (!locations || error) {
return null
}
const hotel =
("hotelId" in selectHotelParams &&
(locations.find(
const hotel = params.hotelId
? ((locations.find(
(location) =>
isHotelLocation(location) &&
"operaId" in location &&
location.operaId === selectHotelParams.hotelId
) as HotelLocation | undefined)) ||
null
location.operaId === params.hotelId
) as HotelLocation | undefined) ?? null)
: null
if (isAlternativeHotels && !hotel) {
return notFound()
@@ -66,16 +62,13 @@ export async function getHotelSearchDetails<
const cityName = isAlternativeHotels
? hotel?.relationships.city.name
: "city" in selectHotelParams
? (selectHotelParams.city as string | undefined)
: undefined
: params.city
const city =
(typeof cityName === "string" &&
locations.find(
const city = cityName
? (locations.find(
(location) => location.name.toLowerCase() === cityName.toLowerCase()
)) ||
null
) ?? null)
: null
if (!city && !hotel) return notFound()
if (isAlternativeHotels && (!city || !hotel)) return notFound()
@@ -84,7 +77,7 @@ export async function getHotelSearchDetails<
let childrenInRoom: ChildrenInRoom = null
let childrenInRoomString: ChildrenInRoomString = null
const { rooms } = selectHotelParams
const { rooms } = params
if (rooms?.length) {
adultsInRoom = rooms.map((room) => room.adults ?? 0)
@@ -97,13 +90,13 @@ export async function getHotelSearchDetails<
return {
adultsInRoom,
bookingCode: selectHotelParams.bookingCode ?? undefined,
bookingCode: params.bookingCode ?? undefined,
childrenInRoom,
childrenInRoomString,
city,
cityName,
hotel,
noOfRooms: rooms?.length ?? 0,
redemption: selectHotelParams.searchType === REDEMPTION,
selectHotelParams: { city: cityName, ...selectHotelParams },
redemption: params.searchType === REDEMPTION,
}
}
+509
View File
@@ -0,0 +1,509 @@
import { describe, expect, test } from "@jest/globals"
import { z } from "zod"
import { parseSearchParams, serializeSearchParams } from "./searchParams"
describe("Parse search params", () => {
test("with flat values", () => {
const searchParams = getSearchParams("city=stockholm&hotel=123")
const result = parseSearchParams(searchParams)
expect(result).toEqual({
city: "stockholm",
hotel: "123",
})
})
test("with comma separated array", () => {
const searchParams = getSearchParams(
"filter=1831,1383,971,1607&packages=ABC,XYZ"
)
const result = parseSearchParams(searchParams, {
typeHints: {
packages: "COMMA_SEPARATED_ARRAY",
filter: "COMMA_SEPARATED_ARRAY",
},
})
expect(result).toEqual({
filter: ["1831", "1383", "971", "1607"],
packages: ["ABC", "XYZ"],
})
})
test("with comma separated array with single value", () => {
const searchParams = getSearchParams(
"details.packages=ABC&filter=1831&rooms[0].packages=XYZ"
)
const result = parseSearchParams(searchParams, {
typeHints: {
filter: "COMMA_SEPARATED_ARRAY",
packages: "COMMA_SEPARATED_ARRAY",
},
})
expect(result).toEqual({
filter: ["1831"],
details: {
packages: ["ABC"],
},
rooms: [
{
packages: ["XYZ"],
},
],
})
})
test("with nested object", () => {
const searchParams = getSearchParams(
"room.details.adults=1&room.ratecode=ABC&room.details.children=2&room.filters=1,2,3,4"
)
const result = parseSearchParams(searchParams, {
typeHints: {
filters: "COMMA_SEPARATED_ARRAY",
},
})
expect(result).toEqual({
room: {
ratecode: "ABC",
filters: ["1", "2", "3", "4"],
details: {
adults: "1",
children: "2",
},
},
})
})
test("with array of objects", () => {
const searchParams = getSearchParams(
"room[0].adults=1&room[0].ratecode=ABC&room[1].adults=2&room[1].ratecode=DEF"
)
const result = parseSearchParams(searchParams)
expect(result).toEqual({
room: [
{
adults: "1",
ratecode: "ABC",
},
{
adults: "2",
ratecode: "DEF",
},
],
})
})
test("with array defined out of order", () => {
const searchParams = getSearchParams("room[1].adults=1&room[0].adults=2")
const result = parseSearchParams(searchParams)
expect(result).toEqual({
room: [
{
adults: "2",
},
{
adults: "1",
},
],
})
})
test("with nested array of objects", () => {
const searchParams = getSearchParams(
"room[0].adults=1&room[0].child[0].age=2&room[1].adults=2&room[1].child[0].age=3"
)
const result = parseSearchParams(searchParams)
expect(result).toEqual({
room: [
{
adults: "1",
child: [
{
age: "2",
},
],
},
{
adults: "2",
child: [
{
age: "3",
},
],
},
],
})
})
test("can handle array syntax with primitive values", () => {
const searchParams = getSearchParams("room[1]=1&room[0]=2")
const result = parseSearchParams(searchParams)
expect(result).toEqual({
room: ["2", "1"],
})
})
test("can rename search param keys", () => {
const searchParams = getSearchParams(
"city=stockholm&hotel=123&room[0].adults=1&room[0].child[0].age=2&room[1].adults=2&room[1].child[0].age=3"
)
const result = parseSearchParams(searchParams, {
keyRenameMap: {
hotel: "hotelId",
room: "rooms",
age: "childAge",
},
})
expect(result).toEqual({
city: "stockholm",
hotelId: "123",
rooms: [
{
adults: "1",
child: [
{
childAge: "2",
},
],
},
{
adults: "2",
child: [
{
childAge: "3",
},
],
},
],
})
})
test("with schema validation", () => {
const searchParams = getSearchParams(
"room[0].adults=1&room[0].child[0].age=2&room[1].adults=2&room[1].child[0].age=3"
)
const result = parseSearchParams(searchParams, {
schema: z.object({
room: z.array(
z.object({
adults: z.string(),
child: z.array(
z.object({
age: z.string(),
})
),
})
),
}),
})
expect(result).toEqual({
room: [
{
adults: "1",
child: [
{
age: "2",
},
],
},
{
adults: "2",
child: [
{
age: "3",
},
],
},
],
})
})
test("throws when schema validation fails", () => {
const searchParams = getSearchParams("city=stockholm")
expect(() =>
parseSearchParams(searchParams, {
schema: z.object({
city: z.string(),
hotel: z.string(),
}),
})
).toThrow()
})
test("with value coercion", () => {
const searchParams = getSearchParams(
"room[0].adults=1&room[0].enabled=true"
)
const result = parseSearchParams(searchParams, {
schema: z.object({
room: z.array(
z.object({
adults: z.coerce.number(),
enabled: z.coerce.boolean(),
})
),
}),
})
expect(result).toEqual({
room: [
{
adults: 1,
enabled: true,
},
],
})
})
})
describe("Serialize search params", () => {
test("with flat values", () => {
const obj = {
city: "stockholm",
hotel: "123",
}
const result = serializeSearchParams(obj)
expect(decodeURIComponent(result.toString())).toEqual(
"city=stockholm&hotel=123"
)
})
test("with comma separated array", () => {
const obj = {
filter: ["1831", "1383", "971", "1607"],
}
const result = serializeSearchParams(obj, {
typeHints: {
filter: "COMMA_SEPARATED_ARRAY",
},
})
expect(decodeURIComponent(result.toString())).toEqual(
"filter=1831,1383,971,1607"
)
})
test("with comma separated array with single value", () => {
const obj = {
details: {
packages: ["ABC"],
},
filter: ["1831"],
rooms: [
{
packages: ["XYZ"],
},
],
}
const result = serializeSearchParams(obj, {
typeHints: {
filter: "COMMA_SEPARATED_ARRAY",
packages: "COMMA_SEPARATED_ARRAY",
},
})
expect(decodeURIComponent(result.toString())).toEqual(
"details.packages=ABC&filter=1831&rooms[0].packages=XYZ"
)
})
test("with nested object", () => {
const obj = {
room: {
ratecode: "ABC",
filters: ["1", "2", "3", "4"],
details: {
adults: "1",
children: "2",
},
},
}
const result = serializeSearchParams(obj, {
typeHints: {
filters: "COMMA_SEPARATED_ARRAY",
},
})
expect(decodeURIComponent(result.toString())).toEqual(
"room.ratecode=ABC&room.filters=1,2,3,4&room.details.adults=1&room.details.children=2"
)
})
test("with array of objects", () => {
const obj = {
room: [
{
adults: "1",
ratecode: "ABC",
},
{
adults: "2",
ratecode: "DEF",
},
],
}
const result = serializeSearchParams(obj)
expect(decodeURIComponent(result.toString())).toEqual(
"room[0].adults=1&room[0].ratecode=ABC&room[1].adults=2&room[1].ratecode=DEF"
)
})
test("with nested array of objects", () => {
const obj = {
room: [
{
adults: "1",
child: [
{
age: "2",
},
],
},
{
adults: "2",
child: [
{
age: "3",
},
],
},
],
}
const result = serializeSearchParams(obj)
expect(decodeURIComponent(result.toString())).toEqual(
"room[0].adults=1&room[0].child[0].age=2&room[1].adults=2&room[1].child[0].age=3"
)
})
test("can handle array syntax with primitive values", () => {
const obj = {
room: ["2", "1"],
}
const result = serializeSearchParams(obj)
expect(decodeURIComponent(result.toString())).toEqual("room[0]=2&room[1]=1")
})
test("can rename search param keys", () => {
const obj = {
city: "stockholm",
hotelId: "123",
rooms: [
{
adults: "1",
child: [
{
childAge: "2",
},
],
},
{
adults: "2",
child: [
{
childAge: "3",
},
],
},
],
}
const result = serializeSearchParams(obj, {
keyRenameMap: {
hotelId: "hotel",
rooms: "room",
childAge: "age",
},
})
expect(decodeURIComponent(result.toString())).toEqual(
"city=stockholm&hotel=123&room[0].adults=1&room[0].child[0].age=2&room[1].adults=2&room[1].child[0].age=3"
)
})
test("with initial search params", () => {
const initialSearchParams = new URLSearchParams("city=stockholm&hotel=123")
const obj = {
hotel: "456",
filter: ["1831", "1383"],
packages: ["ABC"],
}
const result = serializeSearchParams(obj, {
initialSearchParams,
typeHints: {
packages: "COMMA_SEPARATED_ARRAY",
},
})
expect(decodeURIComponent(result.toString())).toEqual(
"city=stockholm&hotel=456&filter[0]=1831&filter[1]=1383&packages=ABC"
)
})
})
describe("Parse serialized search params", () => {
test("should return the same object", () => {
const obj = {
city: "stockholm",
hotelId: "123",
filter: ["1831", "1383", "971", "1607"],
details: {
packages: ["ABC"],
},
rooms: [
{
packages: ["XYZ"],
},
],
}
const searchParams = serializeSearchParams(obj, {
keyRenameMap: {
hotelId: "hotel",
rooms: "room",
},
typeHints: {
filter: "COMMA_SEPARATED_ARRAY",
packages: "COMMA_SEPARATED_ARRAY",
},
})
const searchParamsObj = searchParamsToObject(searchParams)
const result = parseSearchParams(searchParamsObj, {
keyRenameMap: {
hotel: "hotelId",
room: "rooms",
},
typeHints: {
filter: "COMMA_SEPARATED_ARRAY",
packages: "COMMA_SEPARATED_ARRAY",
},
})
expect(result).toEqual(obj)
})
})
// Simulates what Next does behind the scenes for search params
const getSearchParams = (input: string) => {
const searchParams = new URLSearchParams(input)
return searchParamsToObject(searchParams)
}
const searchParamsToObject = (searchParams: URLSearchParams) => {
const obj: Record<string, any> = {}
for (const [key, value] of searchParams.entries()) {
obj[key] = value
}
return obj
}
+209
View File
@@ -0,0 +1,209 @@
import type { z } from "zod"
import type { NextSearchParams } from "@/types/params"
type ParseOptions<T extends z.ZodRawShape> = {
keyRenameMap?: Record<string, string>
typeHints?: Record<string, "COMMA_SEPARATED_ARRAY">
schema?: z.ZodObject<T>
}
type ParseOptionsWithSchema<T extends z.ZodRawShape> = ParseOptions<T> & {
schema: z.ZodObject<T>
}
// This ensures that the return type is correct when a schema is provided
export function parseSearchParams<T extends z.ZodRawShape>(
searchParams: NextSearchParams,
options: ParseOptionsWithSchema<T>
): z.infer<typeof options.schema>
export function parseSearchParams<T extends z.ZodRawShape>(
searchParams: NextSearchParams,
options?: ParseOptions<T>
): Record<string, any>
/**
* Parses URL search parameters into a structured object.
* This function can handle nested objects, arrays, and type validation/transformation using Zod schema.
*
* @param searchParams - The object to parse
* @param options.keyRenameMap - Optional mapping of keys to rename, ie { "oldKey": "newKey" }
* @param options.typeHints - Optional type hints to force certain keys to be treated as arrays
* @param options.schema - Pass a Zod schema to validate and transform the parsed search parameters and get a typed return value
*
* Supported formats:
* - Objects: `user.name=John&user.age=30`
* - Arrays: `tags[0]=javascript&tags[1]=typescript`
* - Arrays of objects: `tags[0].name=javascript&tags[0].age=30`
* - Nested arrays: `tags[0].languages[0]=javascript&tags[0].languages[1]=typescript`
* - Comma-separated arrays: `tags=javascript,typescript`
*
* For comma-separated arrays you must use the `typeHints`
* option to inform the parser that the key should be treated as an array.
*/
export function parseSearchParams<T extends z.ZodRawShape>(
searchParams: NextSearchParams,
options?: ParseOptions<T>
) {
const entries = Object.entries(searchParams)
const buildObject = getBuilder(options || {})
const resultObject: Record<string, any> = {}
for (const [key, value] of entries) {
const paths = key.split(".")
if (Array.isArray(value)) {
throw new Error(
`Arrays from duplicate keys (?a=1&a=2) are not yet supported.`
)
}
if (!value) {
continue
}
buildObject(resultObject, paths, value)
}
if (options?.schema) {
return options.schema.parse(resultObject)
}
return resultObject
}
// Use a higher-order function to avoid passing the options
// object every time we recursively call the builder
function getBuilder<T extends z.ZodRawShape>(options: ParseOptions<T>) {
const keyRenameMap = options.keyRenameMap || {}
const typeHints = options.typeHints || {}
return function buildNestedObject(
obj: Record<string, any>,
paths: string[],
value: string
) {
if (paths.length === 0) return
const path = paths[0]
const remainingPaths = paths.slice(1)
// Extract the key name and optional array index
const match = path.match(/^([^\[]+)(?:\[(\d+)\])?$/)
if (!match) return
const key = keyRenameMap[match[1]] || match[1]
const index = match[2] ? parseInt(match[2]) : null
const forceCommaSeparatedArray = typeHints[key] === "COMMA_SEPARATED_ARRAY"
const hasIndex = index !== null
// If we've reached the last path, set the value
if (remainingPaths.length === 0) {
// This is either an array or a value that is
// forced to be an array by the typeHints
if (hasIndex || forceCommaSeparatedArray) {
if (isNotArray(obj[key])) obj[key] = []
if (!hasIndex || forceCommaSeparatedArray) {
obj[key] = value.split(",")
return
}
obj[key][index] = value
return
}
obj[key] = value
return
}
if (hasIndex) {
// If the key is an array, ensure array and element at index exists
if (isNotArray(obj[key])) obj[key] = []
if (!obj[key][index]) obj[key][index] = {}
buildNestedObject(obj[key][index], remainingPaths, value)
return
}
// Otherwise, it should be an object
if (!obj[key]) obj[key] = {}
buildNestedObject(obj[key], remainingPaths, value)
}
}
function isNotArray(value: any) {
return !value || typeof value !== "object" || !Array.isArray(value)
}
type SerializeOptions = {
keyRenameMap?: Record<string, string>
typeHints?: Record<string, "COMMA_SEPARATED_ARRAY">
initialSearchParams?: URLSearchParams
}
/**
* Serializes an object into URL search parameters.
*
* @param obj - The object to serialize
* @param options.keyRenameMap - Optional mapping of keys to rename, ie { "oldKey": "newKey" }
* @param options.typeHints - Optional type hints to force certain keys to be treated as comma separated arrays
* @returns URLSearchParams - The serialized URL search parameters
*/
export function serializeSearchParams(
obj: Record<string, any>,
options?: SerializeOptions
): URLSearchParams {
const params = new URLSearchParams(options?.initialSearchParams)
const keyRenameMap = options?.keyRenameMap || {}
const typeHints = options?.typeHints || {}
function buildParams(obj: unknown, prefix: string) {
if (obj === null || obj === undefined) return
if (!isRecord(obj)) {
params.set(prefix, String(obj))
return
}
for (const key in obj) {
const value = obj[key]
const renamedKey = keyRenameMap[key] || key
if (Array.isArray(value)) {
if (typeHints[key] === "COMMA_SEPARATED_ARRAY") {
const paramKey = prefix ? `${prefix}.${renamedKey}` : renamedKey
params.set(paramKey, value.join(","))
continue
}
value.forEach((item, index) => {
const indexedKey = `${renamedKey}[${index}]`
const paramKey = prefix ? `${prefix}.${indexedKey}` : indexedKey
buildParams(item, paramKey)
})
continue
}
const paramKey = prefix ? `${prefix}.${renamedKey}` : renamedKey
if (typeof value === "object" && value !== null) {
buildParams(value, paramKey)
continue
}
params.set(paramKey, String(value))
}
}
buildParams(obj, "")
return params
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null
}
+203 -131
View File
@@ -1,10 +1,20 @@
import { z } from "zod"
import { bookingSearchTypes } from "@/constants/booking"
import { Lang } from "@/constants/languages"
import type { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import { parseSearchParams, serializeSearchParams } from "./searchParams"
import type { BookingWidgetSearchData } from "@/types/components/bookingWidget"
import type { DetailsBooking } from "@/types/components/hotelReservation/enterDetails/details"
import type { SelectHotelBooking } from "@/types/components/hotelReservation/selectHotel/selectHotel"
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import type {
Child,
Room,
SelectRateBooking,
} from "@/types/components/hotelReservation/selectRate/selectRate"
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
import type { NextSearchParams } from "@/types/params"
export function removeMultipleSlashes(pathname: string) {
return pathname.replaceAll(/\/\/+/g, "/")
@@ -20,151 +30,213 @@ export function removeTrailingSlash(pathname: string) {
type PartialRoom = { rooms?: Partial<Room>[] }
const keyedSearchParams = new Map([
["room", "rooms"],
["ratecode", "rateCode"],
["counterratecode", "counterRateCode"],
["roomtype", "roomTypeCode"],
["fromdate", "fromDate"],
["todate", "toDate"],
["hotel", "hotelId"],
["child", "childrenInRoom"],
["searchtype", "searchType"],
])
export type SelectHotelParams<T> = Omit<T, "hotel"> & {
hotelId: string
} & PartialRoom
export function getKeyFromSearchParam(key: string): string {
return keyedSearchParams.get(key) || key
}
export function getSearchParamFromKey(key: string): string {
for (const [mapKey, mapValue] of keyedSearchParams.entries()) {
if (mapValue === key) {
return mapKey
}
}
return key
}
export function searchParamsToRecord(searchParams: URLSearchParams) {
return Object.fromEntries(searchParams.entries())
}
export function convertSearchParamsToObj<T extends PartialRoom>(
searchParams: Record<string, string>
): SelectHotelParams<T> {
const searchParamsObject = Object.entries(searchParams).reduce<
SelectHotelParams<T>
>((acc, [key, value]) => {
// The params are sometimes indexed with a number (for ex: `room[0].adults`),
// so we need to split them by . or []
const keys = key.replace(/\]/g, "").split(/\[|\./)
const firstKey = getKeyFromSearchParam(keys[0])
const keyRenameMap = {
room: "rooms",
ratecode: "rateCode",
counterratecode: "counterRateCode",
roomtype: "roomTypeCode",
fromdate: "fromDate",
todate: "toDate",
hotel: "hotelId",
child: "childrenInRoom",
searchtype: "searchType",
}
const adultsSchema = z.coerce.number().min(1).max(6).catch(0)
const childAgeSchema = z.coerce.number().catch(-1)
const childBedSchema = z.coerce.number().catch(-1)
const searchTypeSchema = z.enum(bookingSearchTypes).optional().catch(undefined)
// Room is a special case since it is an array, so we need to handle it separately
if (firstKey === "rooms") {
// Rooms are always indexed with a number, so we need to extract the index
const index = Number(keys[1])
const roomObject =
acc.rooms && Array.isArray(acc.rooms) ? acc.rooms : (acc.rooms = [])
export function parseBookingWidgetSearchParams(
searchParams: NextSearchParams
): BookingWidgetSearchData {
try {
const result = parseSearchParams(searchParams, {
keyRenameMap,
schema: z.object({
city: z.string().optional(),
hotelId: z.string().optional(),
fromDate: z.string().optional(),
toDate: z.string().optional(),
bookingCode: z.string().optional(),
searchType: searchTypeSchema,
rooms: z
.array(
z.object({
adults: adultsSchema,
childrenInRoom: z
.array(
z.object({
bed: childBedSchema,
age: childAgeSchema,
})
)
.optional()
.default([]),
})
)
.optional(),
}),
})
const roomObjectKey = getKeyFromSearchParam(keys[2]) as keyof Room
if (!roomObject[index]) {
roomObject[index] = {}
}
// Adults should be converted to a number
if (roomObjectKey === "adults") {
roomObject[index].adults = Number(value)
// Child is an array, so we need to handle it separately
} else if (roomObjectKey === "childrenInRoom") {
const childIndex = Number(keys[3])
const childKey = keys[4] as keyof Child
if (
!("childrenInRoom" in roomObject[index]) ||
!Array.isArray(roomObject[index].childrenInRoom)
) {
roomObject[index].childrenInRoom = []
}
roomObject[index].childrenInRoom![childIndex] = {
...roomObject[index].childrenInRoom![childIndex],
[childKey]: Number(value),
}
} else if (roomObjectKey === "packages") {
roomObject[index].packages = value.split(",") as RoomPackageCodeEnum[]
} else {
roomObject[index][roomObjectKey] = value
}
} else {
return { ...acc, [firstKey]: value }
}
return acc
}, {} as SelectHotelParams<T>)
return searchParamsObject
return result
} catch (error) {
console.log("[URL] Error parsing search params for booking widget:", error)
return {}
}
}
export function convertObjToSearchParams<T>(
bookingData: T & PartialRoom,
intitalSearchParams = {} as URLSearchParams
) {
const bookingSearchParams = new URLSearchParams(intitalSearchParams)
Object.entries(bookingData).forEach(([key, value]) => {
if (key === "rooms") {
value.forEach((item, index) => {
if (item?.adults) {
bookingSearchParams.set(
`room[${index}].adults`,
item.adults.toString()
)
}
if (item?.childrenInRoom) {
item.childrenInRoom.forEach((child, childIndex) => {
bookingSearchParams.set(
`room[${index}].child[${childIndex}].age`,
child.age.toString()
)
bookingSearchParams.set(
`room[${index}].child[${childIndex}].bed`,
child.bed.toString()
)
export function parseSelectHotelSearchParams(
searchParams: NextSearchParams
): SelectHotelBooking | null {
try {
const result = parseSearchParams(searchParams, {
keyRenameMap,
schema: z.object({
city: z.string(),
hotelId: z.string().optional(),
fromDate: z.string(),
toDate: z.string(),
bookingCode: z.string().optional(),
searchType: searchTypeSchema,
rooms: z.array(
z.object({
adults: adultsSchema,
childrenInRoom: z
.array(
z.object({
bed: childBedSchema,
age: childAgeSchema,
})
)
.optional(),
})
}
if (item?.roomTypeCode) {
bookingSearchParams.set(`room[${index}].roomtype`, item.roomTypeCode)
}
if (item?.rateCode) {
bookingSearchParams.set(`room[${index}].ratecode`, item.rateCode)
}
),
}),
})
if (item?.counterRateCode) {
bookingSearchParams.set(
`room[${index}].counterratecode`,
item.counterRateCode
)
}
return result
} catch (error) {
console.log("[URL] Error parsing search params for select hotel:", error)
if (item.packages && item.packages.length > 0) {
bookingSearchParams.set(
`room[${index}].packages`,
item.packages.join(",")
)
}
})
} else {
bookingSearchParams.set(getSearchParamFromKey(key), value.toString())
}
return null
}
}
export function parseSelectRateSearchParams(
searchParams: NextSearchParams
): SelectRateBooking | null {
try {
const result = parseSearchParams(searchParams, {
keyRenameMap,
schema: z.object({
city: z.string().optional(),
hotelId: z.string(),
fromDate: z.string(),
toDate: z.string(),
searchType: searchTypeSchema,
bookingCode: z.string().optional(),
rooms: z.array(
z.object({
adults: adultsSchema,
bookingCode: z.string().optional(),
counterRateCode: z.string().optional(),
rateCode: z.string().optional(),
roomTypeCode: z.string().optional(),
packages: z
.array(
z.nativeEnum({
...BreakfastPackageEnum,
...RoomPackageCodeEnum,
})
)
.optional(),
childrenInRoom: z
.array(
z.object({
bed: childBedSchema,
age: childAgeSchema,
})
)
.optional(),
})
),
}),
})
return result
} catch (error) {
console.log("[URL] Error parsing search params for select rate:", error)
return null
}
}
export function parseDetailsSearchParams(
searchParams: NextSearchParams
): DetailsBooking | null {
const packageEnum = {
...BreakfastPackageEnum,
...RoomPackageCodeEnum,
} as const
try {
const result = parseSearchParams(searchParams, {
keyRenameMap,
schema: z.object({
city: z.string().optional(),
hotelId: z.string(),
fromDate: z.string(),
toDate: z.string(),
searchType: searchTypeSchema,
bookingCode: z.string().optional(),
rooms: z.array(
z.object({
adults: adultsSchema,
bookingCode: z.string().optional(),
counterRateCode: z.string().optional(),
rateCode: z.string(),
roomTypeCode: z.string(),
packages: z.array(z.nativeEnum(packageEnum)).optional(),
childrenInRoom: z
.array(
z.object({
bed: childBedSchema,
age: childAgeSchema,
})
)
.optional(),
})
),
}),
})
return result
} catch (error) {
console.log("[URL] Error parsing search params for details:", error)
return null
}
}
const reversedKeyRenameMap = Object.fromEntries(
Object.entries(keyRenameMap).map(([key, value]) => [value, key])
)
export function serializeBookingSearchParams(
obj: { [key: string]: any },
{ initialSearchParams }: { initialSearchParams?: URLSearchParams } = {}
) {
return serializeSearchParams(obj, {
keyRenameMap: reversedKeyRenameMap,
initialSearchParams,
})
return bookingSearchParams
}
/**