import {z} from 'zod' export type GetApiSafetySafetyRatedLocationsArgs = {latitude: number, longitude: number, radius?: number | undefined, 'page[limit]'?: number | undefined, 'page[offset]'?: number | undefined}; export type GetApiSafetySafetyRatedLocationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const issue = z.object({status: z.number().int().optional(), code: z.number().int().optional(), title: z.string().optional(), detail: z.string().optional(), source: z.object({pointer: z.string().optional(), parameter: z.string().optional(), example: z.string().optional()}).optional()}); 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 safetyScore = z.object({women: z.number().int().optional(), physicalHarm: z.number().int().optional(), theft: z.number().int().optional(), politicalFreedom: z.number().int().optional(), lgbtq: z.number().int().optional(), medical: z.number().int().optional(), overall: z.number().int().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 safetyRatedLocation = z.object({safetyScore: safetyScore.optional(), 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(), geoCode: geoCode.optional()}); export const getApiSafetySafetyRatedLocationsResponse = z.object({warnings: z.array(issue).optional(), meta: collectionMeta.optional(), data: z.array(safetyRatedLocation)}); 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 safety rating for a given location and radius. * * GET /safety/safety-rated-locations */ export const getApiSafetySafetyRatedLocations = async (args: GetApiSafetySafetyRatedLocationsArgs, options?: GetApiSafetySafetyRatedLocationsOptions) => { 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.latitude != null) query.append("latitude", String(args.latitude)) if (args.longitude != null) query.append("longitude", String(args.longitude)) if (args.radius != null) query.append("radius", String(args.radius)) 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]'])) const url = `${baseUrl.replace(/\/+$/, '')}/safety/safety-rated-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(getApiSafetySafetyRatedLocationsResponse, await res.json()) };