import {z} from 'zod' export type UpdateApiV2510BetaCustomEventsArgs = {body: {customEvent: {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls: Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}>, type: 'pageView', settings: Record} | {id?: string | undefined, name: string, description?: (string | null) | undefined, type: 'productPageView', settings: Array<{productId: string, handle: string}>} | {id?: string | undefined, name: string, description?: (string | null) | undefined, type: 'collectionPageView', settings: Array<{handle: string}>} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'clickEvent', settings: {selectors: Array}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'elementViewed', settings: {selectors: Array}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'scrollDepth', settings: {mobile: {percentage: number}, desktop: {percentage: number}}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'javascriptEvent', settings: {code: string}} | Array<{id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls: Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}>, type: 'pageView', settings: Record} | {id?: string | undefined, name: string, description?: (string | null) | undefined, type: 'productPageView', settings: Array<{productId: string, handle: string}>} | {id?: string | undefined, name: string, description?: (string | null) | undefined, type: 'collectionPageView', settings: Array<{handle: string}>} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'clickEvent', settings: {selectors: Array}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'elementViewed', settings: {selectors: Array}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'scrollDepth', settings: {mobile: {percentage: number}, desktop: {percentage: number}}} | {id?: string | undefined, name: string, description?: (string | null) | undefined, applyToUrls?: (Array<{matchBy: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'doesNotEqual' | 'doesNotContain' | 'doesNotStartWith' | 'doesNotEndWith' | 'isNull' | 'isNotNull' | 'regex', value: string, error?: (string | null) | undefined}> | null) | undefined, type: 'javascriptEvent', settings: {code: string}}>}}; export type UpdateApiV2510BetaCustomEventsOptions = {baseUrl?: string | undefined, intelligemsAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2510BetaCustomEventsResponse = z.object({customEvent: z.union([z.union([z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("pageView"), settings: z.object({})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("productPageView"), settings: z.array(z.object({productId: z.string(), handle: z.string()}))}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("collectionPageView"), settings: z.array(z.object({handle: z.string()}))}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("clickEvent"), settings: z.object({selectors: z.array(z.string())})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("elementViewed"), settings: z.object({selectors: z.array(z.string())})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("scrollDepth"), settings: z.object({mobile: z.object({percentage: z.number()}), desktop: z.object({percentage: z.number()})})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("javascriptEvent"), settings: z.object({code: z.string()})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("javascriptImportedEvent"), settings: z.object({registered: z.boolean()})})]), z.array(z.union([z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("pageView"), settings: z.object({})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("productPageView"), settings: z.array(z.object({productId: z.string(), handle: z.string()}))}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("collectionPageView"), settings: z.array(z.object({handle: z.string()}))}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("clickEvent"), settings: z.object({selectors: z.array(z.string())})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("elementViewed"), settings: z.object({selectors: z.array(z.string())})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("scrollDepth"), settings: z.object({mobile: z.object({percentage: z.number()}), desktop: z.object({percentage: z.number()})})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("javascriptEvent"), settings: z.object({code: z.string()})}), z.object({id: z.string(), identifier: z.string(), name: z.string(), description: z.string().nullable().optional(), eventCountLastDay: z.number().nullable().optional(), lastEventTs: z.number().nullable().optional(), error: z.string().nullable().optional(), createdAtTs: z.number(), lastUpdateTs: z.number(), archivedAtTs: z.number().nullable().optional(), applyToUrls: z.array(z.object({matchBy: z.enum(["equals", "contains", "startsWith", "endsWith", "doesNotEqual", "doesNotContain", "doesNotStartWith", "doesNotEndWith", "isNull", "isNotNull", "regex"]), value: z.string(), error: z.string().nullable().optional()})).nullable().optional(), experiencesList: z.array(z.string()).nullable().optional(), type: z.literal("javascriptImportedEvent"), settings: z.object({registered: z.boolean()})})]))])}); 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 }; /** * Create or update custom event * * PUT /v25-10-beta/custom-events */ export const updateApiV2510BetaCustomEvents = async (args: UpdateApiV2510BetaCustomEventsArgs, options?: UpdateApiV2510BetaCustomEventsOptions) => { 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/custom-events${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV2510BetaCustomEventsResponse, await res.json()) };