import {z} from 'zod' export type GetApiReferencesAirportsAirportCodeArgs = {Accept: string, airportCode: string, lang?: string | undefined, limit?: string | undefined, offset?: string | undefined, LHoperated?: boolean | undefined}; export type GetApiReferencesAirportsAirportCodeOptions = {baseUrl?: string | undefined, auth?: string | undefined, fetch?: typeof fetch | undefined}; export const coordinate = z.object({Latitude: z.number().optional(), Longitude: z.number().optional()}); export const name = z.object({"@LanguageCode": z.string().optional(), $: z.string().optional()}); export const airport = z.object({AirportCode: z.string().optional(), Position: z.object({Coordinate: coordinate.optional()}).optional(), CityCode: z.string().optional(), CountryCode: z.string().optional(), LocationType: z.string().optional(), Names: z.object({Name: z.array(name).optional()}).optional(), UtcOffset: z.number().optional(), TimeZoneId: z.string().optional()}); export const link = z.object({"@Href": z.string().optional(), "@Rel": z.string().optional()}); export const airportResource = z.object({Airports: z.object({Airport: airport.optional()}).optional(), Meta: z.object({"@Version": z.string().optional(), Link: z.array(link).optional(), TotalCount: z.number().int().optional()}).optional()}); export const airportResponse = z.object({AirportResource: airportResource.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 }; /** * Airports * * GET /references/airports/{airportCode} */ export const getApiReferencesAirportsAirportCode = async (args: GetApiReferencesAirportsAirportCodeArgs, options?: GetApiReferencesAirportsAirportCodeOptions) => { const {baseUrl = "https://api.lufthansa.com/v1", fetch: fetchFn = fetch, auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.LHoperated != null) query.append("LHoperated", String(args.LHoperated)) if (auth !== undefined) headers.set('authorization', `Bearer ${auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/references/airports/${encodeURIComponent(String(args.airportCode))}${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(airportResponse, await res.json()) };