import {z} from 'zod' export type PolicyUpdate = {name?: (string | null) | undefined, description?: (string | null) | undefined, enabled?: (boolean | null) | undefined, metadata?: ({[key: string]: unknown} | null) | undefined}; export type UpdateApiV1PoliciesPolicyIdArgs = {policy_id: string, body: PolicyUpdate}; export type UpdateApiV1PoliciesPolicyIdOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const policyRevisionResponse = z.object({id: z.string(), policy_id: z.string(), name: z.string(), description: z.string().nullable().optional(), check_type: z.string(), check_config: z.record(z.string(), z.unknown()).optional(), enforcement_point: z.string(), action: z.string(), action_config: z.record(z.string(), z.unknown()).nullable().optional(), tool_target: z.string().nullable().optional(), mode: z.string(), on_error: z.string(), timeout_ms: z.number().int().nullable().optional(), strictness: z.string(), priority: z.number().int(), created_by: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string()}); export const policyResponse = z.object({id: z.string(), organization_id: z.string(), name: z.string(), description: z.string().nullable().optional(), enabled: z.boolean(), scope: z.string(), active_revision_id: z.string().nullable().optional(), active_revision: policyRevisionResponse.nullable().optional(), metadata: z.record(z.string(), z.unknown()).optional(), created_at: z.string(), updated_at: z.string()}); 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 }; /** * Update a policy's identity fields * * PUT /v1/policies/{policy_id} */ export const updateApiV1PoliciesPolicyId = async (args: UpdateApiV1PoliciesPolicyIdArgs, options: UpdateApiV1PoliciesPolicyIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/policies/${encodeURIComponent(String(args.policy_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(policyResponse, await res.json()) };