-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonbin.js
64 lines (62 loc) · 2.12 KB
/
jsonbin.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
var key = "$2b$10$mSiXNBsd6/8WdlalxQTSNusLxbzLtY74juvihgt5oWROjrHGs19VW"
let req = new XMLHttpRequest();
class Bin {
/**
*
* @param {String} binID
*/
constructor(binID, version = 1) {
this.binID = binID + "/" + version;
}
read(callback) {
try {
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
callback(JSON.parse(req.responseText));
}
};
req.open("GET", "https://api.jsonbin.io/b/" + this.binID, true);
req.setRequestHeader("secret-key", key);
req.send();
} catch (e) { log(e) }
}
create(query, callback) {
try {
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
callback(JSON.parse(req.responseText));
}
};
req.open("POST", "https://api.jsonbin.io/b/" + this.binID, true);
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("secret-key", key);
req.send(query);
} catch (e) { log(e) }
}
update(query, callback) {
try {
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
this.version += 1;
callback(JSON.parse(req.responseText));
}
};
req.open("PUT", "https://api.jsonbin.io/b/" + this.binID, true);
req.setRequestHeader("Content-Type", "application/json");
req.setRequestHeader("secret-key", key);
req.send(query);
} catch (e) { log(e) }
}
delete() {
try {
req.onreadystatechange = () => {
if (req.readyState == XMLHttpRequest.DONE) {
callback(JSON.parse(req.responseText));
}
};
req.open("DELETE", "https://api.jsonbin.io/b/" + this.binID, true);
req.setRequestHeader("secret-key", key);
req.send();
} catch (e) { log(e) }
}
}