Files
web/apps/scandic-web/app/api/web/destinations/[country]/createDataResponse.ts
Joakim Jäderberg b8d887f82d Merged in chore/move-destinations-api-endpoint (pull request #3058)
chore: move /api/destinations/[country]/[?city] -> /api/web/destinations/[country]/[?city]

* chore: move /api/destinations/[country]/[?city] -> /api/web/destinations/[country]/[?city]
2025-11-03 13:33:45 +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`
}