import {z} from 'zod' export type GetApiV1AnalyticsPoliciesViolationsTimeseriesArgs = {granularity?: string | undefined, policy_id?: (string | null) | undefined, enforcement_point?: (string | null) | undefined, channel?: (string | null) | undefined, start_date: string, end_date: string}; export type GetApiV1AnalyticsPoliciesViolationsTimeseriesOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const violationsTimeseriesRow = z.object({time_bucket: z.string(), evaluations: z.number().int(), violations: z.number().int(), enforced_violations: z.number().int(), shadow_violations: z.number().int()}); export const getApiV1AnalyticsPoliciesViolationsTimeseriesResponse = z.array(violationsTimeseriesRow); 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 }; /** * Evaluation + violation volume bucketed over time (enforced vs shadow) * * GET /v1/analytics/policies/violations-timeseries */ export const getApiV1AnalyticsPoliciesViolationsTimeseries = async (args: GetApiV1AnalyticsPoliciesViolationsTimeseriesArgs, options: GetApiV1AnalyticsPoliciesViolationsTimeseriesOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.policy_id != null) query.append("policy_id", String(args.policy_id)) 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.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/violations-timeseries${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(getApiV1AnalyticsPoliciesViolationsTimeseriesResponse, await res.json()) };