import {z} from 'zod' export type GetApiWorkspaceAuditLogsArgs = {workspace: string, start_timestamp: string, end_timestamp: string, action?: string | undefined, entity_id?: string | undefined, entity_type?: string | undefined, entity_name?: string | undefined, username?: string | undefined, ip?: string | undefined, event_source?: string | undefined, next_token?: string | undefined}; export type GetApiWorkspaceAuditLogsOptions = {baseUrl?: string | undefined, accessTokenAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const auditLog = z.object({id: z.string().optional(), action: z.string().optional(), description: z.string().optional(), context: z.object({correlation_id: z.string().optional()}).optional(), actor: z.object({ip: z.string().optional(), user_agent: z.string().optional(), user_id: z.string().optional(), username: z.string().optional()}).optional(), entity: z.object({entity_name: z.string().optional(), entity_type: z.string().optional(), entity_id: z.string().optional()}).optional(), timestamp: z.string().optional(), workspace_username: 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 }; /** * Retrieve audit logs * * GET /{workspace}/audit_logs */ export const getApiWorkspaceAuditLogs = async (args: GetApiWorkspaceAuditLogsArgs, options?: GetApiWorkspaceAuditLogsOptions) => { const {baseUrl = "https://modeanalytics.com/api", fetch: fetchFn = fetch, accessTokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/hal+json") if (args.start_timestamp != null) query.append("start_timestamp", String(args.start_timestamp)) if (args.end_timestamp != null) query.append("end_timestamp", String(args.end_timestamp)) if (args.action != null) query.append("action", String(args.action)) if (args.entity_id != null) query.append("entity_id", String(args.entity_id)) if (args.entity_type != null) query.append("entity_type", String(args.entity_type)) if (args.entity_name != null) query.append("entity_name", String(args.entity_name)) if (args.username != null) query.append("username", String(args.username)) if (args.ip != null) query.append("ip", String(args.ip)) if (args.event_source != null) query.append("event_source", String(args.event_source)) if (args.next_token != null) query.append("next_token", String(args.next_token)) if (accessTokenAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${accessTokenAuth.username}:${accessTokenAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.workspace))}/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(auditLog, await res.json()) };