import {z} from 'zod' export type PolicySubjectType = 'workspace' | 'agent' | 'user' | 'group'; export type PolicyEffect = 'allow' | 'deny' | 'cap' | 'approval' | 'safety' | 'egress'; export type GetApiV1WorkspacesWorkspacePoliciesArgs = {workspace: string, subject_type?: (PolicySubjectType | null) | undefined, subject_id?: (string | null) | undefined, effect?: (PolicyEffect | null) | undefined, target?: (string | null) | undefined, enabled?: (boolean | null) | undefined}; export type GetApiV1WorkspacesWorkspacePoliciesOptions = {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 getApiV1WorkspacesWorkspacePoliciesResponse = z.array(policyRuleResponse); 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 }; /** * List Policy Rules * * GET /v1/workspaces/{workspace}/policies */ export const getApiV1WorkspacesWorkspacePolicies = async (args: GetApiV1WorkspacesWorkspacePoliciesArgs, options: GetApiV1WorkspacesWorkspacePoliciesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.subject_type != null) query.append("subject_type", String(args.subject_type)) if (args.subject_id != null) query.append("subject_id", String(args.subject_id)) if (args.effect != null) query.append("effect", String(args.effect)) if (args.target != null) query.append("target", String(args.target)) if (args.enabled != null) query.append("enabled", String(args.enabled)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1WorkspacesWorkspacePoliciesResponse, await res.json()) };