import {z} from 'zod' export type GetApiV1FlightsFaresFareIdOptionsArgs = {fareId: string, currency?: string | undefined, locale?: string | undefined}; export type GetApiV1FlightsFaresFareIdOptionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1FlightsFaresFareIdOptionsResponse = z.object({fareId: z.string(), currencyCode: z.string(), price: z.object({total: z.number(), totalUsd: z.number().optional(), currency: z.string(), scope: z.literal("party")}).optional(), legs: z.array(z.object({legId: z.number(), from: z.string().optional(), to: z.string().optional(), departsAt: z.string().optional(), airlines: z.array(z.string()).optional()})).optional(), options: z.array(z.object({fareOptionId: z.string(), name: z.string(), price: z.object({total: z.number(), totalUsd: z.number(), currency: z.string(), totalTaxAmount: z.number().optional(), passengers: z.array(z.object({type: z.enum(["adult", "child", "infant"]), count: z.number(), perPerson: z.object({fare: z.number(), tax: z.number(), total: z.number()}), party: z.object({fare: z.number(), tax: z.number(), total: z.number()})})).optional(), covers: z.enum(["leg", "trip"]).optional()}), refundable: z.boolean(), exchangeable: z.boolean(), baggage: z.object({cabin: z.string().optional(), checked: z.string().optional()}), penalties: z.array(z.object({type: z.enum(["change", "cancel"]), policy: z.enum(["free", "fee", "not_permitted"]), amount: z.number().optional(), currency: z.string().optional()})), termsUrls: z.array(z.string()).optional(), legId: z.number().optional()})), metadata: z.object({currencyCode: z.string(), currencyCodeSource: z.enum(["explicit", "default"]), locale: z.string(), localeSource: z.enum(["explicit", "default"])})}); 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 }; /** * List a fare's options * * GET /v1/flights/fares/{fareId}/options * * Also accepts auth: bearerAuth */ export const getApiV1FlightsFaresFareIdOptions = async (args: GetApiV1FlightsFaresFareIdOptionsArgs, options?: GetApiV1FlightsFaresFareIdOptionsOptions) => { const {baseUrl = "https://api.wego.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args.locale != null) query.append("locale", String(args.locale)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/flights/fares/${encodeURIComponent(String(args.fareId))}/options${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(getApiV1FlightsFaresFareIdOptionsResponse, await res.json()) };