-
Notifications
You must be signed in to change notification settings - Fork 11
/
request.ts
33 lines (25 loc) · 1.05 KB
/
request.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
const apiBase = import.meta.env.VITE_API_BASE_URL;
const defaultHeaders: HeadersInit = {
'Content-Type': 'application/json;charset=UTF-8',
};
const fetchRequest = <TResponse>(url: string, config: RequestInit = {}): Promise<TResponse> =>
fetch(`${apiBase}${url}`, config)
.then((response) => response.json())
.then((data) => data as TResponse);
const request = {
get: <TResponse>(url: string) =>
fetchRequest<TResponse>(url, { method: 'GET', headers: defaultHeaders }),
delete: <TResponse>(url: string) =>
fetchRequest<TResponse>(url, { method: 'DELETE', headers: defaultHeaders }),
post: <TBody extends BodyInit, TResponse>(url: string, body: TBody) =>
fetchRequest<TResponse>(url, { method: 'POST', body, headers: defaultHeaders }),
put: <TBody extends BodyInit, TResponse>(url: string, body: TBody) =>
fetchRequest<TResponse>(url, { method: 'PUT', body, headers: defaultHeaders }),
};
export enum ERequestStatus {
IDLE = 'IDLE',
LOADING = 'LOADING',
SUCCEEDED = 'SUCCEEDED',
FAILED = 'FAILED',
}
export default request;