import {z} from 'zod' export type GetApiReviewsCountArgs = {Authorization: string, channels?: Array | undefined, submittedAfter?: string | undefined, submittedBefore?: string | 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}; export type GetApiReviewsCountOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const channelReviewCountResponseDto = z.object({accountId: z.string().optional(), channelList: z.array(z.string()).optional(), totalElements: 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 the total number of reviews based on a filter. * * GET /reviews/count */ export const getApiReviewsCount = async (args: GetApiReviewsCountArgs, options: GetApiReviewsCountOptions) => { 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.submittedAfter != null) query.append("submittedAfter", String(args.submittedAfter)) if (args.submittedBefore != null) query.append("submittedBefore", String(args.submittedBefore)) 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)) const url = `${baseUrl.replace(/\/+$/, '')}/reviews/count${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(channelReviewCountResponseDto, await res.json()) };