forked from suvelocity/network-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.js
66 lines (61 loc) · 1.95 KB
/
ajax.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
58
59
60
61
62
63
64
65
66
const COLLECTION_ID = "5f1d6d37c58dc34bf5dafba4";
const API_URL = "https://api.jsonbin.io/v3/b"; // todo: use API v2
window.API_KEY = localStorage.getItem("API_KEY");
// todo later: move common functionality from all CRUD functions to a single function
function create(binName, binData) { // todo: use await
const options = {
method: "POST", // todo: use instance methods
headers: { // todo: use axios default headers
"Content-Type": "application/json; charset=utf-8",
"X-COLLECTION-ID": COLLECTION_ID,
"X-Bin-Name": binName,
"X-Master-Key": API_KEY
},
data: binData,
url: API_URL // todo: use axios default base_url
}
return axios(options)
.then(res => res.data) // todo: use axios interceptor to return the data
.catch(e => Promise.reject(`${e.message} : '${e.response.data.message}'`)); // todo: use axios interceptor to return create error message
}
function read(binId) {
const options = {
method: "GET",
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-COLLECTION-ID": COLLECTION_ID,
"X-Master-Key": API_KEY
},
url: `${API_URL}/${binId}`
}
return axios(options)
.then(res => res.data)
.catch(e => Promise.reject(`${e.message} : '${e.response.data.message}'`));
}
function update(binId, binData) {
const options = {
method: "PUT",
headers: {
"Content-Type": "application/json; charset=utf-8",
"X-COLLECTION-ID": COLLECTION_ID,
"X-Master-Key": API_KEY
},
data: binData,
url: `${API_URL}/${binId}`
}
return axios(options)
.then(res => res.data)
.catch(e => Promise.reject(`${e.message} : '${e.response.data.message}'`));
}
function destroy(binId) {
const options = {
method: "DELETE",
headers: {
"X-Master-Key": API_KEY
},
url: `${API_URL}/${binId}`
}
return axios(options)
.then(res => res.data)
.catch(e => Promise.reject(`${e.message} : '${e.response.data.message}'`));
}