import {z} from 'zod' export type GetApiAuditArgs = {limit?: number | undefined, cursor?: string | undefined, q?: string | undefined, review?: ('open' | 'confirmed' | 'dismissed' | 'unreviewed') | undefined}; export type GetApiAuditOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const appliedDecisionControls = z.object({governanceRevision: z.number().int().gte(1), organizationApproval: z.object({requireSecondReview: z.boolean(), secondReviewFor: z.array(z.enum(["same_identity", "allow", "restrict", "escalate"])), approverRoles: z.array(z.enum(["owner", "admin", "analyst"]))}), restrictedRoles: z.object({approval: z.array(z.enum(["analyst", "developer", "viewer"])), production: z.array(z.enum(["analyst", "developer", "viewer"]))}), screeningPolicy: z.object({id: z.string(), version: z.number().int().gte(1), review: z.object({requireSecondReview: z.boolean(), secondReviewFor: z.array(z.enum(["same_identity", "allow", "restrict", "escalate"])), approverRoles: z.array(z.enum(["owner", "admin", "analyst"]))})}).nullable()}); export const auditPage = z.object({items: z.array(z.object({id: z.string(), caseId: z.string(), actorId: z.string(), actorEmail: z.string().nullable(), decision: z.enum(["open", "confirmed", "dismissed"]), businessDisposition: z.enum(["pending", "allow", "restrict", "escalate"]), appliedControls: appliedDecisionControls.nullable(), notes: z.string().nullable(), createdAt: z.string(), screeningId: z.string().nullable(), subjectName: z.string().nullable(), evidenceAvailable: z.boolean()})), nextCursor: z.string().nullable()}); export const getApiAuditResponse = z.object({data: auditPage}); 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 retained human review decisions * * GET /audit */ export const getApiAudit = async (args: GetApiAuditArgs, options: GetApiAuditOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.q != null) query.append("q", String(args.q)) if (args.review != null) query.append("review", String(args.review)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/audit${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(getApiAuditResponse, await res.json()) };