Chore/refactor hotel trpc routes * chore(SW-3519): refactor trpc hotel routers * chore(SW-3519): refactor trpc hotel routers * refactor * merge * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/refactor-hotel-trpc-routes Approved-by: Linus Flood
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
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<Cities> {
|
|
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"
|
|
)
|
|
}
|