import { z } from 'zod'; import { RESERVED_EVENT_NAMES } from '@openpanel/constants'; export const zTrackPayload = z .object({ name: z.string().min(1), properties: z.record(z.string(), z.unknown()).optional(), profileId: z.string().or(z.number()).optional(), }) .refine((data) => !RESERVED_EVENT_NAMES.includes(data.name as any), { message: `Event name cannot be one of the reserved names: ${RESERVED_EVENT_NAMES.join(', ')}`, path: ['name'], }) .refine( (data) => { if (data.name !== 'revenue') return true; const revenue = data.properties?.__revenue; if (revenue === undefined) return true; return Number.isInteger(revenue); }, { message: '__revenue must be an integer (no floats or strings)', path: ['properties', '__revenue'], }, ); export const zIdentifyPayload = z.object({ profileId: z.string().min(1), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().email().optional(), avatar: z.string().url().optional(), properties: z.record(z.unknown()).optional(), }); export const zIncrementPayload = z.object({ profileId: z.string().min(1), property: z.string().min(1), value: z.number().positive().optional(), }); export const zDecrementPayload = z.object({ profileId: z.string().min(1), property: z.string().min(1), value: z.number().positive().optional(), }); export const zAliasPayload = z.object({ profileId: z.string().min(1), alias: z.string().min(1), }); export const zTrackHandlerPayload = z.discriminatedUnion('type', [ z.object({ type: z.literal('track'), payload: zTrackPayload, }), z.object({ type: z.literal('identify'), payload: zIdentifyPayload, }), z.object({ type: z.literal('increment'), payload: zIncrementPayload, }), z.object({ type: z.literal('decrement'), payload: zDecrementPayload, }), z.object({ type: z.literal('alias'), payload: zAliasPayload, }), ]); export type ITrackPayload = z.infer; export type IIdentifyPayload = z.infer; export type IIncrementPayload = z.infer; export type IDecrementPayload = z.infer; export type IAliasPayload = z.infer; export type ITrackHandlerPayload = z.infer; // Deprecated types for beta version of the SDKs export interface DeprecatedOpenpanelEventOptions { profileId?: string; } export interface DeprecatedPostEventPayload { name: string; timestamp: string; profileId?: string; properties?: Record & DeprecatedOpenpanelEventOptions; } export interface DeprecatedUpdateProfilePayload { profileId: string; firstName?: string; lastName?: string; email?: string; avatar?: string; properties?: Record; } export interface DeprecatedIncrementProfilePayload { profileId: string; property: string; value: number; } export interface DeprecatedDecrementProfilePayload { profileId?: string; property: string; value: number; }