From 3af542b075e077686c022283c8d34c3a67eac839 Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Wed, 18 Dec 2024 14:29:29 +0100 Subject: [PATCH] fix(SW-664): Fixed issue with params when fetching data from API in relation to Hotellisting --- lib/api/index.ts | 13 ++----------- server/routers/hotels/query.ts | 21 +++++++++++++-------- server/routers/hotels/utils.ts | 18 +++++++++--------- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/lib/api/index.ts b/lib/api/index.ts index 62ae6a8a2..5842b5597 100644 --- a/lib/api/index.ts +++ b/lib/api/index.ts @@ -30,20 +30,11 @@ const wrappedFetch = fetchRetry(fetch, { export async function get( endpoint: Endpoint, options: RequestOptionsWithOutBody, - params: Record = {} + params = {} ) { const url = new URL(env.API_BASEURL) url.pathname = endpoint - const searchParams = new URLSearchParams() - Object.entries(params).forEach(([key, value]) => { - if (Array.isArray(value)) { - value.forEach((val) => searchParams.append(key, val)) - } else { - searchParams.set(key, value) - } - }) - - url.search = searchParams.toString() + url.search = new URLSearchParams(params).toString() return wrappedFetch( url, merge.all([defaultOptions, { method: "GET" }, options]) diff --git a/server/routers/hotels/query.ts b/server/routers/hotels/query.ts index 4cebfea69..86d615ebd 100644 --- a/server/routers/hotels/query.ts +++ b/server/routers/hotels/query.ts @@ -1,3 +1,4 @@ +import { ApiLang } from "@/constants/languages" import * as api from "@/lib/api" import { dt } from "@/lib/dt" import { badRequestError } from "@/server/errors/trpc" @@ -79,12 +80,13 @@ export const getHotelData = cache( async (input: HotelDataInput, serviceToken: string) => { const { hotelId, language, isCardOnlyPayment } = input - const params: Record = { + const includes = ["RoomCategories", "Restaurants"] // "RoomCategories","NearbyHotels","Restaurants","City", + const params = new URLSearchParams({ hotelId, language, - } + }) - params.include = ["RoomCategories", "Restaurants"] // "RoomCategories","NearbyHotels","Restaurants","City", + includes.forEach((include) => params.append("include", include)) getHotelCounter.add(1, { hotelId, @@ -680,8 +682,11 @@ export const hotelQueryRouter = router({ const { locationFilter, hotelsToInclude } = input const language = ctx.lang + const apiLang = toApiLang(language) const options: RequestOptionsWithOutBody = { - cache: "force-cache", + // needs to clear default option as only + // cache or next.revalidate is permitted + cache: undefined, headers: { Authorization: `Bearer ${ctx.serviceToken}`, }, @@ -710,10 +715,10 @@ export const hotelQueryRouter = router({ hotelsToFetch = hotelsToInclude } else if (locationFilter?.city) { const locationsParams = new URLSearchParams({ - language: toApiLang(ctx.lang), + language: apiLang, }) const locations = await getLocations( - ctx.lang, + language, options, locationsParams, null @@ -744,7 +749,7 @@ export const hotelQueryRouter = router({ return [] } const hotelIdsParams = new URLSearchParams({ - language: ctx.lang, + language: apiLang, city: cityId, onlyBasicInfo: "true", }) @@ -779,7 +784,7 @@ export const hotelQueryRouter = router({ hotelsToFetch = filteredHotelIds } else if (locationFilter?.country) { const hotelIdsParams = new URLSearchParams({ - language: ctx.lang, + language: ApiLang.En, country: locationFilter.country, onlyBasicInfo: "true", }) diff --git a/server/routers/hotels/utils.ts b/server/routers/hotels/utils.ts index f2b9803e5..726947538 100644 --- a/server/routers/hotels/utils.ts +++ b/server/routers/hotels/utils.ts @@ -273,10 +273,10 @@ export async function getHotelIdsByCityId( ) { return unstable_cache( async function (params: URLSearchParams) { - getHotelIdsCounter.add(1, { cityId }) + getHotelIdsCounter.add(1, { params: params.toString() }) console.info( "api.hotel.hotel-ids start", - JSON.stringify({ query: { cityId } }) + JSON.stringify({ params: params.toString() }) ) const apiResponse = await api.get( api.endpoints.v1.Hotel.hotels, @@ -287,14 +287,14 @@ export async function getHotelIdsByCityId( if (!apiResponse.ok) { const responseMessage = await apiResponse.text() getHotelIdsFailCounter.add(1, { - cityId, + params: params.toString(), error_type: "http_error", error: responseMessage, }) console.error( "api.hotel.hotel-ids fetch error", JSON.stringify({ - query: { cityId }, + params: params.toString(), error: { status: apiResponse.status, statusText: apiResponse.statusText, @@ -310,14 +310,14 @@ export async function getHotelIdsByCityId( const validatedHotelIds = getHotelIdsByCityIdSchema.safeParse(apiJson) if (!validatedHotelIds.success) { getHotelIdsFailCounter.add(1, { - cityId, + params: params.toString(), error_type: "validation_error", error: JSON.stringify(validatedHotelIds.error), }) console.error( "api.hotel.hotel-ids validation error", JSON.stringify({ - query: { cityId }, + params: params.toString(), error: validatedHotelIds.error, }) ) @@ -327,12 +327,12 @@ export async function getHotelIdsByCityId( getHotelIdsSuccessCounter.add(1, { cityId }) console.info( "api.hotel.hotel-ids success", - JSON.stringify({ query: { cityId } }) + JSON.stringify({ params: params.toString() }) ) return validatedHotelIds.data }, - [`hotels`, params.toString()], + [`hotelsByCityId`, params.toString()], { revalidate: TWENTYFOUR_HOURS } )(params) } @@ -403,7 +403,7 @@ export async function getHotelIdsByCountry( return validatedHotelIds.data }, - [`hotels`, params.toString()], + [`hotelsByCountry`, params.toString()], { revalidate: TWENTYFOUR_HOURS } )(params) }