import {z} from 'zod' export type LogEventType = 1 | 2 | 3 | 4 | 5; export type EventProtocols = 'SSH' | 'SFTP' | 'SCP' | 'FTP' | 'DAV' | 'HTTP' | 'HTTPShare' | 'DataRetention' | 'EventAction' | 'OIDC'; export type GetApiEventsLogArgs = {start_timestamp?: number | undefined, end_timestamp?: number | undefined, events?: Array | undefined, username?: string | undefined, ip?: string | undefined, protocols?: Array | undefined, instance_ids?: Array | undefined, from_id?: string | undefined, role?: string | undefined, csv_export?: boolean | undefined, limit?: number | undefined, order?: ('ASC' | 'DESC') | undefined}; export type GetApiEventsLogOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const logEventType = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]); export const eventProtocols = z.enum(["SSH", "SFTP", "SCP", "FTP", "DAV", "HTTP", "HTTPShare", "DataRetention", "EventAction", "OIDC"]); export const logEvent = z.object({id: z.string().optional(), timestamp: z.number().int().optional(), event: logEventType.optional(), protocol: eventProtocols.optional(), username: z.string().optional(), ip: z.string().optional(), message: z.string().optional(), role: z.string().optional(), instance_id: z.string().optional()}); export const getApiEventsLogResponse = z.array(logEvent); 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 }; /** * Get log events * * GET /events/log * * Also accepts auth: APIKeyAuth */ export const getApiEventsLog = async (args: GetApiEventsLogArgs, options: GetApiEventsLogOptions) => { const {baseUrl, fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/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.events != null) query.append("events", args.events.join(',')) if (args.username != null) query.append("username", String(args.username)) if (args.ip != null) query.append("ip", String(args.ip)) if (args.protocols != null) query.append("protocols", args.protocols.join(',')) if (args.instance_ids != null) query.append("instance_ids", args.instance_ids.join(',')) if (args.from_id != null) query.append("from_id", String(args.from_id)) if (args.role != null) query.append("role", String(args.role)) if (args.csv_export != null) query.append("csv_export", String(args.csv_export)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/events/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(getApiEventsLogResponse, await res.json()) };