import {z} from 'zod' export type ReviewsInputDto = {hotel_ids: Array, language?: ('ar' | 'bg' | 'ca' | 'cs' | 'da' | 'de' | 'el' | 'en' | 'en-gb' | 'en-us' | 'es' | 'es-ar' | 'es-mx' | 'et' | 'fi' | 'fr' | 'he' | 'hi' | 'hr' | 'hu' | 'id' | 'is' | 'it' | 'ja' | 'ka' | 'ko' | 'lt' | 'lv' | 'ms' | 'nl' | 'no' | 'pl' | 'pt-br' | 'pt-pt' | 'pt' | 'ro' | 'ru' | 'sk' | 'sl' | 'sr' | 'sv' | 'th' | 'tl' | 'tr' | 'uk' | 'vi' | 'zh' | 'zh-cn' | 'zh-tw') | undefined, country?: string | undefined, offset?: number | undefined, rows?: number | undefined, last_change?: string | undefined, score_lower_bound?: number | undefined, score_upper_bound?: number | undefined, extras?: 'REVIEW_SCORE_WORD' | undefined}; export type GetApiDemandApiV2CompatibleReviewsArgs = {reviewsInputDto: ReviewsInputDto}; export type GetApiDemandApiV2CompatibleReviewsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({ruid: z.string().optional()}); export const reviewerOutputDto = z.object({country: z.string().optional(), name: z.string().optional(), type: z.string().optional()}); export const reviewOutputDto = z.object({hotel_id: z.number().int().optional(), average_score: z.number().optional(), country: z.string().optional(), headline: z.string().optional(), hotel_url: z.string().optional(), language: z.string().optional(), pros: z.string().optional(), cons: z.string().optional(), review_id: z.number().int().optional(), date: z.string().optional(), last_change: z.string().optional(), text_meets_guidelines: z.boolean().optional(), author: reviewerOutputDto.optional(), review_score_word: z.string().optional()}); export const reviewsResponseDto = z.object({meta: meta.optional(), result: z.array(reviewOutputDto).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 }; /** * List accommodation reviews * * GET /demand-api-v2-compatible/reviews */ export const getApiDemandApiV2CompatibleReviews = async (args: GetApiDemandApiV2CompatibleReviewsArgs, options?: GetApiDemandApiV2CompatibleReviewsOptions) => { const {baseUrl = "https://metasearch-connect-api.booking.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.reviewsInputDto != null) query.append("reviewsInputDto", JSON.stringify(args.reviewsInputDto)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand-api-v2-compatible/reviews${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(reviewsResponseDto, await res.json()) };