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
28
29
30
31
32
33
34
35
import { inferRequestVal } from "https://esm.town/v/andreterron/inferRequestVal?v=2";
import { api } from "https://esm.town/v/pomdtr/api";
export function devtools(handler: (req: Request) => Response | Promise<Response>, options?: {
val?: {
handle: string;
name: string;
};
}) {
return async (req: Request) => {
const { pathname } = new URL(req.url);
let val = options?.val;
if (!options?.val) {
val = inferRequestVal(req);
if (!val) {
return new Response("Could not infer val from request", { status: 400 });
}
}
if (pathname === "/_edit") {
return Response.redirect(`https://www.val.town/v/${val?.handle}/${val?.name}`);
}
if (pathname === "/_logs") {
const { id } = await api(`/v1/alias/${val.handle}/${val.name}`);
return Response.redirect(`https://www.val.town/settings/evaluations?val=${id}`);
}
if (pathname === "/_raw") {
return Response.redirect(`https://esm.town/v/${val?.handle}/${val?.name}`);
}
return handler(req);
};
}