import { getCacheClient } from "@scandic-hotels/common/dataCache" import { createLogger } from "@scandic-hotels/common/logger/createLogger" import * as api from "../../../api" import { type Cities, citiesSchema } from "../output" import type { Endpoint } from "../../../api/endpoints" const logger = createLogger("trpc:hotels:getCity") export async function getCity({ cityUrl, serviceToken, }: { cityUrl: string serviceToken: string }): Promise { const cacheClient = await getCacheClient() return await cacheClient.cacheOrGet( cityUrl, async () => { const url = new URL(cityUrl) const cityResponse = await api.get( url.pathname as Endpoint, { headers: { Authorization: `Bearer ${serviceToken}` } }, url.searchParams ) if (!cityResponse.ok) { return null } const cityJson = await cityResponse.json() const city = citiesSchema.safeParse(cityJson) if (!city.success) { logger.error(`Validation of city failed`, { error: city.error, cityUrl, }) return null } return city.data }, "1d" ) }