import {z} from 'zod' export type GetApiV1FlightsSearchLinkArgs = {from: string, to: string, fromCity?: (boolean | '0' | '1' | 'true' | 'false') | undefined, toCity?: (boolean | '0' | '1' | 'true' | 'false') | undefined, fromDate: string, toDate?: string | undefined, cabin?: ('economy' | 'premium_economy' | 'business' | 'first') | undefined, adults?: number | undefined, children?: number | undefined, infants?: number | undefined, siteCode?: string | undefined, currency?: string | undefined, locale?: string | undefined}; export type GetApiV1FlightsSearchLinkOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1FlightsSearchLinkResponse = z.object({searchUrl: z.string(), expires: z.literal(false)}); 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 }; /** * Build a durable wego.com search link * * GET /v1/flights/search-link * * Also accepts auth: bearerAuth */ export const getApiV1FlightsSearchLink = async (args: GetApiV1FlightsSearchLinkArgs, options?: GetApiV1FlightsSearchLinkOptions) => { 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.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.fromCity != null) query.append("fromCity", String(args.fromCity)) if (args.toCity != null) query.append("toCity", String(args.toCity)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.cabin != null) query.append("cabin", String(args.cabin)) if (args.adults != null) query.append("adults", String(args.adults)) if (args.children != null) query.append("children", String(args.children)) if (args.infants != null) query.append("infants", String(args.infants)) if (args.siteCode != null) query.append("siteCode", String(args.siteCode)) 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/search-link${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(getApiV1FlightsSearchLinkResponse, await res.json()) };