import {z} from 'zod' export type GetApiBookReservationIdRecoveryArgs = {reservationId: string, test_case: 'pricejump'}; export type GetApiBookReservationIdRecoveryOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBookReservationIdRecoveryResponse = z.object({reservation_id: z.string().optional(), status: z.enum(["SUCCESS", "PROCESSING", "ERROR"]).optional(), sub_status: z.string().optional(), price_jump: z.object({difference: z.number().optional(), price_before_price_jump: z.number().optional(), price_after_price_jump: z.number().optional(), currency: z.string().optional(), timeout: z.string().optional(), message: z.object({message_type: z.string().optional(), price_jump_confirmations: z.array(z.object({flavor_id: z.string().optional(), confirm: z.boolean().optional()})).optional()}).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 }; /** * Price Jump * * GET /book/{reservationId}/recovery */ export const getApiBookReservationIdRecovery = async (args: GetApiBookReservationIdRecoveryArgs, options?: GetApiBookReservationIdRecoveryOptions) => { const {baseUrl = "https://api-dev.despegar.com/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.test_case != null) query.append("test_case", String(args.test_case)) if (ApiKeyAuth !== undefined) headers.set("x-apikey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/book/${encodeURIComponent(String(args.reservationId))}/recovery${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(getApiBookReservationIdRecoveryResponse, await res.json()) };