import {z} from 'zod' export type GetApiV1ReviewsArgs = {merchantId?: number | undefined, reservationId?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiV1ReviewsOptions = {baseUrl: string, APITOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const customerReviewData = z.object({identifier: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), profileImage: z.string().optional()}); export const getReviewDto = z.object({reviewId: z.string(), merchantId: z.number().int().optional(), customer: customerReviewData.optional(), status: z.enum(["AVAILABLE_TO_REVIEW", "CREATED", "DENIED", "PUBLISHED"]).optional(), locale: z.string().optional(), rating: z.number().int(), description: z.string().optional(), reservationId: z.string().optional(), createdAt: z.string().optional()}); export const getReviewsDto = z.object({reviews: z.array(getReviewDto).optional(), size: z.number().int().optional(), offset: z.number().int().optional(), limit: z.number().int().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 }; /** * Get information from reviews in a list * * GET /v1/reviews */ export const getApiV1Reviews = async (args: GetApiV1ReviewsArgs, options: GetApiV1ReviewsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APITOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.merchantId != null) query.append("merchantId", String(args.merchantId)) if (args.reservationId != null) query.append("reservationId", String(args.reservationId)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (APITOKEN !== undefined) headers.set("X-Quandoo-AuthToken", APITOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getReviewsDto, await res.json()) };