import {z} from 'zod' export type GetApiConvTripsTripIdHotelSearchContextArgs = {trip_id: string}; export type GetApiConvTripsTripIdHotelSearchContextOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const guestRoomOutput = z.object({adults: z.number().int().gte(1).lte(10), children: z.array(z.record(z.string(), z.number().int())).optional()}); export const hotelSearchGuestConfig = z.object({rooms: z.array(guestRoomOutput)}); export const hotelSearchAnchorSource = z.enum(["city", "neighborhood", "hotel", "airport", "user_phrase", "map"]); export const hotelSearchAnchor = z.object({latitude: z.number().gte(-90).lte(90), longitude: z.number().gte(-180).lte(180), radius_km: z.number().gt(0).lte(50), label: z.string().min(1), source: hotelSearchAnchorSource, place_code: z.string().min(1).max(32).nullable().optional()}); export const hotelSearchCriteria = z.object({check_in: z.string(), check_out: z.string(), guests: hotelSearchGuestConfig, anchor: hotelSearchAnchor}); 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 Trip Hotel Search Context * * GET /conv/trips/{trip_id}/hotel-search-context */ export const getApiConvTripsTripIdHotelSearchContext = async (args: GetApiConvTripsTripIdHotelSearchContextArgs, options: GetApiConvTripsTripIdHotelSearchContextOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conv/trips/${encodeURIComponent(String(args.trip_id))}/hotel-search-context${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(hotelSearchCriteria, await res.json()) };