Public
Script
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Todo

  • extract params from request paths using urlpatterns
  • add a Form component
  • add support for Layouts
  • support other types url in router ("https://val.town/v/user/val" or "owner/val")
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
36
37
38
39
40
41
42
43
44
45
46
/** @jsxImportSource https://esm.sh/react */
import { renderToString } from "https://esm.sh/react-dom@18.2.0/server";
import { html } from "https://esm.town/v/stevekrouse/html";
export type PageProps<T = {}> = T & {
route: string;
params: Record<string, string>;
};
type Context = PageProps & {
render: (props) => Response | Promise<Response>;
};
export type Action = (req: Request, ctx: Context) => Response | Promise<Response>;
export function handler(Component, action?: Action) {
return async (c) => {
if (action) {
return action(c.req.raw, {
params: c.req.param(),
route: c.req.routePath,
render(props) {
return html(renderToString(
<Component
{...{
params: c.req.param(),
route: c.req.routePath,
...props,
}}
/>,
));
},
});
}
return html(renderToString(
<Component
{...{
params: c.req.param(),
route: c.req.routePath,
}}
/>,
));
};
}
February 27, 2024