import {z} from 'zod' export type GetApiV2AuditEventsArgs = {'filter[channel]'?: (string | null) | undefined, 'filter[user.id]'?: (string | null) | undefined, 'filter[action]'?: (string | null) | undefined, 'filter[target_type]'?: (string | null) | undefined, 'filter[request_id]'?: (string | null) | undefined, 'filter[created_gte]'?: (string | null) | undefined, 'filter[created_lte]'?: (string | null) | undefined, 'page[size]'?: number | undefined, 'page[cursor]'?: (string | null) | undefined}; export type GetApiV2AuditEventsOptions = {baseUrl: string, JWTAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const auditEventUser = z.object({id: z.string().nullable(), username: z.string().nullable()}); export const auditEventClient = z.object({id: z.string().nullable().optional(), name: z.string().nullable()}); export const auditEventTarget = z.object({type: z.string(), id: z.string(), display_name: z.string()}); export const auditEventAttributes = z.object({channel: z.string().nullable(), user: auditEventUser, client: auditEventClient, action: z.string(), target: auditEventTarget, field: z.string().nullable(), old_value: z.string().nullable(), new_value: z.string().nullable(), request_id: z.string().nullable().optional(), endpoint: z.string().nullable(), created_at: z.string()}); export const auditEventResource = z.object({type: z.string().optional(), id: z.string(), attributes: auditEventAttributes}); export const paginationLinks = z.object({self: z.string().nullable().optional(), next: z.string().nullable().optional(), prev: z.string().nullable().optional()}); export const paginatedListResponseAuditEventResource = z.object({data: z.array(auditEventResource), meta: z.record(z.string(), z.unknown()).nullable().optional(), links: paginationLinks.nullable().optional(), included: z.array(z.record(z.string(), z.unknown())).nullable().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 }; /** * List audit events * * GET /v2/audit-events/ */ export const getApiV2AuditEvents = async (args: GetApiV2AuditEventsArgs, options: GetApiV2AuditEventsOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args['filter[channel]'] != null) query.append("filter[channel]", String(args['filter[channel]'])) if (args['filter[user.id]'] != null) query.append("filter[user.id]", String(args['filter[user.id]'])) if (args['filter[action]'] != null) query.append("filter[action]", String(args['filter[action]'])) if (args['filter[target_type]'] != null) query.append("filter[target_type]", String(args['filter[target_type]'])) if (args['filter[request_id]'] != null) query.append("filter[request_id]", String(args['filter[request_id]'])) if (args['filter[created_gte]'] != null) query.append("filter[created_gte]", String(args['filter[created_gte]'])) if (args['filter[created_lte]'] != null) query.append("filter[created_lte]", String(args['filter[created_lte]'])) if (args['page[size]'] != null) query.append("page[size]", String(args['page[size]'])) if (args['page[cursor]'] != null) query.append("page[cursor]", String(args['page[cursor]'])) if (JWTAuth !== undefined) headers.set('authorization', `Bearer ${JWTAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/audit-events/${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(paginatedListResponseAuditEventResource, await res.json()) };