import {z} from 'zod' export type CreateApiApiV1PoliciesIdTestArgs = {id: string, 'X-Tenant-ID': string, body: {query: string, context?: Record | undefined}}; export type CreateApiApiV1PoliciesIdTestOptions = {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 against input * * POST /api/v1/policies/{id}/test */ export const createApiApiV1PoliciesIdTest = async (args: CreateApiApiV1PoliciesIdTestArgs, options?: CreateApiApiV1PoliciesIdTestOptions) => { 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") if (args['X-Tenant-ID'] != null) headers.set("X-Tenant-ID", String(args['X-Tenant-ID'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/policies/${encodeURIComponent(String(args.id))}/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()) };