import {z} from 'zod' export type GetApiAuditTrailArgs = {actorId?: string | undefined, walletId?: string | undefined, type?: ('business' | 'wallet') | undefined, limit?: number | undefined, offset?: number | undefined, orderBy?: 'createdAt' | undefined, order?: ('asc' | 'desc') | undefined}; export type GetApiAuditTrailOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const auditTrailResponse = z.object({id: z.string(), actorType: z.string().nullable(), actorId: z.string().nullable(), targetEventType: z.string().nullable(), targetEventAction: z.string(), targetEventId: z.string().nullable(), businessId: z.string().nullable(), walletId: z.string().nullable(), metadata: z.unknown(), createdAt: z.string(), eventTimestamp: z.string()}); export const errorResponse = z.object({error: z.string()}); export const getApiAuditTrailResponse = z.union([z.array(auditTrailResponse), errorResponse]); 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 Audit Trail Records * * GET /audit-trail */ export const getApiAuditTrail = async (args: GetApiAuditTrailArgs, options: GetApiAuditTrailOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.actorId != null) query.append("actorId", String(args.actorId)) if (args.walletId != null) query.append("walletId", String(args.walletId)) if (args.type != null) query.append("type", String(args.type)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.order != null) query.append("order", String(args.order)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/audit-trail${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(getApiAuditTrailResponse, await res.json()) };