import {z} from 'zod' export type FlightSearchLegChoices = {stops?: ('nonstop' | 'one' | 'twoPlus' | null) | undefined, airline_only?: (string | null) | undefined, avoided_airlines?: Array | undefined}; export type FlightFarePreferences = {seat_selection?: (boolean | null) | undefined, carry_on?: (boolean | null) | undefined, exclude_basic_economy?: (boolean | null) | undefined, checked_bags?: (number | null) | undefined, changes?: (boolean | null) | undefined, refunds?: (boolean | null) | undefined}; export type FlightSearchChoices = {airline?: (string | null) | undefined, max_stops?: (number | null) | undefined, outbound?: (FlightSearchLegChoices | null) | undefined, returning?: (FlightSearchLegChoices | null) | undefined, fare_preferences?: (FlightFarePreferences | null) | undefined}; export type CreateApiConvAgentFlightQuotesTokenClaimArgs = {token: string, fare_index?: (number | null) | undefined, open_checkout?: boolean | undefined, body: FlightSearchChoices | null}; export type CreateApiConvAgentFlightQuotesTokenClaimOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const money = z.object({amount: z.number().int(), currency: z.string()}); export const agentFlightPassengers = z.object({adults: z.number().int().gte(1).lte(9), children: z.number().int().gte(0).lte(8).optional(), infants_in_seat: z.number().int().gte(0).lte(8).optional(), infants_on_lap: z.number().int().gte(0).lte(8).optional()}); export const agentFlightQuoteClaimResponse = z.object({trip_id: z.string(), quote_id: z.string(), checkout_token: z.string().nullable().optional(), observation_id: z.string().nullable().optional(), fare_name: z.string().nullable().optional(), fare_family_id: z.string().nullable().optional(), quoted_price: money.nullable().optional(), passengers: agentFlightPassengers.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 }; /** * Claim Agent Flight Quote * * POST /conv/agent-flight-quotes/{token}/claim */ export const createApiConvAgentFlightQuotesTokenClaim = async (args: CreateApiConvAgentFlightQuotesTokenClaimArgs, options: CreateApiConvAgentFlightQuotesTokenClaimOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.fare_index != null) query.append("fare_index", String(args.fare_index)) if (args.open_checkout != null) query.append("open_checkout", String(args.open_checkout)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conv/agent-flight-quotes/${encodeURIComponent(String(args.token))}/claim${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(agentFlightQuoteClaimResponse, await res.json()) };