import {z} from 'zod' export type GetApiV1HotelsHotelIdRatesArgs = {hotelId: number, searchId: string, currency?: string | undefined, locale?: string | undefined}; export type GetApiV1HotelsHotelIdRatesOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1HotelsHotelIdRatesResponse = z.object({hotelId: z.number(), searchId: z.string(), currencyCode: z.string(), searchComplete: z.boolean(), stay: z.object({checkIn: z.string(), checkOut: z.string(), nights: z.number().int().gte(-9007199254740991).lte(9007199254740991), occupancy: z.object({adults: z.number().int().gte(-9007199254740991).lte(9007199254740991), childrenAges: z.array(z.number().int().gte(-9007199254740991).lte(9007199254740991)), rooms: z.number().int().gte(-9007199254740991).lte(9007199254740991)})}).optional(), rates: z.array(z.object({id: z.string(), roomName: z.string(), board: z.string().optional(), refundable: z.boolean(), cancellationPolicy: z.string().optional(), price: z.object({scope: z.literal("booking"), amountPerNight: z.number(), taxAmountPerNight: z.number().optional(), taxInclusive: z.boolean().optional(), localTaxPerNight: z.number().gte(0).optional(), totalLocalTax: z.number().gte(0).optional(), total: z.number().optional(), totalUsd: z.number(), currency: z.string(), deal: z.object({label: z.string().optional(), percentOff: z.number().int().gte(1).lte(99), wasPerNight: z.number().optional(), wasTotal: z.number().optional(), promoCode: z.string().optional()}).optional()}), roomsLeft: z.number().optional(), images: z.array(z.string()).optional()})), metadata: z.object({currencyCode: z.string(), currencyCodeSource: z.enum(["explicit", "default"]), locale: z.string(), localeSource: z.enum(["explicit", "default"])})}); 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 }; /** * List a hotel's rooms & rates * * GET /v1/hotels/{hotelId}/rates * * Also accepts auth: bearerAuth */ export const getApiV1HotelsHotelIdRates = async (args: GetApiV1HotelsHotelIdRatesArgs, options?: GetApiV1HotelsHotelIdRatesOptions) => { 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.searchId != null) query.append("searchId", String(args.searchId)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.locale != null) query.append("locale", String(args.locale)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/hotels/${encodeURIComponent(String(args.hotelId))}/rates${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(getApiV1HotelsHotelIdRatesResponse, await res.json()) };