From 60867fb030bae582082340ead7dbc7efdc2f5398 Mon Sep 17 00:00:00 2001 From: 8cy <50817549+8cy@users.noreply.github.com> Date: Fri, 3 Apr 2020 02:37:42 -0700 Subject: 2020/04/03, 02:34, v1.2.0 --- node_modules/sync-request/browser.js | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 node_modules/sync-request/browser.js (limited to 'node_modules/sync-request/browser.js') diff --git a/node_modules/sync-request/browser.js b/node_modules/sync-request/browser.js new file mode 100644 index 0000000..e652e0c --- /dev/null +++ b/node_modules/sync-request/browser.js @@ -0,0 +1,64 @@ +'use strict'; + +var Response = require('http-response-object'); +var handleQs = require('then-request/lib/handle-qs.js'); + +module.exports = doRequest; +function doRequest(method, url, options) { + var xhr = new 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 (options === null || options === undefined) { + options = {}; + } + if (typeof options !== 'object') { + throw new TypeError('Options must be an object (or null).'); + } + + method = method.toUpperCase(); + options.headers = options.headers || {}; + + // handle cross domain + + var match; + var crossDomain = !!((match = /^([\w-]+:)?\/\/([^\/]+)/.exec(options.uri)) && (match[2] != 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'; + } + + // method, url, async + xhr.open(method, url, false); + + for (var name in options.headers) { + xhr.setRequestHeader(name.toLowerCase(), options.headers[name]); + } + + // avoid sending empty string (#319) + xhr.send(options.body ? options.body : null); + + + 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(); + } + }); + return new Response(xhr.status, headers, xhr.responseText); +} -- cgit v1.2.3