import {z} from 'zod' export type GetApiCasesArgs = {limit?: number | undefined, cursor?: string | undefined, q?: string | undefined, status?: ('potential_match' | 'no_match') | undefined, review?: ('open' | 'confirmed' | 'dismissed' | 'unreviewed') | undefined, origin?: ('manual' | 'api' | 'batch' | 'monitor' | 'unknown') | undefined, evidence?: ('available' | 'expired' | 'all') | undefined, screeningId?: string | undefined}; export type GetApiCasesOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const caseSummary = 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()}); export const casePage = z.object({items: z.array(caseSummary), nextCursor: z.string().nullable()}); export const getApiCasesResponse = z.object({data: casePage}); 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 analyst review cases * * GET /cases */ export const getApiCases = async (args: GetApiCasesArgs, options: GetApiCasesOptions) => { 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.status != null) query.append("status", String(args.status)) if (args.review != null) query.append("review", String(args.review)) if (args.origin != null) query.append("origin", String(args.origin)) if (args.evidence != null) query.append("evidence", String(args.evidence)) if (args.screeningId != null) query.append("screeningId", String(args.screeningId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/cases${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(getApiCasesResponse, await res.json()) };