import {z} from 'zod' export type ReviewScoresInputDto = {hotel_ids: Array, affiliate_id?: number | undefined, 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, reviewer_type?: ('SOLO_TRAVELLER' | 'YOUNG_COUPLE' | 'MATURE_COUPLE' | 'FAMILY_WITH_YOUNG_CHILDREN' | 'FAMILY_WITH_OLDER_CHILDREN' | 'WITH_FRIENDS' | 'GROUP' | 'REVIEW_CATEGORY_COUPLES' | 'REVIEW_CATEGORY_FAMILIES' | 'REVIEW_CATEGORY_GROUP_OF_FRIENDS' | 'REVIEW_CATEGORY_BUSINESS_TRAVELLERS') | undefined}; export type GetApiDemandApiV2CompatibleReviewScoresArgs = {reviewScoresInputDto: ReviewScoresInputDto, Accept?: 'application/json, application/xml' | undefined}; export type GetApiDemandApiV2CompatibleReviewScoresOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const meta = z.object({ruid: z.string().optional()}); export const questionDto = z.object({count: z.number().int().optional(), question: z.string().optional(), question_literal: z.string().optional(), score: z.number().multipleOf(0.1).optional()}); export const scoreBreakdown = z.object({average_score: z.number().multipleOf(0.1).optional(), count: z.number().int().optional(), question: z.array(questionDto).optional(), reviewer_type: z.string().optional()}); export const scoreDistribution = z.object({count: z.number().int().optional(), percent: z.number().multipleOf(0.01).optional(), score: z.number().int().optional()}); export const reviewScoresOutputDto = z.object({hotel_id: z.number().int().optional(), hotel_url: z.string().optional(), score_breakdown: z.array(scoreBreakdown).optional(), score_distribution: z.array(scoreDistribution).optional()}); export const responseOutputV2ReviewScoresOutputDto = z.object({meta: meta.optional(), result: z.array(reviewScoresOutputDto).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 review scores * * GET /demand-api-v2-compatible/reviewScores */ export const getApiDemandApiV2CompatibleReviewScores = async (args: GetApiDemandApiV2CompatibleReviewScoresArgs, options?: GetApiDemandApiV2CompatibleReviewScoresOptions) => { 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.reviewScoresInputDto != null) query.append("reviewScoresInputDto", JSON.stringify(args.reviewScoresInputDto)) if (args.Accept != null) headers.set("Accept", String(args.Accept)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/demand-api-v2-compatible/reviewScores${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(responseOutputV2ReviewScoresOutputDto, await res.json()) };