import {z} from 'zod' export type GetApiReviewsMinimalArgs = {Authorization: string, channels?: Array | undefined, after?: string | undefined, before?: string | undefined, submittedAfter?: string | undefined, submittedBefore?: string | undefined, count?: number | undefined, rating?: (Array<1 | 2 | 3 | 4 | 5>) | undefined, status?: Array | undefined, type?: Array | undefined, hasReply?: boolean | undefined, ignoreStatements?: boolean | undefined, query?: string | undefined, sku?: Array | undefined, orderBy?: string | undefined}; export type GetApiReviewsMinimalOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const reviewState = z.enum(["SUBMITTED", "APPROVED", "REJECTED", "MODERATION", "DISCARDED"]); export const customerReviewReplyDto = z.object({createdAt: z.string(), updatedAt: z.string(), comment: z.string(), sendNotification: z.boolean().optional()}); export const customerReviewResponseProductDto = z.object({id: z.string(), sku: z.string(), mpn: z.string().optional(), gtin: z.string().optional(), name: z.string(), url: z.string(), imageUrl: z.string().optional(), brand: z.string().optional()}); export const minimalCustomerReviewResponseDto = z.object({id: z.string(), channelRef: z.string(), rating: z.number(), title: z.string(), comment: z.string(), submittedAt: z.string(), state: reviewState, reply: customerReviewReplyDto.optional(), customer: z.object({firstName: z.string().optional(), lastName: z.string().optional(), fullName: z.string().optional(), email: z.string().optional(), mobile: z.string().optional()}).optional(), hasAttachments: z.boolean().optional(), product: customerReviewResponseProductDto.optional(), editedAt: z.string().optional()}); export const minimalCustomerReviewListResponseDto = z.object({paging: z.object({cursor: z.object({before: z.string(), after: z.string().optional()})}), items: z.array(minimalCustomerReviewResponseDto)}); 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 a list of reviews with fewer properties per each item. * * GET /reviews-minimal */ export const getApiReviewsMinimal = async (args: GetApiReviewsMinimalArgs, options: GetApiReviewsMinimalOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) for (const item of args.channels ?? []) query.append("channels", String(item)) if (args.after != null) query.append("after", String(args.after)) if (args.before != null) query.append("before", String(args.before)) if (args.submittedAfter != null) query.append("submittedAfter", String(args.submittedAfter)) if (args.submittedBefore != null) query.append("submittedBefore", String(args.submittedBefore)) if (args.count != null) query.append("count", String(args.count)) for (const item of args.rating ?? []) query.append("rating", String(item)) for (const item of args.status ?? []) query.append("status", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) if (args.hasReply != null) query.append("hasReply", String(args.hasReply)) if (args.ignoreStatements != null) query.append("ignoreStatements", String(args.ignoreStatements)) if (args.query != null) query.append("query", String(args.query)) for (const item of args.sku ?? []) query.append("sku", String(item)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) const url = `${baseUrl.replace(/\/+$/, '')}/reviews-minimal${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(minimalCustomerReviewListResponseDto, await res.json()) };