import {z} from 'zod' export type GetApiHistoryFlightsIdentArgs = {ident: string, ident_type?: ('designator' | 'registration' | 'fa_flight_id') | undefined, start?: (string | string) | undefined, end?: (string | string) | undefined, max_pages?: number | undefined, cursor?: string | undefined}; export type GetApiHistoryFlightsIdentOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiHistoryFlightsIdentResponse = z.object({links: z.object({next: z.string()}).nullable(), num_pages: z.number().int().gte(1), flights: z.array(z.object({ident: z.string(), ident_icao: z.string().nullable().optional(), ident_iata: z.string().nullable().optional(), actual_runway_off: z.string().nullable().optional(), actual_runway_on: z.string().nullable().optional(), fa_flight_id: z.string(), operator: z.string().nullable(), operator_icao: z.string().nullable().optional(), operator_iata: z.string().nullable(), flight_number: z.string().nullable(), registration: z.string().nullable(), atc_ident: z.string().nullable(), inbound_fa_flight_id: z.string().nullable(), codeshares: z.array(z.string()).nullable(), codeshares_iata: z.array(z.string()).nullable().optional(), blocked: z.boolean(), diverted: z.boolean(), cancelled: z.boolean(), position_only: z.boolean(), origin: z.object({code: z.string().nullable(), code_icao: z.string().nullable().optional(), code_iata: z.string().nullable().optional(), code_lid: z.string().nullable().optional(), timezone: z.string().nullable().optional(), name: z.string().nullable().optional(), city: z.string().nullable().optional(), airport_info_url: z.string().nullable()}).nullable(), destination: z.object({code: z.string().nullable(), code_icao: z.string().nullable().optional(), code_iata: z.string().nullable().optional(), code_lid: z.string().nullable().optional(), timezone: z.string().nullable().optional(), name: z.string().nullable().optional(), city: z.string().nullable().optional(), airport_info_url: z.string().nullable()}).nullable(), departure_delay: z.number().int().nullable(), arrival_delay: z.number().int().nullable(), filed_ete: z.number().int().nullable(), progress_percent: z.number().int().gte(0).lte(100).nullable(), status: z.string(), aircraft_type: z.string().nullable(), route_distance: z.number().int().nullable(), filed_airspeed: z.number().int().nullable(), filed_altitude: z.number().int().nullable(), route: z.string().nullable(), baggage_claim: z.string().nullable(), seats_cabin_business: z.number().int().nullable(), seats_cabin_coach: z.number().int().nullable(), seats_cabin_first: z.number().int().nullable(), gate_origin: z.string().nullable(), gate_destination: z.string().nullable(), terminal_origin: z.string().nullable(), terminal_destination: z.string().nullable(), type: z.enum(["General_Aviation", "Airline"]), scheduled_out: z.string().nullable(), estimated_out: z.string().nullable(), actual_out: z.string().nullable(), scheduled_off: z.string().nullable(), estimated_off: z.string().nullable(), actual_off: z.string().nullable(), scheduled_on: z.string().nullable(), estimated_on: z.string().nullable(), actual_on: z.string().nullable(), scheduled_in: z.string().nullable(), estimated_in: z.string().nullable(), actual_in: z.string().nullable(), foresight_predictions_available: 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 }; /** * Get information for a historical flight * * GET /history/flights/{ident} */ export const getApiHistoryFlightsIdent = async (args: GetApiHistoryFlightsIdentArgs, options?: GetApiHistoryFlightsIdentOptions) => { const {baseUrl = "https://aeroapi.flightaware.com/aeroapi", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json; charset=UTF-8") if (args.ident_type != null) query.append("ident_type", String(args.ident_type)) if (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.max_pages != null) query.append("max_pages", String(args.max_pages)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (ApiKeyAuth !== undefined) headers.set("x-apikey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/history/flights/${encodeURIComponent(String(args.ident))}${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(getApiHistoryFlightsIdentResponse, await res.json()) };