Public
Script
March 14, 2024
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
26
27
import { API_URL } from "https://esm.town/v/std/API_URL";
async function proxiedFetch(req: Request): Promise<Response> {
let query = new URLSearchParams({
url: req.url,
});
const headers = new Headers(req.headers);
headers.set("X-Valtown-Authorization", `Bearer ${Deno.env.get("valtown")}`);
return fetch(`${API_URL}/v1/fetch?${query}`, {
...req,
headers,
});
}
import isNetworkError from "npm:is-network-error@1.0.1";
export const resilientFetch: typeof fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
const req = new Request(input, init);
try {
const res = await fetch(req);
if (res.ok) {
return res;
}
} catch (e) {
if (!isNetworkError(e)) {
throw e;
}
}
return await proxiedFetch(req);
};
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!
March 14, 2024