Files
web/apps/scandic-web/app/api/destinations/[country]/createDataResponse.ts
Joakim Jäderberg 15a2da333d Merged in feature/SW-3572-hotel-data-endpoint (pull request #3051)
SW-3572 API route for listing hotels per city or country

* wip hotel data endpoint

* Correct route params type

* wip

* skip static paths call

* timeout when getting destinations take too long

* call noStore when we get a timeout

* add cache-control headers

* .

* .

* .

* wip

* wip

* wip

* wip

* add route for getting hotels per country

* include city when listing by country

* fix distance SI unit

* fix sorting

* Merge branch 'master' of bitbucket.org:scandic-swap/web into feature/SW-3572-hotel-data-endpoint

* packages/tracking passWithNoTests

* revalidate must be static value

* remove oxc reference

* cleanup

* cleanup hotel api route

* feat(SW-3572): cleanup error handling


Approved-by: Anton Gunnarsson
2025-11-03 12:10:22 +00:00

60 lines
1.6 KiB
TypeScript

export function createDataResponse(
{
countryParam,
cityParam,
hotels,
}: {
countryParam: string
cityParam?: string
hotels: Array<{
name: string
distanceToCentre?: number | undefined
relationships: { city: { name: string } }
images?: { large?: string } | undefined
}>
},
options?: { includeCity: boolean }
) {
const { includeCity = false } = options || {}
return {
country: countryParam.toLowerCase(),
city: cityParam?.toLowerCase(),
hotels: hotels
.map((h) => ({
name: h.name,
city: includeCity ? h.relationships.city.name : undefined,
metersToCityCentre: h.distanceToCentre,
images: {
tiny: createImageUrl({ src: h.images?.large, width: 300 }),
small: createImageUrl({ src: h.images?.large, width: 500 }),
medium: createImageUrl({ src: h.images?.large, width: 1080 }),
large: createImageUrl({ src: h.images?.large, width: 1920 }),
},
}))
.toSorted((a, b) => a.name.localeCompare(b.name))
.toSorted((a, b) => {
return (
(a.metersToCityCentre ?? Infinity) -
(b.metersToCityCentre ?? Infinity)
)
})
.toSorted((a, b) => {
if (!includeCity) return 0
if (!a.city || !b.city) return 0
return a.city.localeCompare(b.city)
}),
}
}
function createImageUrl({
src,
width,
}: {
src: string | null | undefined
width: number
}) {
if (!src) return undefined
return `https://img.scandichotels.com/.netlify/images?url=${encodeURIComponent(src)}&w=${width}&q=90`
}