import {z} from 'zod' export type CreateApiV2510BetaAnalyticsSitewideOrderDistributionArgs = {body: {start?: string | undefined, end?: string | undefined, filters?: ({deviceType?: ('any' | 'mobile' | 'desktop' | null) | undefined, visitorType?: ('any' | 'new' | 'returning' | null) | undefined, customerType?: ('new' | 'returning' | null) | undefined, sourceSitesOrChannels?: (Array<'Instagram' | 'Facebook' | 'Google' | 'Twitter' | 'TikTok' | 'Youtube' | 'Klaviyo' | 'Attentive' | 'Postscript' | 'Paid Shopping' | 'Paid Search' | 'Paid Social' | 'Paid Video' | 'Display' | 'Paid Other' | 'Organic Shopping' | 'Organic Social' | 'Organic Video' | 'Organic Search' | 'Other' | 'Referral' | 'Affiliate' | 'SMS' | 'Email' | 'Direct'>) | undefined, landingPage?: ({landingPageUrlPath: string | null, landingPageUrlPathFilterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | null} | null) | undefined, landingPageFilters?: (Array<{values?: Array | undefined, filterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull'}>) | undefined, landingPageFiltersOperator?: ('or' | 'and') | undefined, countryCodes?: Array | undefined, countryCodesFilterType?: ('include' | 'exclude') | undefined, currencyCodes?: Array | undefined, currencyCodesFilterType?: ('include' | 'exclude') | undefined, maxZScore?: (number | string | null) | undefined, onlyProductIds?: Array | undefined, landingPageTypes?: (Array<'PDP' | 'Collection' | 'Content' | 'Blog' | 'Homepage' | 'Search' | 'Cart' | 'Other'>) | undefined, allOrders?: boolean | undefined}) | undefined}}; export type CreateApiV2510BetaAnalyticsSitewideOrderDistributionOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2510BetaAnalyticsSitewideOrderDistributionResponse = z.object({currency: z.string(), orderValueDistribution: z.object({kernel_density_estimate: z.array(z.object({x: z.number(), current_period: z.number().nullable(), previous_period: z.number().nullable()})), cumulative_distribution_function: z.array(z.object({x: z.number(), current_period: z.number().nullable(), previous_period: z.number().nullable()}))}), orderBreakdown: z.array(z.object({dt: z.string(), unit_quantity: z.number(), n_orders: z.object({value: z.number().nullable()}), net_revenue: z.object({value: z.number().nullable()}), gross_profit: z.object({value: z.number().nullable()}), pct_orders_by_unit_quantity: z.object({value: z.number().nullable()}), pct_revenue_by_unit_quantity: z.object({value: z.number().nullable()}), pct_profit_by_unit_quantity: z.object({value: z.number().nullable()})}))}); 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 sitewide order distribution * * POST /v25-10-beta/analytics/sitewide/order-distribution */ export const createApiV2510BetaAnalyticsSitewideOrderDistribution = async (args: CreateApiV2510BetaAnalyticsSitewideOrderDistributionArgs, options?: CreateApiV2510BetaAnalyticsSitewideOrderDistributionOptions) => { const {baseUrl = "https://api.intelligems.io", fetch: fetchFn = fetch, intelligemsAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (intelligemsAccessToken !== undefined) headers.set("intelligems-access-token", intelligemsAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/v25-10-beta/analytics/sitewide/order-distribution${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(createApiV2510BetaAnalyticsSitewideOrderDistributionResponse, await res.json()) };