-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsame-as-usual.js
57 lines (54 loc) · 1.14 KB
/
same-as-usual.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
55
56
57
function $http (baseUrl){
let headers = {}
function wrapperAjax ({url, method, data}){
const setup = {}
setup.method = method;
setup.headers = headers;
if(data){
setup.body = typeof(data) !== "string" ? JSON.stringify(data): data;
}
return fetch(`${baseUrl+ url}`, setup)
}
function fallbackStrategy (prom, cb) {
if(cb && typeof(cb) === "function") {
prom
.then(response => response.json())
.then(data => cb(false, data))
.catch(cb)
} else {
return prom.then(response => response.json())
}
}
return {
headers(obj){
const _self = this;
this.headers = obj;
return _self;
},
method(verb, url, data, cb){
return fallbackStrategy(wrapperAjax({
url, method:verb, data
}), cb)
},
get(url, cb){
return fallbackStrategy(wrapperAjax({
url, method:"GET"
}), cb)
},
post(url, data, cb){
return fallbackStrategy(wrapperAjax({
url, method:"POST", data
}), cb)
},
put(url, data, cb){
return fallbackStrategy(wrapperAjax({
url, method:"PUT", data
}), cb)
},
delete(url, cb){
return fallbackStrategy(wrapperAjax({
url, method:"DELETE"
}), cb)
}
}
}