import {z} from 'zod' export type CreateApiApiV1PoliciesImpactReportArgs = {body: {policy_id: string, inputs: Array<{query: string, request_type?: string | undefined, user?: {[key: string]: unknown} | undefined, context?: {[key: string]: unknown} | undefined}>}}; export type CreateApiApiV1PoliciesImpactReportOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1PoliciesImpactReportResponse = z.object({policy_id: z.string().optional(), total_inputs: z.number().int().optional(), matched: z.number().int().optional(), blocked: z.number().int().optional(), match_rate: z.number().optional(), block_rate: z.number().optional(), results: z.array(z.object({input_index: z.number().int().optional(), matched: z.boolean().optional(), blocked: z.boolean().optional(), actions: z.array(z.string()).optional()})).optional(), processing_time_ms: z.number().int().optional(), generated_at: z.string().optional(), tier: z.string().optional()}); 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 }; /** * Generate impact report for a policy (Evaluation+) * * POST /api/v1/policies/impact-report * * Also accepts auth: basicAuth */ export const createApiApiV1PoliciesImpactReport = async (args: CreateApiApiV1PoliciesImpactReportArgs, options?: CreateApiApiV1PoliciesImpactReportOptions) => { const {baseUrl = "https://orchestrator.getaxonflow.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/policies/impact-report${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(createApiApiV1PoliciesImpactReportResponse, await res.json()) };