import {z} from 'zod' export type GetApiCasesIdArgs = {id: string, limit?: number | undefined, cursor?: string | undefined}; export type GetApiCasesIdOptions = {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 caseDetail = z.object({id: z.string(), environment: z.string(), screening_id: z.string(), monitor_id: z.string().nullable(), decision: z.string(), business_disposition: z.string(), review_started_at: z.string(), superseded_by_screening_id: z.string().nullable(), superseded_by_case_id: z.string().nullable(), created_at: z.string(), updated_at: z.string(), latest_screening_id: z.string().nullable(), counterparty_id: z.string().nullable(), assignee_id: z.string().nullable(), priority: z.string(), due_at: z.string().nullable(), escalated_at: z.string().nullable(), revision: z.number().int(), requires_second_review: z.boolean(), review_state: z.string(), submitted_by: z.string().nullable(), approved_by: z.string().nullable(), submitted_screening_id: z.string().nullable(), retention_policy: z.string(), retention_years: z.number().int().nullable(), retention_days: z.number().int().nullable(), closed_at: z.string().nullable(), retention_expires_at: z.string().nullable(), retention_trigger: z.string(), retention_event_at: z.string().nullable(), retention_event_reason: z.string().nullable(), retention_event_actor: z.string().nullable(), subjectName: z.string().nullable(), entityType: z.string().nullable(), origin: z.string(), status: z.enum(["potential_match", "no_match"]).nullable(), matchCount: z.number().int().gte(0), evidenceAvailable: z.boolean(), retention: z.enum(["standard", "minimal"]), expiresAt: z.string().nullable(), decisions: z.array(z.object({id: z.string(), actor_id: z.string(), decision: z.enum(["open", "confirmed", "dismissed"]), business_disposition: z.enum(["pending", "allow", "restrict", "escalate"]), appliedControls: appliedDecisionControls.nullable(), notes: z.string().nullable(), created_at: z.string(), actor_email: z.string().nullable(), screening_id: z.string().nullable()})), nextDecisionCursor: z.string().nullable()}); export const getApiCasesIdResponse = z.object({data: caseDetail}); 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 }; /** * Read a case and append-only decisions * * GET /cases/{id} */ export const getApiCasesId = async (args: GetApiCasesIdArgs, options: GetApiCasesIdOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cases/${encodeURIComponent(String(args.id))}${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(getApiCasesIdResponse, await res.json()) };