import {z} from 'zod' export type GetApiV1HotelsHotelIdArgs = {hotelId: number, locale?: string | undefined, view?: ('default' | 'detail') | undefined}; export type GetApiV1HotelsHotelIdOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1HotelsHotelIdResponse = z.object({hotelId: z.number(), name: z.string(), pageUrl: z.string(), star: z.number().optional(), review: z.object({score: z.number(), count: z.number()}).optional(), location: z.object({lat: z.number().optional(), lng: z.number().optional(), address: z.string().optional(), cityName: z.string().optional(), districtName: z.string().optional(), countryName: z.string().optional()}), propertyType: z.string().optional(), brandName: z.string().optional(), chainName: z.string().optional(), description: z.string().optional(), amenities: z.array(z.string()).optional(), images: z.array(z.string()).optional(), distanceToCityCentre: z.number().optional(), distanceToNearestAirport: z.number().optional(), categorizedImages: z.array(z.object({category: z.string(), images: z.array(z.object({url: z.string(), altText: z.string().optional()}))})).optional(), reviewHighlights: z.array(z.object({sentiment: z.string().optional(), text: z.string()})).optional(), highlights: z.array(z.object({text: z.string(), subtext: z.string().optional()})).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get hotel detail * * GET /v1/hotels/{hotelId} * * Also accepts auth: bearerAuth */ export const getApiV1HotelsHotelId = async (args: GetApiV1HotelsHotelIdArgs, options?: GetApiV1HotelsHotelIdOptions) => { const {baseUrl = "https://api.wego.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.locale != null) query.append("locale", String(args.locale)) if (args.view != null) query.append("view", String(args.view)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hotels/${encodeURIComponent(String(args.hotelId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1HotelsHotelIdResponse, await res.json()) };