import {z} from 'zod' export type GetApiAuditEventsArgs = {'Amperity-Tenant': string, 'Api-Version': string, api_version?: string | undefined, with_total?: boolean | undefined, limit?: number | undefined, next_token?: string | undefined, happened_from?: string | undefined, happened_to?: string | undefined}; export type GetApiAuditEventsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const auditEvent = z.object({event_id: z.string(), event_type: z.string(), happened_at: z.string(), object_id: z.string(), object_name: z.string(), origin_ip: z.string(), principal_email: z.string(), principal_id: z.string(), principal_name: z.string(), session_id: z.string(), tenant: z.string(), tenant_family: z.string(), user_agent: z.string()}); export const auditEventListEnvelope = z.object({data: z.array(auditEvent), next_token: z.string().optional(), total: z.number().int().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 /audit-events */ export const getApiAuditEvents = async (args: GetApiAuditEventsArgs, options?: GetApiAuditEventsOptions) => { const {baseUrl = "https://app.amperity.com/api", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['Amperity-Tenant'] != null) headers.set("Amperity-Tenant", String(args['Amperity-Tenant'])) if (args['Api-Version'] != null) headers.set("Api-Version", String(args['Api-Version'])) if (args.api_version != null) query.append("api_version", String(args.api_version)) if (args.with_total != null) query.append("with_total", String(args.with_total)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.next_token != null) query.append("next_token", String(args.next_token)) if (args.happened_from != null) query.append("happened_from", String(args.happened_from)) if (args.happened_to != null) query.append("happened_to", String(args.happened_to)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(auditEventListEnvelope, await res.json()) };