import {z} from 'zod' export type CreateApiReviewsCustomChannelReviewsArgs = {body: {customChannelName: unknown, internalReservationId: unknown, externalReviewId: unknown, createdAt: string, updatedAt: string, rating: number, bodyText: unknown, reviewerName: unknown, titleText?: unknown | undefined, locale?: unknown | undefined, categoryRatings?: {[key: string]: number} | undefined}}; export type CreateApiReviewsCustomChannelReviewsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiReviewsCustomChannelReviewsResponse = z.object({_id: z.object({}), accountId: z.object({}), externalReviewId: z.string(), channelId: z.enum(["bookingCom", "airbnb2", "homeaway2", "custom"]), customChannelName: z.string().optional(), subListingId: z.object({}).optional(), listingId: z.object({}), complexId: z.object({}).optional(), externalListingId: z.string().optional(), externalComplexId: z.string().optional(), reservationId: z.object({}).optional(), externalReservationId: z.string().optional(), guestId: z.object({}).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), createdAtGuesty: z.string().optional(), updatedAtGuesty: z.string().optional(), rawReview: z.object({}), contents: z.object({body: z.string(), reviewerName: z.string(), title: z.string().optional(), locale: z.string().optional(), rating: z.number()}).optional(), reviewReplies: z.array(z.object({status: z.enum(["PENDING", "FAILED", "COMPLETED", "NOT_FOUND"]), reviewReply: z.string(), replyAt: z.string()})).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 }; /** * Publish a custom-channel review * * POST /reviews/custom-channel-reviews */ export const createApiReviewsCustomChannelReviews = async (args: CreateApiReviewsCustomChannelReviewsArgs, options?: CreateApiReviewsCustomChannelReviewsOptions) => { const {baseUrl = "https://open-api.guesty.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set("authorization", bearerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reviews/custom-channel-reviews${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiReviewsCustomChannelReviewsResponse, await res.json()) };