import {z} from 'zod' export type GetApiApiOrgIdRumAnalyticsFunnelsIdArgs = {org_id: string, id: string, app: string}; export type GetApiApiOrgIdRumAnalyticsFunnelsIdOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const funnelDef = z.object({b: z.string().nullable().optional(), bs: z.number().int().gte(0).nullable().optional(), f: z.array(z.object({})).nullable().optional(), s: z.array(z.array(z.object({}))), u: z.string().nullable().optional(), w: z.string().nullable().optional(), x: z.array(z.object({})).nullable().optional()}); export const savedFunnel = z.object({app: z.string(), createdAt: z.number().int(), createdBy: z.string(), currentRulesHash: z.string(), def: funnelDef, description: z.string().nullable().optional(), eventIds: z.array(z.string()), id: z.string(), name: z.string(), rulesHash: z.string().nullable().optional(), sql: z.string(), 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 }; /** * Get a saved funnel * * GET /api/{org_id}/rum/analytics/funnels/{id} */ export const getApiApiOrgIdRumAnalyticsFunnelsId = async (args: GetApiApiOrgIdRumAnalyticsFunnelsIdArgs, options: GetApiApiOrgIdRumAnalyticsFunnelsIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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/funnels/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(savedFunnel, await res.json()) };