import {z} from 'zod' export type GetApiAuditLogArgs = {agentId?: string | undefined, action?: ('sign' | 'approve' | 'reject' | 'proxy') | undefined, status?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, page?: number | undefined, limit?: number | undefined}; export type GetApiAuditLogOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAuditLogResponse = z.object({ok: z.literal(true), data: z.object({data: z.array(z.object({id: z.string(), timestamp: z.string(), agentId: z.string(), action: z.enum(["sign", "approve", "reject", "proxy"]), status: z.string(), details: z.record(z.string(), z.unknown()), policyResults: z.unknown(), value: z.string().optional(), to: z.string().optional()})), pagination: z.object({page: z.number().int().gte(1), limit: z.number().int().gte(1).lte(200), total: z.number().int().gte(0), totalPages: z.number().int().gte(0)})})}); 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 tenant transaction and proxy audit log entries * * GET /audit/log */ export const getApiAuditLog = async (args: GetApiAuditLogArgs, options?: GetApiAuditLogOptions) => { const {baseUrl = "http://localhost:3200", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.agentId != null) query.append("agentId", String(args.agentId)) if (args.action != null) query.append("action", String(args.action)) if (args.status != null) query.append("status", String(args.status)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/audit/log${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(getApiAuditLogResponse, await res.json()) };