import {z} from 'zod' export type PageOp = 'eq' | 'prefix' | 'regex'; export type PageRule = {op: PageOp, value: string}; export type SessionField = 'user_agent_user_agent_family' | 'user_agent_os_family' | 'user_agent_device_family' | 'geo_info_country' | 'geo_info_city' | 'usr_id' | 'usr_email' | 'usr_anonymous_id' | 'env' | 'version'; export type MatchOp = 'eq' | 'in' | 'prefix' | 'suffix' | 'contains' | 'regex'; export type SessionRule = {field: SessionField, op: MatchOp, values: Array}; export type PutAppSettings = {bots: boolean, pages: Array, sessions: Array, version: number}; export type UpdateApiApiOrgIdRumAnalyticsSettingsArgs = {org_id: string, app: string, body: PutAppSettings}; export type UpdateApiApiOrgIdRumAnalyticsSettingsOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const builtinBots = z.object({devices: z.array(z.string()), families: z.array(z.string()), version: z.number().int().gte(0)}); export const pageOp = z.enum(["eq", "prefix", "regex"]); export const pageRule = z.object({op: pageOp, value: z.string()}); export const sessionField = z.enum(["user_agent_user_agent_family", "user_agent_os_family", "user_agent_device_family", "geo_info_country", "geo_info_city", "usr_id", "usr_email", "usr_anonymous_id", "env", "version"]); export const matchOp = z.enum(["eq", "in", "prefix", "suffix", "contains", "regex"]); export const sessionRule = z.object({field: sessionField, op: matchOp, values: z.array(z.string())}); export const appSettings = z.object({app: z.string(), bots: z.boolean(), builtinBots: builtinBots, createdAt: z.number().int().nullable().optional(), createdBy: z.string().nullable().optional(), id: z.string().nullable().optional(), pages: z.array(pageRule), rulesHash: z.string(), sessions: z.array(sessionRule), updatedAt: z.number().int().nullable().optional(), updatedBy: z.string().nullable().optional(), 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 }; /** * Save an app's exclusions at the version the caller read; 0 creates * * PUT /api/{org_id}/rum/analytics/settings */ export const updateApiApiOrgIdRumAnalyticsSettings = async (args: UpdateApiApiOrgIdRumAnalyticsSettingsArgs, options: UpdateApiApiOrgIdRumAnalyticsSettingsOptions) => { 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/settings${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(appSettings, await res.json()) };