Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { AuthTokens } from "https://esm.town/v/easrng/denoAuthTokens";
let cache: [string, AuthTokens] | undefined;
export function moduleFetch(input: RequestInfo | URL, init?: RequestInit | undefined): Promise<Response> {
const req = new Request(input, init);
let authTokens = Deno.env.get("DENO_AUTH_TOKENS");
let valTownToken = Deno.env.get("valtown");
if (typeof authTokens === "undefined" && typeof valTownToken === "string") {
authTokens = `${valTownToken}@esm.town`;
}
if (typeof authTokens !== "undefined") {
const parsedAuthTokens = (cache && (cache[0] === authTokens))
? cache[1]
: ((cache = [authTokens, new AuthTokens(authTokens)])[1]);
const token = parsedAuthTokens.get(req.url);
if (token) {
req.headers.set("Authorization", token.toString());
}
}
return fetch(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!
February 2, 2024