import {z} from 'zod' export type GetApiReferenceDataRecommendedLocationsArgs = {cityCodes: string, travelerCountryCode?: string | undefined, destinationCountryCodes?: string | undefined}; export type GetApiReferenceDataRecommendedLocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const warning = z.object({code: z.number().int(), title: z.string(), detail: z.string().optional(), source: z.object({parameter: z.string().optional(), pointer: z.string().optional(), example: z.string().optional()}).optional()}); export const recommendedLocation = z.object({subtype: z.string().optional(), name: z.string().optional(), iataCode: z.string().optional(), geoCode: z.object({latitude: z.number().gte(-90).lte(90).optional(), longitude: z.number().gte(-180).lte(180).optional()}).optional(), type: z.string().optional(), relevance: z.number().optional()}); export const meta = z.object({count: z.number().int().gte(0).optional(), links: z.object({self: z.string().optional()}).optional()}); export const responseRecommendedLocation = z.object({warnings: z.array(warning).optional(), data: z.array(recommendedLocation).optional(), meta: meta.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 }; /** * GET recommended destinations * * GET /reference-data/recommended-locations */ export const getApiReferenceDataRecommendedLocations = async (args: GetApiReferenceDataRecommendedLocationsArgs, options?: GetApiReferenceDataRecommendedLocationsOptions) => { 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") if (args.cityCodes != null) query.append("cityCodes", String(args.cityCodes)) if (args.travelerCountryCode != null) query.append("travelerCountryCode", String(args.travelerCountryCode)) if (args.destinationCountryCodes != null) query.append("destinationCountryCodes", String(args.destinationCountryCodes)) const url = `${baseUrl.replace(/\/+$/, '')}/reference-data/recommended-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(responseRecommendedLocation, await res.json()) };