diff options
| author | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
|---|---|---|
| committer | 8cy <[email protected]> | 2020-04-03 02:37:42 -0700 |
| commit | 60867fb030bae582082340ead7dbc7efdc2f5398 (patch) | |
| tree | 4c6a7356351be2e4914e15c4703172597c45656e /node_modules/then-request/browser.js | |
| parent | commenting (diff) | |
| download | s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.tar.xz s5nical-60867fb030bae582082340ead7dbc7efdc2f5398.zip | |
2020/04/03, 02:34, v1.2.0
Diffstat (limited to 'node_modules/then-request/browser.js')
| -rw-r--r-- | node_modules/then-request/browser.js | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/node_modules/then-request/browser.js b/node_modules/then-request/browser.js new file mode 100644 index 0000000..a2b605a --- /dev/null +++ b/node_modules/then-request/browser.js @@ -0,0 +1,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); +} |