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( export async function get(
endpoint: Endpoint, endpoint: Endpoint,
options: RequestOptionsWithOutBody, options: RequestOptionsWithOutBody,
params: Record<string, any> = {} params = {}
) { ) {
const url = new URL(env.API_BASEURL) const url = new URL(env.API_BASEURL)
url.pathname = endpoint url.pathname = endpoint
const searchParams = new URLSearchParams() url.search = new URLSearchParams(params).toString()
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()
return wrappedFetch( return wrappedFetch(
url, url,
merge.all([defaultOptions, { method: "GET" }, options]) merge.all([defaultOptions, { method: "GET" }, options])

View File

@@ -1,3 +1,4 @@
import { ApiLang } from "@/constants/languages"
import * as api from "@/lib/api" import * as api from "@/lib/api"
import { dt } from "@/lib/dt" import { dt } from "@/lib/dt"
import { badRequestError } from "@/server/errors/trpc" import { badRequestError } from "@/server/errors/trpc"
@@ -79,12 +80,13 @@ export const getHotelData = cache(
async (input: HotelDataInput, serviceToken: string) => { async (input: HotelDataInput, serviceToken: string) => {
const { hotelId, language, isCardOnlyPayment } = input const { hotelId, language, isCardOnlyPayment } = input
const params: Record<string, string | string[]> = { const includes = ["RoomCategories", "Restaurants"] // "RoomCategories","NearbyHotels","Restaurants","City",
const params = new URLSearchParams({
hotelId, hotelId,
language, language,
} })
params.include = ["RoomCategories", "Restaurants"] // "RoomCategories","NearbyHotels","Restaurants","City", includes.forEach((include) => params.append("include", include))
getHotelCounter.add(1, { getHotelCounter.add(1, {
hotelId, hotelId,
@@ -680,8 +682,11 @@ export const hotelQueryRouter = router({
const { locationFilter, hotelsToInclude } = input const { locationFilter, hotelsToInclude } = input
const language = ctx.lang const language = ctx.lang
const apiLang = toApiLang(language)
const options: RequestOptionsWithOutBody = { const options: RequestOptionsWithOutBody = {
cache: "force-cache", // needs to clear default option as only
// cache or next.revalidate is permitted
cache: undefined,
headers: { headers: {
Authorization: `Bearer ${ctx.serviceToken}`, Authorization: `Bearer ${ctx.serviceToken}`,
}, },
@@ -710,10 +715,10 @@ export const hotelQueryRouter = router({
hotelsToFetch = hotelsToInclude hotelsToFetch = hotelsToInclude
} else if (locationFilter?.city) { } else if (locationFilter?.city) {
const locationsParams = new URLSearchParams({ const locationsParams = new URLSearchParams({
language: toApiLang(ctx.lang), language: apiLang,
}) })
const locations = await getLocations( const locations = await getLocations(
ctx.lang, language,
options, options,
locationsParams, locationsParams,
null null
@@ -744,7 +749,7 @@ export const hotelQueryRouter = router({
return [] return []
} }
const hotelIdsParams = new URLSearchParams({ const hotelIdsParams = new URLSearchParams({
language: ctx.lang, language: apiLang,
city: cityId, city: cityId,
onlyBasicInfo: "true", onlyBasicInfo: "true",
}) })
@@ -779,7 +784,7 @@ export const hotelQueryRouter = router({
hotelsToFetch = filteredHotelIds hotelsToFetch = filteredHotelIds
} else if (locationFilter?.country) { } else if (locationFilter?.country) {
const hotelIdsParams = new URLSearchParams({ const hotelIdsParams = new URLSearchParams({
language: ctx.lang, language: ApiLang.En,
country: locationFilter.country, country: locationFilter.country,
onlyBasicInfo: "true", onlyBasicInfo: "true",
}) })

View File

@@ -273,10 +273,10 @@ export async function getHotelIdsByCityId(
) { ) {
return unstable_cache( return unstable_cache(
async function (params: URLSearchParams) { async function (params: URLSearchParams) {
getHotelIdsCounter.add(1, { cityId }) getHotelIdsCounter.add(1, { params: params.toString() })
console.info( console.info(
"api.hotel.hotel-ids start", "api.hotel.hotel-ids start",
JSON.stringify({ query: { cityId } }) JSON.stringify({ params: params.toString() })
) )
const apiResponse = await api.get( const apiResponse = await api.get(
api.endpoints.v1.Hotel.hotels, api.endpoints.v1.Hotel.hotels,
@@ -287,14 +287,14 @@ export async function getHotelIdsByCityId(
if (!apiResponse.ok) { if (!apiResponse.ok) {
const responseMessage = await apiResponse.text() const responseMessage = await apiResponse.text()
getHotelIdsFailCounter.add(1, { getHotelIdsFailCounter.add(1, {
cityId, params: params.toString(),
error_type: "http_error", error_type: "http_error",
error: responseMessage, error: responseMessage,
}) })
console.error( console.error(
"api.hotel.hotel-ids fetch error", "api.hotel.hotel-ids fetch error",
JSON.stringify({ JSON.stringify({
query: { cityId }, params: params.toString(),
error: { error: {
status: apiResponse.status, status: apiResponse.status,
statusText: apiResponse.statusText, statusText: apiResponse.statusText,
@@ -310,14 +310,14 @@ export async function getHotelIdsByCityId(
const validatedHotelIds = getHotelIdsByCityIdSchema.safeParse(apiJson) const validatedHotelIds = getHotelIdsByCityIdSchema.safeParse(apiJson)
if (!validatedHotelIds.success) { if (!validatedHotelIds.success) {
getHotelIdsFailCounter.add(1, { getHotelIdsFailCounter.add(1, {
cityId, params: params.toString(),
error_type: "validation_error", error_type: "validation_error",
error: JSON.stringify(validatedHotelIds.error), error: JSON.stringify(validatedHotelIds.error),
}) })
console.error( console.error(
"api.hotel.hotel-ids validation error", "api.hotel.hotel-ids validation error",
JSON.stringify({ JSON.stringify({
query: { cityId }, params: params.toString(),
error: validatedHotelIds.error, error: validatedHotelIds.error,
}) })
) )
@@ -327,12 +327,12 @@ export async function getHotelIdsByCityId(
getHotelIdsSuccessCounter.add(1, { cityId }) getHotelIdsSuccessCounter.add(1, { cityId })
console.info( console.info(
"api.hotel.hotel-ids success", "api.hotel.hotel-ids success",
JSON.stringify({ query: { cityId } }) JSON.stringify({ params: params.toString() })
) )
return validatedHotelIds.data return validatedHotelIds.data
}, },
[`hotels`, params.toString()], [`hotelsByCityId`, params.toString()],
{ revalidate: TWENTYFOUR_HOURS } { revalidate: TWENTYFOUR_HOURS }
)(params) )(params)
} }
@@ -403,7 +403,7 @@ export async function getHotelIdsByCountry(
return validatedHotelIds.data return validatedHotelIds.data
}, },
[`hotels`, params.toString()], [`hotelsByCountry`, params.toString()],
{ revalidate: TWENTYFOUR_HOURS } { revalidate: TWENTYFOUR_HOURS }
)(params) )(params)
} }