Fetch all pages with backoff (TS)
import { setTimeout as delay } from 'node:timers/promises';
import { fetch } from 'undici';
async function fetchAll(url, token) {
let next = url;
const items = [];
while (next) {
const res = await fetch(next, { headers: { Authorization: `Bearer ${token}` } });
if (!res.ok) {
if (res.status >= 500) { await delay(1000); continue; }
throw new Error(`HTTP ${res.status}`);
}
const data = await res.json();
items.push(...data.items);
next = data.next;
}
return items;
}