import {z} from 'zod' export type GetApiAuditArgs = {page?: number | undefined, size?: number | undefined, before?: string | undefined, after?: string | undefined, range_field?: string | undefined, actor?: string | undefined, action?: string | undefined, level?: ('INFO' | 'WARN' | 'ERROR') | undefined, domain?: ('USER_MANAGEMENT' | 'CONFIG_MANAGEMENT' | 'OTHER') | undefined}; export type GetApiAuditOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sort = z.object({direction: z.string().optional(), property: z.string().optional(), ignoreCase: z.boolean().optional(), nullHandling: z.string().optional(), ascending: z.boolean().optional(), descending: z.boolean().optional()}); export const auditEvent = z.object({actor: z.string().optional(), action: z.string().optional(), eventId: z.string().optional(), message: z.string().optional(), metadata: z.string().optional(), domain: z.enum(["USER_MANAGEMENT", "CONFIG_MANAGEMENT", "OTHER"]).optional(), level: z.enum(["INFO", "WARN", "ERROR"]).optional(), timeStamp: z.string().optional()}); export const auditEventResponse = z.object({first: z.boolean().optional(), last: z.boolean().optional(), number: z.number().int().optional(), numberOfElements: z.number().int().optional(), size: z.number().int().optional(), totalElements: z.number().int().optional(), totalPages: z.number().int().optional(), sort: z.array(sort).optional(), content: z.array(auditEvent).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 }; /** * Get audits * * GET /audit */ export const getApiAudit = async (args: GetApiAuditArgs, options: GetApiAuditOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = 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.size != null) query.append("size", String(args.size)) if (args.before != null) query.append("before", String(args.before)) if (args.after != null) query.append("after", String(args.after)) if (args.range_field != null) query.append("range_field", String(args.range_field)) if (args.actor != null) query.append("actor", String(args.actor)) if (args.action != null) query.append("action", String(args.action)) if (args.level != null) query.append("level", String(args.level)) if (args.domain != null) query.append("domain", String(args.domain)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/audit${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(auditEventResponse, await res.json()) };