import {z} from 'zod' export type CreateApiV2510BetaAnalyticsExperienceExperienceIdExportArgs = {experienceId: string, body: {exportType: 'orders' | 'order_line_items' | 'order_reconciliation', startTs?: string | undefined, endTs?: 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, urlParam?: ({queryParam: string | null, queryParamValue: string | null, queryParamFilterType: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | null}) | 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, userBehavior?: ({viewedCheckout?: (boolean | null) | undefined, addedToCart?: (boolean | null) | undefined, viewedProductPage?: (boolean | null) | undefined, viewedCollectionPage?: (boolean | null) | undefined}) | undefined, countryCodes?: Array | undefined, countryCodesFilterType?: ('include' | 'exclude') | undefined, maxZScore?: (number | string | null) | undefined, triggeredCustomEvents?: Array | undefined, customEventsFilterType?: ('any' | 'all' | 'none') | undefined, reachedCheckoutStage?: ('started' | 'contact_info_submitted' | 'address_info_submitted' | null) | undefined, minNetRevenue?: (number | string | null) | undefined, maxNetRevenue?: (number | string | null) | undefined, minShippingRevenue?: (number | string | null) | undefined, maxShippingRevenue?: (number | string | null) | undefined, shippingMethods?: Array | undefined, onlyProductIds?: (Array | null) | undefined, landingPageTypes?: (Array<'PDP' | 'Collection' | 'Content' | 'Blog' | 'Homepage' | 'Search' | 'Cart' | 'Other'>) | undefined, experimentProductsOnly?: (boolean | null) | undefined, onlyProductHandles?: (Array | null) | undefined}) | undefined}}; export type CreateApiV2510BetaAnalyticsExperienceExperienceIdExportOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2510BetaAnalyticsExperienceExperienceIdExportResponse = z.object({exportType: z.enum(["orders", "order_line_items", "order_reconciliation"]), experienceId: z.string(), experienceName: z.string(), url: z.string().nullable(), expiresIn: z.number(), format: z.literal("csv"), message: z.string()}); 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 }; /** * Export experience data * * POST /v25-10-beta/analytics/experience/{experienceId}/export */ export const createApiV2510BetaAnalyticsExperienceExperienceIdExport = async (args: CreateApiV2510BetaAnalyticsExperienceExperienceIdExportArgs, options?: CreateApiV2510BetaAnalyticsExperienceExperienceIdExportOptions) => { 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/experience/${encodeURIComponent(String(args.experienceId))}/export${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(createApiV2510BetaAnalyticsExperienceExperienceIdExportResponse, await res.json()) };