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
|
import { buildPath } from '@/lib/url';
export interface ErrorResponse {
error: {
status: number;
message: string;
code?: string;
};
}
export interface FetchResponse {
ok: boolean;
status: number;
data?: any;
error?: ErrorResponse;
}
export async function request(
method: string,
url: string,
body?: string,
headers: object = {},
): Promise<FetchResponse> {
return fetch(url, {
method,
cache: 'no-cache',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
...headers,
},
body,
}).then(async res => {
const data = await res.json();
return {
ok: res.ok,
status: res.status,
data,
};
});
}
export async function httpGet(path: string, params: object = {}, headers: object = {}) {
return request('GET', buildPath(path, params), undefined, headers);
}
export async function httpDelete(path: string, params: object = {}, headers: object = {}) {
return request('DELETE', buildPath(path, params), undefined, headers);
}
export async function httpPost(path: string, params: object = {}, headers: object = {}) {
return request('POST', path, JSON.stringify(params), headers);
}
export async function httpPut(path: string, params: object = {}, headers: object = {}) {
return request('PUT', path, JSON.stringify(params), headers);
}
|