import {z} from 'zod' export type CreatePolicyRequest = {name: string, description?: string | undefined, type: 'static' | 'dynamic', category?: string | undefined, tier?: ('organization' | 'tenant') | undefined, pattern?: string | undefined, action?: ('block' | 'require_approval' | 'redact' | 'warn' | 'log') | undefined, severity?: ('critical' | 'high' | 'medium' | 'low') | undefined, priority?: number | undefined, enabled?: boolean | undefined, conditions?: Array> | undefined, actions?: Array> | undefined}; export type CreateApiApiV1PoliciesArgs = {'X-Tenant-ID': string, 'X-User-ID'?: string | undefined, body: CreatePolicyRequest}; export type CreateApiApiV1PoliciesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const policy = z.object({id: z.string().optional(), policy_id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), type: z.enum(["static", "dynamic"]).optional(), category: z.string().optional(), tier: z.enum(["system", "organization", "tenant"]).optional(), pattern: z.string().optional(), action: z.enum(["block", "require_approval", "redact", "warn", "log"]).optional(), severity: z.enum(["critical", "high", "medium", "low"]).optional(), priority: z.number().int().optional(), enabled: z.boolean().optional(), conditions: z.array(z.object({})).optional(), actions: z.array(z.object({})).optional(), version: z.number().int().optional(), tenant_id: z.string().optional(), organization_id: z.string().optional(), created_by: z.string().optional(), created_at: z.string().optional(), updated_by: z.string().optional(), updated_at: 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 }; /** * Create policy * * POST /api/v1/policies */ export const createApiApiV1Policies = async (args: CreateApiApiV1PoliciesArgs, options?: CreateApiApiV1PoliciesOptions) => { 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'])) if (args['X-User-ID'] != null) headers.set("X-User-ID", String(args['X-User-ID'])) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/policies${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(policy, await res.json()) };