-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-api.js
54 lines (41 loc) · 1.54 KB
/
fetch-api.js
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
!function(exports){
var types = {
json: 'application/json', form: 'application/x-www-form-urlencoded',
};
function fetchWithBody(method, input, body, type, creds, init){
if (body == undefined) throw 'body is required';
if (type == undefined) throw 'type is required';
init = init || {};
if (creds === true)
init.credentials = 'same-origin';
else if (creds != undefined)
init.credentials = creds;
init.method = method;
init.body = body
init.headers = init.headers || {};
init.headers['Content-Type'] = types[type] || type;
if (type == 'json')
init.headers['Accept'] = init.headers['Accept'] || types[type];
return fetch(input, init);
}
exports.Fetch = {
fetch:function(input, init){
return fetch(input, init);
},
get:function(input, creds, init){
init = init || {};
init.method = 'GET';
if (creds === true)
init.credentials = 'same-origin';
else if (creds != undefined)
init.credentials = creds;
return fetch(input, init);
},
post:function(input, body, type, creds, init){
return fetchWithBody('POST', input, body, type, creds, init);
},
patch:function(input, body, type, creds, init){
return fetchWithBody('PATCH', input, body, type, creds, init);
},
};
}(typeof module == 'undefined' ? window : module.exports);