import {z} from 'zod' export type GetApiFlightsFutureArgs = {key: string, iataCode: string, type: string, date: string, airline_iata?: string | undefined, airline_icao?: string | undefined, flight_num?: string | undefined}; export type GetApiFlightsFutureOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const futureSchedulesResponse = z.object({weekday: z.string().optional(), departure: z.object({iataCode: z.string().optional(), icaoCode: z.string().optional(), terminal: z.string().optional(), gate: z.string().optional(), scheduledTime: z.string().optional()}).optional(), arrival: z.object({iataCode: z.string().optional(), icaoCode: z.string().optional(), terminal: z.string().optional(), gate: z.string().optional(), scheduledTime: z.string().optional()}).optional(), aircraft: z.object({modelCode: z.string().optional(), modelText: z.string().optional()}).optional(), airline: z.object({name: z.string().optional(), iataCode: z.string().optional(), icaoCode: z.string().optional()}).optional(), flight: z.object({number: z.string().optional(), iataNumber: z.string().optional(), icaoNumber: z.string().optional()}).optional(), codeshared: z.object({airline: z.object({name: z.string().optional(), iataCode: z.string().optional(), icaoCode: z.string().optional()}).optional(), flight: z.object({number: z.string().optional(), iataNumber: z.string().optional(), icaoNumber: z.string().optional()}).optional()}).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 }; /** * Complete daily departure and arrival schedule data of airports worldwide on a given future date. * * GET /flightsFuture */ export const getApiFlightsFuture = async (args: GetApiFlightsFutureArgs, options?: GetApiFlightsFutureOptions) => { const {baseUrl = "https://aviation-edge.com/v2/public", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.key != null) query.append("key", String(args.key)) if (args.iataCode != null) query.append("iataCode", String(args.iataCode)) if (args.type != null) query.append("type", String(args.type)) if (args.date != null) query.append("date", String(args.date)) if (args.airline_iata != null) query.append("airline_iata", String(args.airline_iata)) if (args.airline_icao != null) query.append("airline_icao", String(args.airline_icao)) if (args.flight_num != null) query.append("flight_num", String(args.flight_num)) if (apiKey !== undefined) query.set("key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/flightsFuture${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(futureSchedulesResponse, await res.json()) };