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 CreateApiApiV1PoliciesImportArgs = {'X-Tenant-ID': string, 'X-User-ID'?: string | undefined, body: {policies: Array, mode?: ('create' | 'upsert') | undefined}}; export type CreateApiApiV1PoliciesImportOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1PoliciesImportResponse = z.object({created: z.number().int().optional(), updated: z.number().int().optional(), failed: z.number().int().optional(), errors: z.array(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 }; /** * Import policies * * POST /api/v1/policies/import */ export const createApiApiV1PoliciesImport = async (args: CreateApiApiV1PoliciesImportArgs, options?: CreateApiApiV1PoliciesImportOptions) => { 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/import${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(createApiApiV1PoliciesImportResponse, await res.json()) };