1 | import 'isomorphic-fetch';
|
2 |
|
3 | type HttpMethod = 'GET' | 'PUT' | 'POST' | 'PATCH' | 'DELETE';
|
4 | export interface IResponse<T> {
|
5 | ok: boolean;
|
6 | status: number;
|
7 | statusText?: string;
|
8 | type: string;
|
9 | url: string;
|
10 | data?: T;
|
11 | }
|
12 |
|
13 | export interface IHttpHeaders {
|
14 | [id: string]: string;
|
15 | }
|
16 |
|
17 | async function request<T>(
|
18 | method: HttpMethod,
|
19 | requestUrl: string,
|
20 | data?: object,
|
21 | headers?: IHttpHeaders,
|
22 | ): Promise<IResponse<T>> {
|
23 | const payload = {
|
24 | method,
|
25 | body: data ? JSON.stringify(data) : undefined,
|
26 | headers: {},
|
27 | };
|
28 | if (data) {
|
29 | Object.assign(payload.headers, {
|
30 | Accept: 'application/json',
|
31 | 'Content-Type': 'application/json',
|
32 | });
|
33 | }
|
34 | if (headers) {
|
35 | Object.assign(payload.headers, headers);
|
36 | }
|
37 |
|
38 | const response = await fetch(requestUrl, payload);
|
39 | const { ok, status, statusText, type, url } = response;
|
40 | const result = { ok, status, statusText, type, url, data: undefined };
|
41 | if (ok) {
|
42 |
|
43 | const contentType = (response.headers as any)._headers['content-type'];
|
44 | if (contentType[0].startsWith('application/json')) {
|
45 | result.data = await response.json();
|
46 | }
|
47 | }
|
48 | return result;
|
49 | }
|
50 |
|
51 | function get<T>(url: string, headers?: IHttpHeaders): Promise<IResponse<T>> {
|
52 | return request<T>('GET', url, undefined, headers);
|
53 | }
|
54 |
|
55 | function put<T>(
|
56 | url: string,
|
57 | data?: object,
|
58 | headers?: IHttpHeaders,
|
59 | ): Promise<IResponse<T>> {
|
60 | return request<T>('PUT', url, data, headers);
|
61 | }
|
62 |
|
63 | function post<T>(
|
64 | url: string,
|
65 | data?: object,
|
66 | headers?: IHttpHeaders,
|
67 | ): Promise<IResponse<T>> {
|
68 | return request<T>('POST', url, data, headers);
|
69 | }
|
70 |
|
71 | function patch<T>(
|
72 | url: string,
|
73 | data?: object,
|
74 | headers?: IHttpHeaders,
|
75 | ): Promise<IResponse<T>> {
|
76 | return request<T>('PATCH', url, data, headers);
|
77 | }
|
78 |
|
79 | function del<T>(url: string, headers?: IHttpHeaders): Promise<IResponse<T>> {
|
80 | return request<T>('DELETE', url, undefined, headers);
|
81 | }
|
82 |
|
83 | function headers(httpHeaders: IHttpHeaders) {
|
84 | return {
|
85 | get: <T>(url: string): Promise<IResponse<T>> => get<T>(url, httpHeaders),
|
86 | put: <T>(url: string, data?: object): Promise<IResponse<T>> =>
|
87 | put<T>(url, data, httpHeaders),
|
88 | post: <T>(url: string, data?: object): Promise<IResponse<T>> =>
|
89 | post<T>(url, data, httpHeaders),
|
90 | patch: <T>(url: string, data?: object): Promise<IResponse<T>> =>
|
91 | patch<T>(url, data, httpHeaders),
|
92 | delete: <T>(url: string): Promise<IResponse<T>> => del<T>(url, httpHeaders),
|
93 | headers: (append: IHttpHeaders) => headers({ ...httpHeaders, ...append }),
|
94 | };
|
95 | }
|
96 |
|
97 | export { get, put, post, patch, del as delete, headers };
|
98 | export default { get, put, post, patch, delete: del, headers };
|