-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
46 lines (39 loc) · 1.23 KB
/
Copy pathdatabase.js
File metadata and controls
46 lines (39 loc) · 1.23 KB
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
const FIREBASE_URL = Deno.env.get("FIREBASE_URL");
const FIREBASE_SECRET = Deno.env.get("FIREBASE_SECRET");
export class Database {
static async get(key) {
return await fetch(FIREBASE_URL + encodeURIComponent(key) + "/.json?auth=" + FIREBASE_SECRET)
.then(e => e.text())
.then(value => {
if (!value) return null;
try { value = JSON.parse(value); }
catch { return null; }
if (value === undefined || value === null || value.error !== undefined) return null;
return value;
});
}
static async set(key, value) {
await fetch(FIREBASE_URL + encodeURIComponent(key) + "/.json?auth=" + FIREBASE_SECRET, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(value),
});
}
static async delete(key) {
await fetch(
FIREBASE_URL + encodeURIComponent(key) + "/.json?auth=" + FIREBASE_SECRET,
{ method: "DELETE" }
);
}
static async dictionary() {
return await fetch(`${FIREBASE_URL}/.json?auth=${FIREBASE_SECRET}`)
.then(e => e.text())
.then(value => {
if (!value) return null;
try { value = JSON.parse(value); }
catch { return null; }
if (value === undefined || value === null || value.error !== undefined) return null;
return value;
});
}
}