import {z} from 'zod' export type GetApiReferenceDataLocationsArgs = {subType: Array<'AIRPORT' | 'CITY'>, keyword: string, countryCode?: string | undefined, 'page[limit]'?: number | undefined, 'page[offset]'?: number | undefined, sort?: 'analytics.travelers.score' | undefined, view?: ('LIGHT' | 'FULL') | undefined}; export type GetApiReferenceDataLocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const collectionMeta = z.object({count: z.number().int().optional(), links: z.object({self: z.string().optional(), next: z.string().optional(), previous: z.string().optional(), last: z.string().optional(), first: z.string().optional(), up: z.string().optional()}).optional()}); export const links = z.object({href: z.string(), methods: z.array(z.enum(["GET", "PUT", "DELETE", "POST", "PATCH"])).optional(), count: z.number().int().optional()}); export const geoCode = z.object({latitude: z.number().optional(), longitude: z.number().optional()}); export const address = z.object({cityName: z.string().optional(), cityCode: z.string().optional(), countryName: z.string().optional(), countryCode: z.string().optional(), stateCode: z.string().optional(), regionCode: z.string().optional()}); export const distance = z.object({value: z.number().int().optional(), unit: z.enum(["KM", "MI"]).optional()}); export const travelers = z.object({score: z.number().optional()}); export const analytics = z.object({travelers: travelers.optional()}); export const location = z.object({id: z.string().optional(), self: links.optional(), type: z.string().optional(), subType: z.enum(["AIRPORT", "CITY", "POINT_OF_INTEREST", "DISTRICT"]).optional(), name: z.string().optional(), detailedName: z.string().optional(), timeZoneOffset: z.string().optional(), iataCode: z.string().optional(), geoCode: geoCode.optional(), address: address.optional(), distance: distance.optional(), analytics: analytics.optional(), relevance: z.number().optional(), category: z.enum(["SIGHTS", "BEACH_PARK", "HISTORICAL", "NIGHTLIFE", "RESTAURANT", "SHOPPING"]).optional(), tags: z.array(z.string()).optional(), rank: z.string().optional()}); export const getApiReferenceDataLocationsResponse = z.object({meta: collectionMeta.optional(), data: z.array(location)}); 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 }; /** * Returns a list of airports and cities matching a given keyword. * * GET /reference-data/locations */ export const getApiReferenceDataLocations = async (args: GetApiReferenceDataLocationsArgs, options?: GetApiReferenceDataLocationsOptions) => { const {baseUrl = "https://test.api.amadeus.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.amadeus+json") for (const item of args.subType ?? []) query.append("subType", String(item)) if (args.keyword != null) query.append("keyword", String(args.keyword)) if (args.countryCode != null) query.append("countryCode", String(args.countryCode)) if (args['page[limit]'] != null) query.append("page[limit]", String(args['page[limit]'])) if (args['page[offset]'] != null) query.append("page[offset]", String(args['page[offset]'])) if (args.sort != null) query.append("sort", String(args.sort)) if (args.view != null) query.append("view", String(args.view)) const url = `${baseUrl.replace(/\/+$/, '')}/reference-data/locations${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(getApiReferenceDataLocationsResponse, await res.json()) };