yieldray-gists.web.val.run
Readme

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;
}
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!
v6
May 10, 2024