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:

Create valconst 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;
}
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.