import {z} from 'zod' export type DID = unknown; export type GetApiEntitiesEntityDIDAuditLogArgs = {entityDID: DID, domain?: string | undefined, action?: string | undefined, actor_type?: string | undefined, actor_id?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiEntitiesEntityDIDAuditLogOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiEntitiesEntityDIDAuditLogResponse = z.object({data: z.array(z.object({id: z.string(), timestamp: z.union([z.string(), z.string()]), domain: z.string(), action: z.string(), actorType: z.string(), actorId: z.string(), actorName: z.string().nullable().optional(), actorMetadata: z.record(z.string(), z.unknown()).nullable().optional(), entityDid: z.string(), target: z.record(z.string(), z.unknown()), context: z.record(z.string(), z.unknown()), result: z.record(z.string(), z.unknown())})), pagination: z.object({total: z.number(), limit: z.number(), offset: z.number()})}); 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 log events * * GET /entities/{entityDID}/audit-log */ export const getApiEntitiesEntityDIDAuditLog = async (args: GetApiEntitiesEntityDIDAuditLogArgs, options?: GetApiEntitiesEntityDIDAuditLogOptions) => { const {baseUrl = "https://api.eu1.notabene.id", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.domain != null) query.append("domain", String(args.domain)) if (args.action != null) query.append("action", String(args.action)) if (args.actor_type != null) query.append("actor_type", String(args.actor_type)) if (args.actor_id != null) query.append("actor_id", String(args.actor_id)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/${encodeURIComponent(String(args.entityDID))}/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(getApiEntitiesEntityDIDAuditLogResponse, await res.json()) };