import {z} from 'zod' export type GetApiApiAuditLogsArgs = {projectId?: string | undefined, userId?: string | undefined, fromDate?: string | undefined, toDate?: string | undefined, type?: ('organisation' | 'user' | 'group' | 'accessToken' | 'project' | 'document' | 'release' | 'member' | 'personalAccessToken') | undefined, action?: string | undefined, orderBy?: string | undefined, sort?: string | undefined, page?: number | undefined, perPage?: number | undefined}; export type GetApiApiAuditLogsOptions = {baseUrl: string, personalAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiAuditLogsResponse = z.object({results: z.array(z.object({id: z.string(), createdAt: z.string(), type: z.enum(["organisation", "user", "group", "accessToken", "project", "document", "release", "member", "personalAccessToken"]), action: z.string(), ipAddress: z.string().nullable(), userAgent: z.string().nullable(), refId: z.string().nullable(), user: z.object({id: z.string(), email: z.string(), firstName: z.string().nullable(), lastName: z.string().nullable()}).nullable().optional(), project: z.object({id: z.string(), name: z.string()}).nullable().optional()})), paginate: z.object({current: z.number(), from: z.number(), to: z.number(), total: z.number(), pageSize: z.number()})}); 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 }; /** * List audit logs * * GET /api/audit-logs * * Also accepts auth: internalAuth */ export const getApiApiAuditLogs = async (args: GetApiApiAuditLogsArgs, options: GetApiApiAuditLogsOptions) => { const {baseUrl, fetch: fetchFn = fetch, personalAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.userId != null) query.append("userId", String(args.userId)) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (args.type != null) query.append("type", String(args.type)) if (args.action != null) query.append("action", String(args.action)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.page != null) query.append("page", String(args.page)) if (args.perPage != null) query.append("perPage", String(args.perPage)) if (personalAccessToken !== undefined) headers.set('authorization', `Bearer ${personalAccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(getApiApiAuditLogsResponse, await res.json()) };