import {z} from 'zod' export type UserContext = {id?: number | undefined, email?: string | undefined, role?: string | undefined, region?: string | undefined, permissions?: Array | undefined, tenant_id?: string | undefined, org_id?: string | undefined}; export type CreateApiApiV1PoliciesTestArgs = {body: {query: string, user?: UserContext | undefined, request_type?: string | undefined}}; export type CreateApiApiV1PoliciesTestOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const appliedPolicyDetail = z.object({policy_id: z.string().optional(), policy_name: z.string().optional(), description: z.string().optional(), action: z.string().optional(), risk_level: z.enum(["low", "medium", "high", "critical"]).optional(), allow_override: z.boolean().optional(), matched_rule: z.string().optional(), segment_id: z.string().optional()}); export const policyEvaluationResult = z.object({allowed: z.boolean().optional(), applied_policies: z.array(z.string()).optional(), risk_score: z.number().gte(0).lte(1).optional(), severity: z.enum(["critical", "high", "medium", "low"]).optional(), severity_policy_id: z.string().optional(), required_actions: z.array(z.string()).optional(), processing_time_ms: z.number().int().optional(), database_accessed: z.boolean().optional(), evaluation_error: z.boolean().optional(), segments_resolved: z.boolean().optional(), applied_policies_detail: z.array(appliedPolicyDetail).optional(), preferred_provider: z.string().optional(), allowed_providers: z.array(z.string()).optional(), routing_reason: 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 }; /** * Test policy evaluation * * POST /api/v1/policies/test */ export const createApiApiV1PoliciesTest = async (args: CreateApiApiV1PoliciesTestArgs, options?: CreateApiApiV1PoliciesTestOptions) => { const {baseUrl = "https://orchestrator.getaxonflow.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/policies/test${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(policyEvaluationResult, await res.json()) };