import {z} from 'zod' export type AuditModule = 'collectors' | 'tokens' | 'fleets' | 'rollouts' | 'configs' | 'settings'; export type GetApiApiOrgIdFleetAuditArgs = {org_id: string, from?: number | undefined, to?: number | undefined, offset?: number | undefined, limit?: number | undefined, fleet?: string | undefined, module?: AuditModule | undefined, q?: string | undefined}; export type GetApiApiOrgIdFleetAuditOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const auditEvent = z.object({action: z.string(), actor: z.string(), at: z.number().int(), id: z.string(), outcome: z.string(), target: z.string()}); export const auditList = z.object({audit_disabled: z.boolean().optional(), items: z.array(auditEvent), total: 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 }; /** * One page of the fleet audit trail, newest first, from the last 30 days or a period * * GET /api/{org_id}/fleet/audit */ export const getApiApiOrgIdFleetAudit = async (args: GetApiApiOrgIdFleetAuditArgs, options: GetApiApiOrgIdFleetAuditOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.fleet != null) query.append("fleet", String(args.fleet)) if (args.module != null) query.append("module", String(args.module)) if (args.q != null) query.append("q", String(args.q)) if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/fleet/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(auditList, await res.json()) };