-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (55 loc) · 1.58 KB
/
index.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
var Promise = require("promise");
var querystring = require("query-string");
var _get = "GET";
module.exports = json_xhr;
["get", "post", "put", "delete"].forEach(function(verb) {
json_xhr[verb] = json_xhr.bind(null, verb);
});
function json_xhr(verb, url, data) {
if (arguments.length === 2) {
if ((typeof url) === "string") return json_xhr(verb, url, {})
else return json_xhr(_get, verb, url);
} else if (arguments.length === 1) {
return json_xhr(_get, verb, {});
} else if (arguments.length === 0)
return Promise.reject(new Error("Not Enough Arguments"))
verb = verb.toUpperCase();
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
var to_send;
var qs = "";
var is_get = (verb === _get);
if (is_get) qs = querystring.stringify(data);
if (qs) qs = "?" + qs;
else try {
to_send = JSON.stringify(data);
} catch (e) {
return reject(e);
}
xhr.open(verb, url + qs);
xhr.onreadystatechange = handle_complete;
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(to_send);
function handle_complete() {
if (xhr.readyState !== 4) return;
var status = xhr.status
var result = {};
var response_text = xhr.responseText;
if (response_text) try {
result = JSON.parse(response_text)
} catch (e) {
return reject(e);
}
if ((status < 400) && status) return resolve({
status: status,
response: result,
request: xhr
});
var error = new Error("Server responded with status of " + status);
error.status = status;
error.request = xhr;
error.response = result;
reject(error);
}
});
}