Readme

Devtools shortcuts

Add useful shortcuts to a val website.

Usage

  • /_edit -> go to the val editor
  • /_raw -> view val source
  • /_logs -> view val logs

Example

import { devtools } from "https://esm.town/v/pomdtr/devtools"; export default devtools((_req: Request) => { return new Response("hello world"); });
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);
};
}
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!
v12
June 4, 2024