-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (44 loc) · 1.11 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
var urls = require('comichron-data-urls');
module.exports = {
titles: titles,
byMonth: byMonth,
byIssue: byIssue,
sourceData: sourceData
};
function titles(callback) {
sendXhr(urls.titles(), callback);
}
function byMonth(id, callback) {
sendXhr(urls.byMonth(id), callback);
}
function byIssue(id, callback) {
sendXhr(urls.byIssue(id), callback);
}
function sourceData(year, month, callback) {
sendXhr(urls.sourceData(year, month), callback);
}
function sendXhr(url, callback) {
var req = new XMLHttpRequest();
req.addEventListener('load', onLoad);
req.addEventListener('error', onFail);
req.addEventListener('abort', onFail);
req.open('GET', url);
req.send();
function onLoad(event) {
if (req.status >= 400) {
return onFail(event);
}
var json = JSON.parse(this.responseText);
return callback(null, json);
}
function onFail(event) {
callback(makeError(req, url, event));
}
}
function makeError(req, url, xhrEvent) {
var error = new Error('Unable to GET ' + url);
error.statusCode = req.status;
error.url = url;
error.event = xhrEvent;
return error;
}