import {z} from 'zod' export type PolicyEffect = 'allow' | 'deny' | 'cap' | 'approval' | 'safety' | 'egress'; export type PolicySubjectType = 'workspace' | 'agent' | 'user' | 'group'; export type PolicyRuleCreateRequest = {condition?: (string | null) | undefined, effect: PolicyEffect, enabled?: boolean | undefined, params?: {[key: string]: unknown} | undefined, priority?: number | undefined, subject_id: string, subject_type: PolicySubjectType, target: string}; export type CreateApiV1WorkspacesWorkspacePoliciesArgs = {workspace: string, body: PolicyRuleCreateRequest}; export type CreateApiV1WorkspacesWorkspacePoliciesOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const policyEffect = z.enum(["allow", "deny", "cap", "approval", "safety", "egress"]); export const policySubjectType = z.enum(["workspace", "agent", "user", "group"]); export const policyRuleResponse = z.object({condition: z.string().nullable().optional(), effect: policyEffect, enabled: z.boolean(), id: z.string(), params: z.record(z.string(), z.unknown()), priority: z.number().int(), subject_id: z.string(), subject_type: policySubjectType, target: 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 }; /** * Create Policy Rule * * POST /v1/workspaces/{workspace}/policies */ export const createApiV1WorkspacesWorkspacePolicies = async (args: CreateApiV1WorkspacesWorkspacePoliciesArgs, options: CreateApiV1WorkspacesWorkspacePoliciesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/${encodeURIComponent(String(args.workspace))}/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(policyRuleResponse, await res.json()) };