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

This val implements a simple key/value database with GitHub, in a CRUD way.

The key is a file path within a repository. The value is the file content.

The val needs a GitHub token to access the GitHub API, the account name and the repository name.

Also, the val needs GHDB_API_KEY variable. This value defines the exptected value of the GHDB_API_KEY header to allow access to the endpoints.

Endpoints:

  • GET /data/path/to/file.ext - read file
  • GET /raw/path/to/file.ext - read file from GitHub CDN (10 times faster)
  • DELETE /data/path/to/file.ext - delete file
  • POST /data/path/to/file.ext - create file
  • PUT /data/path/to/file.ext - update/commit file
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 { Buffer } from "node:buffer";
import { env } from "node:process";
import consola from "npm:consola";
import { Hono } from "npm:hono";
import { HTTPException } from "npm:hono/http-exception";
consola.options.formatOptions.columns = 0;
consola.options.formatOptions.compact = false;
export type Data = {
content: string | Uint8Array;
sha: string;
size: number;
type: string;
encoding: string;
name: string;
download_url: string;
url: string;
git_url: string;
html_url: string;
};
const GH = `https://api.github.com/repos`;
const GH_STORAGE = "https://raw.githubusercontent.com";
const now = () => new Date().toISOString().replace("T", " ").replace("Z", "");
export class GitHubAPI {
#token: string | undefined;
#contents: string;
#raw: string;
constructor() {
const { GITHUB_TOKEN, GITHUB_ACCOUNT, GITHUB_REPO } = env;
this.#token = GITHUB_TOKEN;
this.#contents = `${GH}/${GITHUB_ACCOUNT}/${GITHUB_REPO}/contents`;
this.#raw = `${GH_STORAGE}/${GITHUB_ACCOUNT}/${GITHUB_REPO}/main`;
}
headers = () => ({
Authorization: `token ${this.#token}`,
Accept: "application/vnd.github.v3+json",
});
error(message: string, cause: string, response?: Response): undefined {
if (response) {
const { ok, status, statusText } = response;
consola.error(message, {
cause,
ok,
status,
statusText,
});
} else {
consola.error(message, { cause });
}
return undefined;
}
async exist(path: string): Promise<boolean> {
try {
const url = `${this.#contents}/${path}`;
const response = await fetch(url, { headers: this.headers() });
if (response.ok) return true;
this.error("exist", url, response);
} catch (e) {
this.error("exist", e.toString());
}
return false;
}
async get(path: string, binary?: boolean): Promise<Data | undefined> {
try {
const url = `${this.#contents}/${path}`;
const response = await fetch(url, { headers: this.headers() });
if (!response.ok) {
return this.error("get: not found", url, response);
}
const data = (await response.json()) as Data;
const bytes = Buffer.from(data.content as string, "base64");
data.content = binary ? bytes : new TextDecoder().decode(bytes);
return data;
} catch (e) {
return this.error("get", e.toString());
}
}
async raw(
path: string,
binary?: boolean,
): Promise<ArrayBuffer | string | undefined> {
try {
const url = `${this.#raw}/${path}`;
const response = await fetch(url, { headers: this.headers() });
if (!response.ok) return this.error("raw", url, response);
const data = binary
? await response.arrayBuffer()
: await response.text();
return data;
begoon-ghdb.web.val.run
September 19, 2024