fix(SW-664): Fixed issue with params when fetching data from API in relation to Hotellisting

This commit is contained in:
Erik Tiekstra
2024-12-18 14:29:29 +01:00
parent 3939bf7cdc
commit 3af542b075
3 changed files with 24 additions and 28 deletions

View File

@@ -30,20 +30,11 @@ const wrappedFetch = fetchRetry(fetch, {
export async function get(
endpoint: Endpoint,
options: RequestOptionsWithOutBody,
params: Record<string, any> = {}
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])

View File

@@ -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<string, string | string[]> = {
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",
})

View File

@@ -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)
}