import {z} from 'zod' export type BusinessId = string; export type GetApiReviewsQuantitativeEvolutionArgs = {bucket?: ('day' | 'week' | 'month' | 'year') | undefined, business__in?: Array | undefined, keywords?: Array | undefined, update_date__gte?: unknown | undefined, update_date__lte?: unknown | undefined}; export type GetApiReviewsQuantitativeEvolutionOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiReviewsQuantitativeEvolutionResponse = z.array(z.object({date: z.string().optional(), added: z.number().int().optional(), previous_sum: z.number().optional(), cum_sum: z.number().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 }; /** * Quantitative evolution * * GET /reviews/quantitative-evolution */ export const getApiReviewsQuantitativeEvolution = async (args: GetApiReviewsQuantitativeEvolutionArgs, options?: GetApiReviewsQuantitativeEvolutionOptions) => { const {baseUrl = "https://api.partoo.co/v2", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bucket != null) query.append("bucket", String(args.bucket)) for (const item of args.business__in ?? []) query.append("business__in", String(item)) for (const item of args.keywords ?? []) query.append("keywords", String(item)) if (args.update_date__gte != null) query.append("update_date__gte", String(args.update_date__gte)) if (args.update_date__lte != null) query.append("update_date__lte", String(args.update_date__lte)) if (ApiKeyAuth !== undefined) headers.set("x-APIKey", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/reviews/quantitative-evolution${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(getApiReviewsQuantitativeEvolutionResponse, await res.json()) };