import {z} from 'zod' export type AuditAction = 'CREATE' | 'UPDATE' | 'DELETE' | 'COPY' | 'SHARE'; export type GetApiApiV1AuditLogArgs = {entityType?: (string | null) | undefined, action?: (AuditAction | null) | undefined, entityId?: (string | null) | undefined, dateFrom?: (string | null) | undefined, dateTo?: (string | null) | undefined, actorEmail?: (string | null) | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1AuditLogOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const auditAction = z.enum(["CREATE", "UPDATE", "DELETE", "COPY", "SHARE"]); export const auditLogDTO = z.object({id: z.string(), userId: z.string(), entityId: z.string(), entityType: z.string(), entityName: z.string().nullable().optional(), action: auditAction, actorEmail: z.string().nullable().optional(), createdAt: z.string()}); export const pageAuditLogDTO = z.object({items: z.array(auditLogDTO), total: z.number().int().gte(0), page: z.number().int().gte(1), size: z.number().int().gte(1), pages: z.number().int().gte(0)}); 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 audit log * * GET /api/v1/audit-log */ export const getApiApiV1AuditLog = async (args: GetApiApiV1AuditLogArgs, options: GetApiApiV1AuditLogOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.entityType != null) query.append("entityType", String(args.entityType)) if (args.action != null) query.append("action", String(args.action)) if (args.entityId != null) query.append("entityId", String(args.entityId)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.actorEmail != null) query.append("actorEmail", String(args.actorEmail)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/audit-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(pageAuditLogDTO, await res.json()) };