20 stock_image: "3",
21 });
22 const response = await fetch(`/image?${params.toString()}`, {
23 method: "GET",
24 });
38 formData.append("type", imageType);
39
40 fetch("/upload", {
41 method: "POST",
42 body: formData,
1# Pagination Utilities
2
3## Fetch Paginated Data
4
5This 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](https://github.com/pomdtr/valtown-vscode).
6
7Usage:
9```ts
10const id = <vt user id>
11await fetchPaginatedData(`https://api.val.town/v1/users/${id}/vals`, {
12 headers: { Authorization: `Bearer ${Deno.env.get("valtown")}` },
13});
1import { fetch } from "https://esm.town/v/std/fetch?v=4";
2
3export async function fetchPaginatedData(url: string, init?: RequestInit, limit: number = 100) {
4 let u = new URL(url);
5 u.searchParams.set("limit", String(limit));
8 const data = [];
9 while (true) {
10 const resp = await fetch(url, init);
11 if (!resp.ok) {
12 throw new Error(`Fetch failed with status ${resp.status}`);
13 }
14 const body = await resp.json();
3
4const app = new Hono();
5export default app.fetch;
6
7app.get("/", async (c) => {
9 const app = await getApp();
10
11 return app.fetch(req);
12}
13
7 timeout: 5000,
8 });
9 let feedFile = await fetch("https://mlb.com/feeds/news/rss.xml");
10 let feedText = await feedFile.text();
11 let feedData = await parser.parseString(feedText);
137});
138
139export default app.fetch;
12 const start = performance.now();
13 try {
14 const res = await fetch(url);
15 end = performance.now();
16 status = res.status;
23 } catch (e) {
24 end = performance.now();
25 reason = `couldn't fetch: ${e}`;
26 ok = false;
27 console.log(`Website down (${url}): ${reason} (${end - start}ms)`);
12 const start = performance.now();
13 try {
14 const res = await fetch(url);
15 end = performance.now();
16 status = res.status;
23 } catch (e) {
24 end = performance.now();
25 reason = `couldn't fetch: ${e}`;
26 ok = false;
27 console.log(`Website down (${url}): ${reason} (${end - start}ms)`);