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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
'use strict';
var Promise = require('promise');
var Response = require('http-response-object');
var handleQs = require('./lib/handle-qs.js');
module.exports = doRequest;
function doRequest(method, url, options, callback) {
var result = new Promise(function (resolve, reject) {
var xhr = new window.XMLHttpRequest();
// check types of arguments
if (typeof method !== 'string') {
throw new TypeError('The method must be a string.');
}
if (typeof url !== 'string') {
throw new TypeError('The URL/path must be a string.');
}
if (typeof options === 'function') {
callback = options;
options = {};
}
if (options === null || options === undefined) {
options = {};
}
if (typeof options !== 'object') {
throw new TypeError('Options must be an object (or null).');
}
if (typeof callback !== 'function') {
callback = undefined;
}
method = method.toUpperCase();
options.headers = options.headers || {};
function attempt(n) {
doRequest(method, url, {
qs: options.qs,
headers: options.headers,
timeout: options.timeout
}).nodeify(function (err, res) {
var retry = err || res.statusCode >= 400;
if (typeof options.retry === 'function') {
retry = options.retry(err, res, n + 1);
}
if (n >= (options.maxRetries | 5)) {
retry = false;
}
if (retry) {
var delay = options.retryDelay;
if (typeof options.retryDelay === 'function') {
delay = options.retryDelay(err, res, n + 1);
}
delay = delay || 200;
setTimeout(function () {
attempt(n + 1);
}, delay);
} else {
if (err) reject(err);
else resolve(res);
}
});
}
if (options.retry && method === 'GET') {
return attempt(0);
}
// handle cross domain
var match;
var crossDomain = !!((match = /^([\w-]+:)?\/\/([^\/]+)/.exec(url)) && (match[2] != window.location.host));
if (!crossDomain) options.headers['X-Requested-With'] = 'XMLHttpRequest';
// handle query string
if (options.qs) {
url = handleQs(url, options.qs);
}
// handle json body
if (options.json) {
options.body = JSON.stringify(options.json);
options.headers['Content-Type'] = 'application/json';
}
if (options.timeout) {
xhr.timeout = options.timeout;
var start = Date.now();
xhr.ontimeout = function () {
var duration = Date.now() - start;
var err = new Error('Request timed out after ' + duration + 'ms');
err.timeout = true;
err.duration = duration;
reject(err);
};
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var headers = {};
xhr.getAllResponseHeaders().split('\r\n').forEach(function (header) {
var h = header.split(':');
if (h.length > 1) {
headers[h[0].toLowerCase()] = h.slice(1).join(':').trim();
}
});
var res = new Response(xhr.status, headers, xhr.responseText);
res.url = url;
resolve(res);
}
};
// method, url, async
xhr.open(method, url, true);
for (var name in options.headers) {
xhr.setRequestHeader(name, options.headers[name]);
}
// avoid sending empty string (#319)
xhr.send(options.body ? options.body : null);
});
result.getBody = function () {
return result.then(function (res) { return res.getBody(); });
};
return result.nodeify(callback);
}
|