import {z} from 'zod' export type GetApiEReputationHotelSentimentsArgs = {hotelIds: Array}; export type GetApiEReputationHotelSentimentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const collectionLinks = 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()}); export const collectionMeta = z.object({count: z.number().int().optional(), links: collectionLinks.optional()}); export const errorSource = z.object({parameter: z.string().optional(), pointer: z.string().optional(), example: z.string().optional()}); export const warningNotFound = z.object({code: z.number().int(), title: z.string(), detail: z.string().optional(), source: errorSource.optional(), documentation: z.string().optional()}); export const score = z.number().int(); export const hotelSentiment = z.object({hotelId: z.string(), type: z.string().optional(), overallRating: score, numberOfRatings: z.number().int(), numberOfReviews: z.number().int(), sentiments: z.object({sleepQuality: score.optional(), service: score.optional(), facilities: score.optional(), roomComforts: score.optional(), valueForMoney: score.optional(), catering: score.optional(), swimmingPool: score.optional(), location: score.optional(), internet: score.optional(), pointsOfInterest: score.optional(), staff: score.optional()}).optional()}); export const getApiEReputationHotelSentimentsResponse = z.object({meta: collectionMeta.optional(), warnings: z.array(warningNotFound).optional(), data: z.array(hotelSentiment)}); 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 sentiments by Amadeus Hotel Ids * * GET /e-reputation/hotel-sentiments */ export const getApiEReputationHotelSentiments = async (args: GetApiEReputationHotelSentimentsArgs, options?: GetApiEReputationHotelSentimentsOptions) => { const {baseUrl = "https://test.api.amadeus.com/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.amadeus+json") for (const item of args.hotelIds ?? []) query.append("hotelIds", String(item)) const url = `${baseUrl.replace(/\/+$/, '')}/e-reputation/hotel-sentiments${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(getApiEReputationHotelSentimentsResponse, await res.json()) };