import {z} from 'zod' export type GetApiV1AnalyticsPoliciesViolationRateArgs = {enforcement_point?: (string | null) | undefined, channel?: (string | null) | undefined, strictness?: (string | null) | undefined, start_date: string, end_date: string}; export type GetApiV1AnalyticsPoliciesViolationRateOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const violationRateRow = z.object({policy_id: z.string(), policy_name: z.string(), strictness: z.string(), evaluations: z.number().int(), violations: z.number().int(), violation_rate_pct: z.number(), enforced: z.number().int(), would_be: z.number().int(), errors: z.number().int(), avg_latency_ms: z.number().nullable().optional()}); export const getApiV1AnalyticsPoliciesViolationRateResponse = z.array(violationRateRow); 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 }; /** * Per-policy violation rate + action counts within a window * * GET /v1/analytics/policies/violation-rate */ export const getApiV1AnalyticsPoliciesViolationRate = async (args: GetApiV1AnalyticsPoliciesViolationRateArgs, options: GetApiV1AnalyticsPoliciesViolationRateOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.enforcement_point != null) query.append("enforcement_point", String(args.enforcement_point)) if (args.channel != null) query.append("channel", String(args.channel)) if (args.strictness != null) query.append("strictness", String(args.strictness)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/analytics/policies/violation-rate${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(getApiV1AnalyticsPoliciesViolationRateResponse, await res.json()) };