import {z} from 'zod' export type CreateApiV2AccountIdBestReviewsArgs = {account_id: string, body: {minimum_review_length?: number | undefined, maximum_review_length?: number | undefined, minimum_title_length?: number | undefined, maximum_title_length?: number | undefined, start_date?: string | undefined, end_date?: string | undefined, external_product_ids?: Array | undefined, topics?: Array | undefined, exclude_topics?: Array | undefined, max_reviews?: number | undefined, minimum_score?: number | undefined, exclude_unpublished?: boolean | undefined, exclude_syndicated?: boolean | undefined, exclude_negative_opinions?: boolean | undefined, exclude_grouped_reviews?: boolean | undefined}}; export type CreateApiV2AccountIdBestReviewsOptions = {baseUrl?: string | undefined, XYotpoToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2AccountIdBestReviewsResponse = z.object({status: z.object({code: z.number().int().optional(), message: z.string().optional()}).optional(), pagination: z.unknown(), best_review: z.object({best_reviews: z.array(z.object({review_id: z.string().optional(), external_product_id: z.string().optional(), topics: z.array(z.string()).optional()})).optional()}).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 }; /** * Best Reviews * * POST /v2/{account_id}/best_reviews */ export const createApiV2AccountIdBestReviews = async (args: CreateApiV2AccountIdBestReviewsArgs, options?: CreateApiV2AccountIdBestReviewsOptions) => { const {baseUrl = "https://developers.yotpo.com", fetch: fetchFn = fetch, XYotpoToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (XYotpoToken !== undefined) headers.set("X-Yotpo-Token", XYotpoToken) const url = `${baseUrl.replace(/\/+$/, '')}/v2/${encodeURIComponent(String(args.account_id))}/best_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(createApiV2AccountIdBestReviewsResponse, await res.json()) };