import {z} from 'zod' export type ProviderEventAction = 'add' | 'update' | 'delete'; export type ProviderEventObjectType = 'user' | 'folder' | 'group' | 'admin' | 'api_key' | 'share' | 'event_action' | 'event_rule' | 'role'; export type GetApiEventsProviderArgs = {start_timestamp?: number | undefined, end_timestamp?: number | undefined, actions?: Array | undefined, username?: string | undefined, ip?: string | undefined, object_name?: string | undefined, object_types?: Array | undefined, instance_ids?: Array | undefined, from_id?: string | undefined, role?: string | undefined, csv_export?: boolean | undefined, omit_object_data?: boolean | undefined, limit?: number | undefined, order?: ('ASC' | 'DESC') | undefined}; export type GetApiEventsProviderOptions = {baseUrl: string, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const providerEventAction = z.enum(["add", "update", "delete"]); export const providerEventObjectType = z.enum(["user", "folder", "group", "admin", "api_key", "share", "event_action", "event_rule", "role"]); export const providerEvent = z.object({id: z.string().optional(), timestamp: z.number().int().optional(), action: providerEventAction.optional(), username: z.string().optional(), ip: z.string().optional(), object_type: providerEventObjectType.optional(), object_name: z.string().optional(), object_data: z.string().optional(), role: z.string().optional(), instance_id: z.string().optional()}); export const getApiEventsProviderResponse = z.array(providerEvent); 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 provider events * * GET /events/provider * * Also accepts auth: APIKeyAuth */ export const getApiEventsProvider = async (args: GetApiEventsProviderArgs, options: GetApiEventsProviderOptions) => { 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.actions != null) query.append("actions", args.actions.join(',')) if (args.username != null) query.append("username", String(args.username)) if (args.ip != null) query.append("ip", String(args.ip)) if (args.object_name != null) query.append("object_name", String(args.object_name)) if (args.object_types != null) query.append("object_types", args.object_types.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.omit_object_data != null) query.append("omit_object_data", String(args.omit_object_data)) 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/provider${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(getApiEventsProviderResponse, await res.json()) };