import {z} from 'zod' export type PassengerCount = {adults?: number | undefined, children?: number | undefined, infants_in_seat?: number | undefined, infants_on_lap?: number | undefined}; export type TripFlightSearch = {origin: string, destination: string, depart_date: string, return_date?: (string | null) | undefined, passenger_mix?: PassengerCount | undefined, cabin_class?: ('economy' | 'premium_economy' | 'business' | 'first') | undefined, nonstop?: boolean | undefined, airlines?: string | undefined, exclude_airlines?: string | undefined, include_redeye?: boolean | undefined, max_stops?: (number | null) | undefined, depart_after?: string | undefined, depart_before?: string | undefined}; export type EntrySearchWarmRequest = {flight_search: TripFlightSearch}; export type CreateApiConvEntrySearchWarmArgs = {body: EntrySearchWarmRequest}; export type CreateApiConvEntrySearchWarmOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const entrySearchWarmResponse = z.object({started: z.boolean()}); 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 }; /** * Warm Entry Search * * POST /conv/entry-search/warm */ export const createApiConvEntrySearchWarm = async (args: CreateApiConvEntrySearchWarmArgs, options: CreateApiConvEntrySearchWarmOptions) => { 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 (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/conv/entry-search/warm${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(entrySearchWarmResponse, await res.json()) };