import {z} from 'zod' export type AuditAction = 'create' | 'read' | 'update' | 'delete' | 'authenticate' | 'authorize' | 'access' | 'enable' | 'disable' | 'start' | 'stop' | 'backup' | 'restore' | 'export' | 'import'; export type AuditOutcome = 'success' | 'failure' | 'pending'; export type AuditEventType = 'activity' | 'monitor' | 'control'; export type AuditResourceType = 'user' | 'api_key' | 'system' | 'provider' | 'virtual_key' | 'team' | 'customer' | 'role' | 'permission' | 'guardrail' | 'mcp_client' | 'mcp_tool_group' | 'plugin' | 'config' | 'session' | 'inference'; export type GetApiApiAuditLogsArgs = {page?: number | undefined, limit?: number | undefined, cursor?: string | undefined, search?: string | undefined, action?: AuditAction | undefined, actions?: string | undefined, outcome?: AuditOutcome | undefined, outcomes?: string | undefined, event_type?: AuditEventType | undefined, event_types?: string | undefined, initiator_id?: string | undefined, initiator_ids?: string | undefined, initiator_type?: AuditResourceType | undefined, initiator_types?: string | undefined, target_id?: string | undefined, target_ids?: string | undefined, target_type?: AuditResourceType | undefined, target_types?: string | undefined, start_date?: string | undefined, end_date?: string | undefined, period?: string | undefined, request_method?: string | undefined, request_methods?: string | undefined, request_path?: string | undefined, request_paths?: string | undefined, request_ip?: string | undefined, request_ips?: string | undefined, tags?: string | undefined, sort_by?: string | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiApiAuditLogsOptions = {baseUrl?: string | undefined, ManagementBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const auditEventType = z.enum(["activity", "monitor", "control"]); export const auditAction = z.enum(["create", "read", "update", "delete", "authenticate", "authorize", "access", "enable", "disable", "start", "stop", "backup", "restore", "export", "import"]); export const auditOutcome = z.enum(["success", "failure", "pending"]); export const auditResource = z.object({id: z.string(), typeURI: z.string(), name: z.string().optional(), host: z.string().optional()}); export const auditReason = z.object({reasonCode: z.string().optional(), reasonType: z.string().optional(), message: z.string().optional()}); export const auditAttachment = z.object({name: z.string(), contentType: z.string(), content: z.string()}); export const auditEvent = z.object({id: z.string(), typeURI: z.string(), eventType: auditEventType, eventTime: z.string(), action: auditAction, outcome: auditOutcome, initiator: auditResource, target: auditResource, observer: auditResource, reason: auditReason.optional(), attachments: z.array(auditAttachment).optional(), tags: z.array(z.string()).optional(), requestMethod: z.string().optional(), requestPath: z.string().optional(), requestIP: z.string().optional(), userAgent: z.string().optional(), duration: z.number().int().optional(), signature: z.string(), createdAt: z.string()}); export const auditLogsResult = z.object({audit_logs: z.array(auditEvent), total: z.number().int(), page: z.number().int(), limit: z.number().int(), total_pages: z.number().int(), has_more: z.boolean(), next_cursor: z.string().optional()}); 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 logs * * GET /api/audit-logs */ export const getApiApiAuditLogs = async (args: GetApiApiAuditLogsArgs, options?: GetApiApiAuditLogsOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, ManagementBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.search != null) query.append("search", String(args.search)) if (args.action != null) query.append("action", String(args.action)) if (args.actions != null) query.append("actions", String(args.actions)) if (args.outcome != null) query.append("outcome", String(args.outcome)) if (args.outcomes != null) query.append("outcomes", String(args.outcomes)) if (args.event_type != null) query.append("event_type", String(args.event_type)) if (args.event_types != null) query.append("event_types", String(args.event_types)) if (args.initiator_id != null) query.append("initiator_id", String(args.initiator_id)) if (args.initiator_ids != null) query.append("initiator_ids", String(args.initiator_ids)) if (args.initiator_type != null) query.append("initiator_type", String(args.initiator_type)) if (args.initiator_types != null) query.append("initiator_types", String(args.initiator_types)) if (args.target_id != null) query.append("target_id", String(args.target_id)) if (args.target_ids != null) query.append("target_ids", String(args.target_ids)) if (args.target_type != null) query.append("target_type", String(args.target_type)) if (args.target_types != null) query.append("target_types", String(args.target_types)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.period != null) query.append("period", String(args.period)) if (args.request_method != null) query.append("request_method", String(args.request_method)) if (args.request_methods != null) query.append("request_methods", String(args.request_methods)) if (args.request_path != null) query.append("request_path", String(args.request_path)) if (args.request_paths != null) query.append("request_paths", String(args.request_paths)) if (args.request_ip != null) query.append("request_ip", String(args.request_ip)) if (args.request_ips != null) query.append("request_ips", String(args.request_ips)) if (args.tags != null) query.append("tags", String(args.tags)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (ManagementBearerAuth !== undefined) headers.set('authorization', `Bearer ${ManagementBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/audit-logs${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(auditLogsResult, await res.json()) };