This repository has been archived by the owner on May 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
request-helper.ts
68 lines (62 loc) · 1.78 KB
/
request-helper.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
import * as _ from 'lodash';
let defaultHeaders = {
'Content-Type': 'application/json;charset=utf-8',
'Cache-Control': 'no-cache'
};
// use fetch for all API calls
const doFetch = (url: string, headers: any = {}): Promise<any> => {
const allHeaders = _.assign(defaultHeaders, headers);
return fetch(url, {
method: 'GET',
mode: 'cors',
headers: allHeaders
})
.then(response => {
return response.json();
});
};
const doPost = function <T>(url: string, data: T, headers: any = {}): Promise<any> {
const allHeaders = _.assign(defaultHeaders, headers);
const request = {
method: 'POST',
mode: 'cors',
redirect: 'follow',
body: JSON.stringify(data),
headers: allHeaders
};
return fetch(url, request as any).then(response => {
return response.json();
});
};
const doPut = function <T>(url: string, data: T, headers: any = {}): Promise<any> {
const allHeaders = _.assign(defaultHeaders, headers);
const request = {
method: 'PUT',
mode: 'cors',
redirect: 'follow',
body: JSON.stringify(data),
headers: allHeaders
};
return fetch(url, request as any).then(response => {
return response.json();
});
};
const doDelete = function <T>(url: string, headers: any = {}): Promise<any> {
const allHeaders = _.assign(defaultHeaders, headers);
const request = {
method: 'DELETE',
mode: 'cors',
redirect: 'follow',
headers: allHeaders
};
return fetch(url, request as any).then((response: Response) => {
return response.text();
});
};
const RequestHelper = {
get: doFetch,
post: doPost,
put: doPut,
delete: doDelete
}
export { RequestHelper }