import {z} from 'zod' export type GetApiSchedulesDateStartDateEndOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiSchedulesDateStartDateEndResponse = z.object({links: z.object({next: z.string()}).nullable(), num_pages: z.number().int().gte(1), scheduled: z.array(z.object({ident: z.string(), ident_icao: z.string().nullable(), ident_iata: z.string().nullable(), actual_ident: z.string().nullable(), actual_ident_icao: z.string().nullable(), actual_ident_iata: z.string().nullable(), aircraft_type: z.string(), scheduled_in: z.string(), scheduled_out: z.string(), origin: z.string(), origin_icao: z.string().nullable(), origin_iata: z.string().nullable(), origin_lid: z.string().nullable(), destination: z.string(), destination_icao: z.string().nullable(), destination_iata: z.string().nullable(), destination_lid: z.string().nullable(), fa_flight_id: z.string().nullable(), meal_service: z.string(), seats_cabin_business: z.number().int(), seats_cabin_coach: z.number().int(), seats_cabin_first: z.number().int()}))}); 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 scheduled flights * * GET /schedules/{date_start}/{date_end} */ export const getApiSchedulesDateStartDateEnd = async (options?: GetApiSchedulesDateStartDateEndOptions) => { 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 (ApiKeyAuth !== undefined) headers.set("x-apikey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/schedules/${encodeURIComponent(String(args.date_start))}/${encodeURIComponent(String(args.date_end))}${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(getApiSchedulesDateStartDateEndResponse, await res.json()) };