import {z} from 'zod' export type GetApiCounterpartiesIdHistoryArgs = {id: string, limit?: number | undefined, cursor?: string | undefined, kind?: ('screenings' | 'cases' | 'monitors') | undefined}; export type GetApiCounterpartiesIdHistoryOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const counterpartyScreeningHistory = z.object({id: z.string(), created_at: z.string(), expires_at: z.string().nullable(), status: z.enum(["potential_match", "no_match", "expired"]), evidence_state: z.enum(["available", "expired"])}); export const counterpartyCaseHistory = z.object({id: z.string(), created_at: z.string(), decision: z.enum(["open", "confirmed", "dismissed"]), business_disposition: z.enum(["pending", "allow", "restrict", "escalate"]), review_state: z.string(), screening_id: z.string(), latest_screening_id: z.string().nullable()}); export const counterpartyMonitorHistory = z.object({id: z.string(), created_at: z.string(), name: z.string(), status: z.enum(["active", "paused", "deleted"]), last_screening_id: z.string().nullable(), next_run_at: z.string()}); export const counterpartyHistory = z.object({items: z.array(z.union([counterpartyScreeningHistory, counterpartyCaseHistory, counterpartyMonitorHistory])), total: z.number().int().gte(0), nextCursor: z.string().nullable()}); export const getApiCounterpartiesIdHistoryResponse = z.object({data: counterpartyHistory}); 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 }; /** * Page through linked history including truthful expired evidence metadata * * GET /counterparties/{id}/history */ export const getApiCounterpartiesIdHistory = async (args: GetApiCounterpartiesIdHistoryArgs, options: GetApiCounterpartiesIdHistoryOptions) => { 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.kind != null) query.append("kind", String(args.kind)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/counterparties/${encodeURIComponent(String(args.id))}/history${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(getApiCounterpartiesIdHistoryResponse, await res.json()) };