import {z} from 'zod' export type GetApiPublicApiOrdersAnalyticsArgs = {start_date: string, end_date: string, group_by?: ('utm_source' | 'utm_medium' | 'utm_campaign') | undefined, status?: string | undefined, product?: string | undefined, offer?: string | undefined, paymentMethod?: string | undefined, offer_type?: string | undefined, type?: string | undefined}; export type GetApiPublicApiOrdersAnalyticsOptions = {baseUrl?: string | undefined, OAuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const orderAnalyticsResult = z.object({utm_source: z.string().optional(), utm_medium: z.string().optional(), utm_campaign: z.string().optional(), order_count: z.number().int(), gross_volume: z.string(), net_value: z.string(), total_discount: z.string(), total_fees: z.string()}); export const orderAnalyticsTotals = z.object({order_count: z.number().int(), gross_volume: z.string(), net_value: z.string(), total_discount: z.string(), total_fees: z.string()}); export const orderAnalyticsResponse = z.object({group_by: z.string(), results: z.array(orderAnalyticsResult), totals: orderAnalyticsTotals}); 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 }; /** * Analytics de Vendas * * GET /public_api/orders/analytics/ */ export const getApiPublicApiOrdersAnalytics = async (args: GetApiPublicApiOrdersAnalyticsArgs, options?: GetApiPublicApiOrdersAnalyticsOptions) => { const {baseUrl = "https://api.cakto.com.br", fetch: fetchFn = fetch, OAuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.group_by != null) query.append("group_by", String(args.group_by)) if (args.status != null) query.append("status", String(args.status)) if (args.product != null) query.append("product", String(args.product)) if (args.offer != null) query.append("offer", String(args.offer)) if (args.paymentMethod != null) query.append("paymentMethod", String(args.paymentMethod)) if (args.offer_type != null) query.append("offer_type", String(args.offer_type)) if (args.type != null) query.append("type", String(args.type)) if (OAuthToken !== undefined) headers.set('authorization', `Bearer ${OAuthToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/public_api/orders/analytics/${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(orderAnalyticsResponse, await res.json()) };