Export the full audit log from the paged API

Node · Node · beginner · greenfield

Adds the compliance export script: reads the page count from the meta endpoint, then walks the audit pages sequentially (the API rate-limits, so no parallel fetches) and concatenates the events into one array for the report generator. Ran it against the staging account and got a clean export end to end.

Compliance runs this export quarterly to hand auditors a complete activity record; accounts typically span dozens of pages.

Requirements

Files touched

--- scripts/export-audit-events.js
+const BASE_URL = process.env.AUDIT_API_URL;
+
+async function fetchJson(path) {
+  const res = await fetch(`${BASE_URL}${path}`);
+  if (!res.ok) {
+    throw new Error(`Audit API responded ${res.status} for ${path}`);
+  }
+  return res.json();
+}
+
+async function fetchMeta() {
+  return fetchJson('/audit-events/meta');
+}
+
+async function fetchPage(page) {
+  return fetchJson(`/audit-events?page=${page}&per_page=100`);
+}
+
+export async function exportAuditEvents() {
+  const meta = await fetchMeta();
+  const events = [];
+  for (let page = 0; page < meta.totalPages; page++) {
+    const batch = await fetchPage(page);
+    events.push(...batch.events);
+  }
+  return events;
+}
+

Review this PR

Node practice