-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.ts
98 lines (84 loc) · 2.85 KB
/
fetch.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
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
import { getStorage } from "./util.js";
import validate, { Schema, SchemaToType } from "./validate.js";
/** The result of a fetch operation. */
type FetchResult<T extends Schema | undefined> =
| (T extends undefined ? { ok: true } : { ok: true; data: SchemaToType<T> })
| { ok: false; error: string };
/**
* Fetches some resources from the server.
* @param route The route to fetch.
* @param method The method to use.
* @returns A promise resolving to data returned by the server. If an error occurs, `null` is returned.
*/
export default async function fetch(
route: string,
method: "GET" | "POST"
): Promise<FetchResult<undefined>>;
/**
* Fetches some resources from the server.
* @param route The route to fetch.
* @param method The method to use.
* @param schema The schema to match data against. If none is provided, no data is returned.
* @param body The body to send to the server. May be a JSON object.
* @returns A promise resolving to data returned by the server. If an error occurs, `null` is returned.
*/
export default async function fetch<T extends Schema>(
route: string,
method: "GET" | "POST",
schema: T,
body?: unknown
): Promise<FetchResult<T>>;
export default async function fetch<T extends Schema | undefined>(
route: string,
method: "GET" | "POST",
schema?: T,
body?: unknown
): Promise<FetchResult<T>> {
try {
let headers: Record<string, string> = {};
let request: RequestInit = { method };
let token = getStorage("options:authToken");
if (token) headers["x-auth"] = token;
if (body) {
let json = JSON.stringify(body);
headers["content-type"] = "application/json";
headers["content-length"] = "" + json.length;
request.body = json;
}
request.headers = headers;
let response = await window.fetch(route, request);
let respBody: unknown;
try {
try {
respBody = await response.clone().json();
} catch {
respBody = await response.clone().text();
}
} catch {
respBody = null;
}
if (typeof respBody == "object" && respBody !== null) {
if ((respBody as any)?.error) {
return {
ok: false,
error: String((respBody as any)?.message || (respBody as any)?.error),
};
}
}
// throws after getting body so that we can use fastify's json message if possible
if (!response.ok)
throw new Error(`${(respBody as any).message ?? respBody}`); // prettier-ignore
if (!schema) return { ok: true } as FetchResult<T>;
if (validate(respBody, schema))
return { ok: true, data: respBody } as FetchResult<T>;
throw new Error("Response doesn't match schema");
} catch (err: any) {
return { ok: false, error: String(err?.message || err) };
}
}
declare global {
interface StorageItems {
/** The user's authetication token. */
"options:authToken"?: string;
}
}