-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
52 lines (47 loc) · 1.63 KB
/
util.ts
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
import { ApiKey, Backend, apiErrPrefix, storage } from "~constants";
export function fmtTime(dateTime: string): string {
let parsedDateTime = new Date(Date.parse(dateTime));
return parsedDateTime.toDateString();
}
export function normalize(site: string): string {
return site.replace(/\/+$/, "");
}
export type ApiResp = {
error?: string,
data?: object[],
}
export async function isAuthenticated(): Promise<boolean> {
const apiKey = await storage.get(ApiKey);
console.log("API key: " + apiKey);
return apiKey !== undefined;
}
export async function apiRequest(
endpoint: { method: string, path: string },
body?: object
): Promise<ApiResp> {
try {
const apiKey = await storage.get(ApiKey);
let reqInit = {
method: endpoint.method,
headers: {
"X-Auth-Token": apiKey,
"Content-Type": "application/json",
}
}
if (body !== undefined) {
reqInit["body"] = JSON.stringify(body);
}
const response = await fetch(Backend + endpoint.path, reqInit);
const jsonBody = await response.json();
if ("error" in jsonBody) {
// Make the error message more descriptive.
jsonBody.error = apiErrPrefix[endpoint.path] + jsonBody.error;
} else if (!("data" in jsonBody)) {
return { "error": "API did not return a 'data' attribute." }
}
return jsonBody;
} catch (error) {
console.error("Error talking to backend API: " + error);
return { "error": "Error talking to backend API." } as unknown as Promise<ApiResp>
}
}