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 { refs } from "https://esm.town/v/stevekrouse/refs?v=11";
const webValRunHost = ".web.val.run";
export function inferRequestVal(req: Request) {
// First, try to infer val from the request URL
try {
const url = new URL(req.url);
if (url.hostname.endsWith(webValRunHost)) {
// Parse from URL
const urlVal = url.hostname
.substring(0, url.hostname.length - webValRunHost.length)
.split(".")
.at(-1)
?.split("-");
if (urlVal && urlVal.length === 2 && urlVal[0] && urlVal[1]) {
return {
handle: urlVal[0],
name: urlVal[1],
};
}
}
} catch {}
return undefined;
}
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!
v2
February 26, 2024