-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
160 lines (133 loc) · 3.52 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { components as SchemaComponents, paths as SchemaPath } from './schema';
type HttpStatus = 200 | 404 | 422;
const host = 'http://localhost:5001';
export type User = SchemaComponents['schemas']['User'];
export type Session = SchemaComponents['schemas']['Session'];
function isValidHttpStatus(status: number): status is HttpStatus {
return [200, 404, 422].includes(status);
}
type ContentType = {
content: {
'application/json': unknown;
};
};
type GetParametersType = {
get: {
parameters: {
path: object;
};
};
};
type GetResponseType = {
get: {
responses: {
[status in HttpStatus]: ContentType;
};
};
};
type PostParametersType = {
post: {
parameters: {
path: object;
};
};
};
type PostResponseType = {
post: {
responses: {
[status in HttpStatus]: ContentType;
};
};
};
type PostRequestType = {
post: {
requestBody: ContentType;
};
};
type FetchParams<URL extends keyof SchemaPath> = SchemaPath[URL] extends GetParametersType
? SchemaPath[URL]['get']['parameters']['path']
: SchemaPath[URL] extends PostParametersType
? SchemaPath[URL]['post']['parameters']['path']
: never;
type FetchRequest<URL extends keyof SchemaPath> = SchemaPath[URL] extends PostRequestType
? SchemaPath[URL]['post']['requestBody']['content']['application/json']
: never;
type FetchResult<
URL extends keyof SchemaPath,
Status extends HttpStatus,
> = SchemaPath[URL] extends GetResponseType
? SchemaPath[URL]['get']['responses'][Status]['content']['application/json']
: SchemaPath[URL] extends PostResponseType
? SchemaPath[URL]['post']['responses'][Status]['content']['application/json']
: never;
type FetchResponse<URL extends keyof SchemaPath> =
| {
status: 200;
data: FetchResult<URL, 200>;
}
| {
status: 404;
data: FetchResult<URL, 404>;
}
| {
status: 422;
data: FetchResult<URL, 422>;
};
async function get<URL extends keyof SchemaPath>(
_url: URL,
request: SchemaPath[URL] extends GetParametersType ? { params: FetchParams<URL> } : undefined,
): Promise<FetchResponse<URL>> {
let url: string = _url;
if (request?.params) {
Object.entries(request?.params).forEach(([key, value]) => {
url = url.replace(`{${key}}`, value.toString());
});
}
const res = await fetch(`${host}${url}`, {
method: 'get',
});
if (!isValidHttpStatus(res.status)) {
throw new Error('Server error');
}
return { status: res.status, data: await res.json() };
}
async function post<URL extends keyof SchemaPath>(
_url: URL,
request: SchemaPath[URL] extends PostParametersType
? SchemaPath[URL] extends PostRequestType
? {
params: FetchParams<URL>;
payload: FetchRequest<URL>;
}
: {
params: FetchParams<URL>;
}
: SchemaPath[URL] extends PostRequestType
? {
payload: FetchRequest<URL>;
}
: never,
): Promise<FetchResponse<URL>> {
let url: string = _url;
if ('params' in request) {
Object.entries(request.params).forEach(([key, value]) => {
url = url.replace(`{${key}}`, value.toString());
});
}
const res = await fetch(`${host}${url}`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
},
body: 'payload' in request ? JSON.stringify(request.payload) : undefined,
});
if (!isValidHttpStatus(res.status)) {
throw new Error('Server error');
}
return { status: res.status, data: await res.json() };
}
const api = {
get,
post,
};
export default api;