import {z} from 'zod' export type AuditActorType = 'user' | 'system' | 'API'; export type AuditActionType = 'create' | 'edit' | 'delete' | 'info'; export type AuditEntityType = 'cosigner' | 'cosigner_policy' | 'cosigner_transaction' | 'cosigner_manual_action' | 'label' | 'asset' | 'monitor' | 'api_key' | 'user_session' | 'audit_export' | 'metric' | 'response_wallet' | 'rpc_gateway_rule' | 'settings' | 'endpoint' | 'feature' | 'backoffice_api_key' | 'organization' | 'organization_extended'; export type AuditLogSearchRequest = {start_date: string, end_date: string, sort?: ('asc' | 'desc') | undefined, actor_types?: Array | undefined, actor_names?: Array | undefined, actions?: Array | undefined, entity_types?: Array | undefined, entity_ids?: Array | undefined, chains?: Array | undefined, search_text?: string | undefined, page?: number | undefined, page_size?: number | undefined}; export type CreateApiV0PlatformAuditLogsSearchArgs = {body: AuditLogSearchRequest}; export type CreateApiV0PlatformAuditLogsSearchOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const auditActorType = z.enum(["user", "system", "API"]); export const auditActionType = z.enum(["create", "edit", "delete", "info"]); export const auditEntityType = z.enum(["cosigner", "cosigner_policy", "cosigner_transaction", "cosigner_manual_action", "label", "asset", "monitor", "api_key", "user_session", "audit_export", "metric", "response_wallet", "rpc_gateway_rule", "settings", "endpoint", "feature", "backoffice_api_key", "organization", "organization_extended"]); export const auditLogItem = z.object({id: z.string(), actor: z.string().optional(), entity_id: z.string().optional(), chain: z.string().optional(), summary: z.string().optional(), timestamp: z.string(), actor_type: auditActorType, action: auditActionType, entity_type: auditEntityType}); export const auditLogSearchResponse = z.object({items: z.array(auditLogItem), total: z.number().int(), page: z.number().int(), pages: z.number().int(), size: z.number().int()}); 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 }; /** * Search Audit Logs * * POST /v0/platform/audit-logs/search * * Also accepts auth: ClientID, JWT */ export const createApiV0PlatformAuditLogsSearch = async (args: CreateApiV0PlatformAuditLogsSearchArgs, options?: CreateApiV0PlatformAuditLogsSearchOptions) => { const {baseUrl = "https://api.blockaid.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("X-API-Key", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/platform/audit-logs/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(auditLogSearchResponse, await res.json()) };