import {z} from 'zod' export type GetApiReferenceDataLocationsHotelArgs = {keyword: string, subType: Array<'HOTEL_LEISURE' | 'HOTEL_GDS'>, countryCode?: string | undefined, lang?: string | undefined, max?: number | undefined}; export type GetApiReferenceDataLocationsHotelOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiReferenceDataLocationsHotelResponse = z.object({data: z.array(z.object({id: z.number().gte(1).lte(10), type: z.string(), name: z.string().min(1).max(80), iataCode: z.string().min(3).max(3), hotelIds: z.array(z.string()), subType: z.enum(["HOTEL_GDS", "HOTEL_LEISURE"]), address: z.object({cityName: z.string().min(1).max(105), stateCode: z.string().min(2).max(2).optional(), countryCode: z.string().min(2).max(2)}).optional(), geoCode: z.object({latitude: z.number(), longitude: z.number()}).optional(), relevance: z.number().int().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 }; /** * Returns a list of hotels matching a given keyword. * * GET /reference-data/locations/hotel */ export const getApiReferenceDataLocationsHotel = async (args: GetApiReferenceDataLocationsHotelArgs, options?: GetApiReferenceDataLocationsHotelOptions) => { 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.keyword != null) query.append("keyword", String(args.keyword)) for (const item of args.subType ?? []) query.append("subType", String(item)) if (args.countryCode != null) query.append("countryCode", String(args.countryCode)) if (args.lang != null) query.append("lang", String(args.lang)) if (args.max != null) query.append("max", String(args.max)) const url = `${baseUrl.replace(/\/+$/, '')}/reference-data/locations/hotel${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(getApiReferenceDataLocationsHotelResponse, await res.json()) };