Readme

Fetch Paginated Data

This val exports a function that loops through paginated API responses and returns the combined data as an array. It expects pagination with next and there to be a data property in the API response. This conforms to the Val Town API, so this function is useful when fetching paginated Val Town API responses for creating custom folders in pomdtr's vscode extension.

Usage:

const id = <vt user id> await fetchPaginatedData(`https://api.val.town/v1/users/${id}/vals`, { headers: { Authorization: `Bearer ${Deno.env.get("valtown")}` }, });

For demo usage in the context of the vscode extension see this val.

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;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v49
January 8, 2024