import {z} from 'zod' export type GetApiHotelPurchasesHotelPurchaseIdStatusArgs = {hotel_purchase_id: string}; export type GetApiHotelPurchasesHotelPurchaseIdStatusOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({amount: z.number().int(), currency: z.string()}); export const latLng = z.object({latitude: z.number().gte(-90).lte(90), longitude: z.number().gte(-180).lte(180)}); export const viewport = z.object({sw: latLng, ne: latLng}); export const hotelFlightCashbackTarget = z.object({flight_booking_id: z.string(), flight_booking_session_id: z.string(), payment_intent_id: z.string(), maximum: money}); export const tripHotelFlightDiscount = z.object({discount: money, destination: z.string().min(3).max(3).nullable().optional(), nights: z.number().int().gt(0), average_stay_total: money.nullable().optional(), sample_size: z.number().int().gte(1).nullable().optional(), sample_bounds: viewport.nullable().optional(), hotel_booking_id: z.string().nullable().optional(), cashback_target: hotelFlightCashbackTarget.nullable().optional(), cashback_status: z.enum(["pending", "processing", "refunded", "review_required"]).nullable().optional(), cashback_obligation_id: z.string().nullable().optional(), cashback_refund_id: z.string().nullable().optional()}); export const reviewInvite = z.object({trigger: z.enum(["second_booking", "repricing_win", "repricing_backfill"]).optional(), review_url: z.string().optional(), email_invited: z.boolean(), booking_id: z.string(), booking_kind: z.enum(["flight", "hotel"]), created_at: z.string()}); export const hotelPurchaseStatusResponse = z.object({hotel_flight_discount: tripHotelFlightDiscount.nullable().optional(), hotel_purchase_id: z.string(), state: z.string(), display_state: z.string(), membership_status: z.string().nullable().optional(), final_charge_amount_cents: z.number().int().nullable().optional(), final_charge_currency: z.string().nullable().optional(), failure_reason: z.string().nullable().optional(), failure_code: z.string().nullable().optional(), credit_applied_cents: z.number().int().optional(), supplier_confirmation_code: z.string().nullable().optional(), review_invite: reviewInvite.nullable().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 Purchase Status * * GET /hotel-purchases/{hotel_purchase_id}/status */ export const getApiHotelPurchasesHotelPurchaseIdStatus = async (args: GetApiHotelPurchasesHotelPurchaseIdStatusArgs, options: GetApiHotelPurchasesHotelPurchaseIdStatusOptions) => { 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(/\/+$/, '')}/hotel-purchases/${encodeURIComponent(String(args.hotel_purchase_id))}/status${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(hotelPurchaseStatusResponse, await res.json()) };