-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
115 lines (99 loc) · 2.94 KB
/
api.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const PREFIX = '';
namespace api {
/**
Might want to look into the `api.ts` file to see the docstring for this struct
*/
export interface Age {
/**
Even supports docstring on fields and optional fields
*/
age?: AgeInner;
}
export interface AgeInner {
/**
This gets converted to a `string` on the TypeScript side
because `numbers` there cannot be greater than 64 bits
*/
age: string;
}
export interface Hello<T, S> {
name: S;
vec: T[];
}
export interface Huh<T> {
huh: T;
}
export interface QueryOptions {
id: number;
query: string;
}
export enum Alphabet {
A = "A",
B = "B",
C = "C",
}
/**
An example how an api error type could look like
*/
export enum Error {
/**
Normal 404 error
*/
NotFound = "NotFound",
/**
Internally something really bad happened
*/
InternalServerError = "InternalServerError",
}
export type Result<T> = T | Error;
export type S = string;
async function fetch_api(endpoint: string, options: RequestInit): Promise<any> {
const response = await fetch(endpoint, {
headers: {
"Content-Type": "application/json",
...options.headers,
},
...options,
});
if (response.headers.get('Content-Length') === '0') {
return;
} else {
return response.json();
}
}
function query_str(params: Record<string, any>): string {
if (params) {
let data: Record<string, string> = {};
for (let key in params) {
if (params[key] != null) data[key] = params[key].toString();
}
return '?' + new URLSearchParams(data).toString();
}
return '';
}
/**
Docstrings for functions are also supported
*/
export async function add_root(path: number, data: Result<Hello<Hello<Huh<Huh<Hello<Age, string>>>, string>, string>>): Promise<Result<string>> {
return fetch_api(`${PREFIX}/${encodeURIComponent(path)}`, {
method: "POST",
body: JSON.stringify(data)
});
}
export async function fetch_other(query: QueryOptions): Promise<string> {
return fetch_api(`${PREFIX}/other${query_str(query)}`, {
method: "GET",
});
}
export async function fetch_root(queryMap: Record<string, string>, path: number): Promise<string> {
return fetch_api(`${PREFIX}/${encodeURIComponent(path)}?${new URLSearchParams(queryMap).toString()}`, {
method: "GET",
});
}
export async function get_alphabet(pathTuple: [Alphabet, S]): Promise<[Alphabet, S]> {
return fetch_api(`${PREFIX}/char/${encodeURIComponent(pathTuple[0])}/metadata/${encodeURIComponent(pathTuple[1])}`, {
method: "GET",
});
}
}
export default api;