import {z} from 'zod' export type ViewOp = 'eq' | 'prefix' | 'regex' | 'param' | 'ref'; export type MatchOp = 'eq' | 'in' | 'prefix' | 'suffix' | 'contains' | 'regex'; export type Rule = {name?: (string | null) | undefined, onPage?: (string | null) | undefined, op: ViewOp, t: 'view', value?: (string | null) | undefined} | {onPage?: (string | null) | undefined, op?: (MatchOp | null) | undefined, t: 'action', targets?: Array | undefined, value?: (string | null) | undefined} | {onPage?: (string | null) | undefined, op: MatchOp, t: 'error', value: string}; export type CreateNamedEvent = {name: string, rules: Array}; export type CreateApiApiOrgIdRumAnalyticsNamedEventsArgs = {org_id: string, app: string, body: CreateNamedEvent}; export type CreateApiApiOrgIdRumAnalyticsNamedEventsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const viewOp = z.enum(["eq", "prefix", "regex", "param", "ref"]); export const matchOp = z.enum(["eq", "in", "prefix", "suffix", "contains", "regex"]); export const rule = z.union([z.object({name: z.string().nullable().optional(), onPage: z.string().nullable().optional(), op: viewOp, t: z.literal("view"), value: z.string().nullable().optional()}), z.object({onPage: z.string().nullable().optional(), op: matchOp.nullable().optional(), t: z.literal("action"), targets: z.array(z.string()).optional(), value: z.string().nullable().optional()}), z.object({onPage: z.string().nullable().optional(), op: matchOp, t: z.literal("error"), value: z.string()})]); export const namedEvent = z.object({app: z.string(), createdAt: z.number().int(), createdBy: z.string(), id: z.string(), name: z.string(), rules: z.array(rule), updatedAt: z.number().int(), updatedBy: z.string(), version: z.number().int()}); 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 a named event * * POST /api/{org_id}/rum/analytics/named_events */ export const createApiApiOrgIdRumAnalyticsNamedEvents = async (args: CreateApiApiOrgIdRumAnalyticsNamedEventsArgs, options: CreateApiApiOrgIdRumAnalyticsNamedEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.app != null) query.append("app", String(args.app)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/rum/analytics/named_events${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(namedEvent, await res.json()) };