Back to packages list

Vals using serve-router

Description from the NPM package:
tiny router library that routes for your web standard http server

Get A Website for Your Gists

Example: https://yieldray-gists.web.val.run
Usage: fork this val and replace with your github usename

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import ServeRouter from "https://esm.sh/serve-router@latest";
const app = ServeRouter();
const USERNAME = "yieldray"; // <--- Replace with your Github username
const gistList: Array<Gist> = await fetch(`https://api.github.com/users/${USERNAME}/gists`).then((res) => res.json());
app.get("/", (req) => {
return Response.json(
gistList.map((gist) => ({
id: gist.id,
description: gist.description,
url: new URL(`/${gist.id}`, req.url).toString(),
html_url: gist.html_url,
})),
);
});
app.get<{
id: string;
}>("/:id", (req, { params: { id } }) => {
const gist = gistList.find((gist) => gist.id === id) ?? gistList.find((gist) => gist.description === id);
if (!gist) return new Response("404 Gist Not Found", { status: 404 });
return Response.json(
Object.fromEntries(
Object.entries(gist.files).map(([fileName, file]) => [
fileName,
{
...file,
url: new URL(`/${gist.id}/${fileName}`, req.url).toString(),
},
]),
),
);
});
app.get<{
id: string;
file: string;
}>("/:id/:file", async (_req, { params: { id, file } }) => {
const gist = gistList.find((gist) => gist.id === id) ?? gistList.find((gist) => gist.description === id);
if (!gist) return new Response("404 Gist Not Found", { status: 404 });
const f = gist.files[file];
if (!f) return new Response("404 File Not Found", { status: 404 });
const res = await fetch(f.raw_url);
const headers = new Headers(res.headers);
// delete unwanted header
headers.delete("content-security-policy");
headers.delete("cross-origin-resource-policy");
for (const key of headers.keys()) {
if (key.startsWith("x-")) headers.delete(key);
}
if ([".ts", ".sh", ".cmd", ".bat", ".ps1"].some((end) => file.endsWith(end))) {
headers.set("Content-Type", "text/plain;charset=UTF-8");
} else {
headers.set("Content-Type", f.type);
}
return new Response(res.body, {
...res,
headers,
});
});
const handler = app.fetch
export default handler
interface Gist {
url: string;
forks_url: string;
commits_url: string;
id: string;
node_id: string;
git_pull_url: string;
git_push_url: string;
html_url: string;
files: Record<
string,
{
filename: string;
type: string;
language: string;
raw_url: string;
size: number;
}
>;
public: boolean;
created_at: Date;
updated_at: Date;
description: string;
comments: number;
user: null;
comments_url: string;
owner: null;
truncated: boolean;
}
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import ServeRouter from "https://esm.sh/serve-router@latest";
import { notems } from "https://esm.town/v/yieldray/notems";
import htm from "npm:htm";
import { micromark } from "npm:micromark";
import { gfm, gfmHtml } from "npm:micromark-extension-gfm";
import vhtml from "npm:vhtml";
const html = htm.bind(vhtml);
const markdown = (md) =>
micromark(md, {
extensions: [gfm()],
htmlExtensions: [gfmHtml()],
});
// components
const layout_html = ({ head, body }) =>
`<!DOCTYPE html>`
+ html`<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
${head}
</head>
<body class="markdown-body">
${body}
</body>
</html>`;
const layout_home = (msg = "") =>
layout_html({
head: html`<link rel="stylesheet" href="//unpkg.com/landsoul@latest" />
<style>
html {
padding: 1rem;
}
body {
max-width: 640px;
margin: auto;
}
@media screen and (max-width: 640px) {
body {
margin: 0 0.5rem;
}
}
label {
display: block;
padding: 0.5rem;
}
textarea {
display: block;
width: 100%;
min-height: 5rem;
box-sizing: border-box;
}
</style>`,
body: html`<header style="padding:1rem 0">
<h1>NoteBook</h1>
</header>
${msg}
<form action="/" method="post">
<label>
<span>ID:</span>
<input type="text" name="id" />
</label>
<label>
<span>Type:</span>
<select name="type">
<option value="text">Text</option>
<option value="markdown">Markdown</option>
<option value="html">HTML</option>
<option value="url">URL</option>
</select>
</label>
<label>
<span>Data:</span>
<textarea name="data"></textarea>
</label>
<input type="submit" value="Submit" />
</form>`,
});
const layout_markdown = (md = "") =>
layout_html({
head: html`<link rel="stylesheet" href="//unpkg.com/github-markdown-css@latest/github-markdown.min.css" />
<style>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 980px;
margin: 0 auto;
padding: 45px;
}
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
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
import ServeRouter from "https://esm.sh/serve-router@latest";
const app = ServeRouter();
app.get("/", () => new Response("Hello, world!"));
app.get("/headers", (req: Request) => Response.json(Object.fromEntries(req.headers.entries())));
app.get("/user/:name", (_req, { params }) => {
return new Response(`Hello, ${params.name}`);
});
app.post("/post", async (req) => {
const json = await req.json();
return Response.json(json);
});
app.route("/api")
// for /api
.get("", () => new Response("api"))
// for /api/one
.get("/one", () => new Response("one"))
// for /api/two
.get("/two", () => new Response("two"));
export const serve_router = app.fetch
1
Next