1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { fetch } from "https://esm.town/v/std/fetch?v=4";
export async function fetchPaginatedData(url: string, init?: RequestInit, limit: number = 100) {
let u = new URL(url);
u.searchParams.set("limit", String(limit));
url = u.toString();
const data = [];
while (true) {
const resp = await fetch(url, init);
if (!resp.ok) {
throw new Error(`Fetch failed with status ${resp.status}`);
}
const body = await resp.json();
data.push(...body.data);
if (!body.links?.next) {
break;
}
url = body.links?.next;
}
return data;
}