Beta 7 with some repo names changed.

This commit is contained in:
Luis Fernando Molina committed 2020-11-15 13:05:19 +01:00
1 parent 0f7b40cca7
commit 963c19493a
3408 files changed
+462217 -15516

No files matched your search

+59
View File
@@ -0,0 +1,59 @@
var queryString = require('query-string')
var setQuery = require('url-set-query')
var assign = require('object-assign')
var ensureHeader = require('./lib/ensure-header.js')
// this is replaced in the browser
var request = require('./lib/request.js')
var mimeTypeJson = 'application/json'
var noop = function () {}
module.exports = xhrRequest
function xhrRequest (url, opt, cb) {
if (!url || typeof url !== 'string') {
throw new TypeError('must specify a URL')
}
if (typeof opt === 'function') {
cb = opt
opt = {}
}
if (cb && typeof cb !== 'function') {
throw new TypeError('expected cb to be undefined or a function')
}
cb = cb || noop
opt = opt || {}
var defaultResponse = opt.json ? 'json' : 'text'
opt = assign({ responseType: defaultResponse }, opt)
var headers = opt.headers || {}
var method = (opt.method || 'GET').toUpperCase()
var query = opt.query
if (query) {
if (typeof query !== 'string') {
query = queryString.stringify(query)
}
url = setQuery(url, query)
}
// allow json response
if (opt.responseType === 'json') {
ensureHeader(headers, 'Accept', mimeTypeJson)
}
// if body content is json
if (opt.json && method !== 'GET' && method !== 'HEAD') {
ensureHeader(headers, 'Content-Type', mimeTypeJson)
opt.body = JSON.stringify(opt.body)
}
opt.method = method
opt.url = url
opt.headers = headers
delete opt.query
delete opt.json
return request(opt, cb)
}
+7
View File
@@ -0,0 +1,7 @@
module.exports = ensureHeader
function ensureHeader (headers, key, value) {
var lower = key.toLowerCase()
if (!headers[key] && !headers[lower]) {
headers[key] = value
}
}
+12
View File
@@ -0,0 +1,12 @@
module.exports = getResponse
function getResponse (opt, resp) {
if (!resp) return null
return {
statusCode: resp.statusCode,
headers: resp.headers,
method: opt.method,
url: opt.url,
// the XHR object in browser, http response in Node
rawRequest: resp.rawRequest ? resp.rawRequest : resp
}
}
+42
View File
@@ -0,0 +1,42 @@
var xhr = require('xhr')
var normalize = require('./normalize-response')
var noop = function () {}
module.exports = xhrRequest
function xhrRequest (opt, cb) {
delete opt.uri
// for better JSON.parse error handling than xhr module
var useJson = false
if (opt.responseType === 'json') {
opt.responseType = 'text'
useJson = true
}
var req = xhr(opt, function xhrRequestResult (err, resp, body) {
if (useJson && !err) {
try {
var text = resp.rawRequest.responseText
body = JSON.parse(text)
} catch (e) {
err = e
}
}
resp = normalize(opt, resp)
if (err) cb(err, null, resp)
else cb(err, body, resp)
cb = noop
})
// Patch abort() so that it also calls the callback, but with an error
var onabort = req.onabort
req.onabort = function () {
var ret = onabort.apply(req, Array.prototype.slice.call(arguments))
cb(new Error('XHR Aborted'))
cb = noop
return ret
}
return req
}
+47
View File
@@ -0,0 +1,47 @@
var request = require('simple-get')
var toArrayBuffer = require('buffer-to-arraybuffer')
var normalize = require('./normalize-response')
var timeout = require('timed-out')
var ensureHeader = require('./ensure-header')
// supported types
var responseTypes = [ 'text', 'arraybuffer', 'json' ]
module.exports = xhrRequest
function xhrRequest (opt, cb) {
var responseType = opt.responseType
if (responseType && responseTypes.indexOf(responseType) === -1) {
throw new TypeError('invalid responseType for node: ' + responseType)
}
// set a default user agent for Node
ensureHeader(opt.headers, 'User-Agent', 'https://github.com/Jam3/xhr-request')
var req = request.concat(opt, function xhrRequestResult (err, resp, data) {
if (!err) {
if (responseType === 'arraybuffer') {
data = toArrayBuffer(data)
} else if (responseType === 'json') {
try {
data = JSON.parse(data.toString())
} catch (e) {
err = e
}
} else { // 'text' response
data = data.toString()
}
}
resp = normalize(opt, resp)
if (err) {
cb(err, null, resp)
} else {
cb(null, data, resp)
}
})
if (typeof opt.timeout === 'number' && opt.timeout !== 0) {
timeout(req, opt.timeout)
}
return req
}
+90
View File
@@ -0,0 +1,90 @@
{
"_from": "xhr-request@^1.0.1",
"_id": "xhr-request@1.1.0",
"_inBundle": false,
"_integrity": "sha512-Y7qzEaR3FDtL3fP30k9wO/e+FBnBByZeybKOhASsGP30NIkRAAkKD/sCnLvgEfAIEC1rcmK7YG8f4oEnIrrWzA==",
"_location": "/xhr-request",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "xhr-request@^1.0.1",
"name": "xhr-request",
"escapedName": "xhr-request",
"rawSpec": "^1.0.1",
"saveSpec": null,
"fetchSpec": "^1.0.1"
},
"_requiredBy": [
"/swarm-js",
"/xhr-request-promise"
],
"_resolved": "https://registry.npmjs.org/xhr-request/-/xhr-request-1.1.0.tgz",
"_shasum": "f4a7c1868b9f198723444d82dcae317643f2e2ed",
"_spec": "xhr-request@^1.0.1",
"_where": "C:\\Users\\Luis\\source\\repos\\AAMasters\\BackendServers\\node_modules\\swarm-js",
"author": {
"name": "Matt DesLauriers",
"email": "dave.des@gmail.com",
"url": "https://github.com/mattdesl"
},
"browser": {
"./lib/request.js": "./lib/request-browser.js"
},
"bugs": {
"url": "https://github.com/Jam3/xhr-request/issues"
},
"bundleDependencies": false,
"dependencies": {
"buffer-to-arraybuffer": "^0.0.5",
"object-assign": "^4.1.1",
"query-string": "^5.0.1",
"simple-get": "^2.7.0",
"timed-out": "^4.0.1",
"url-set-query": "^1.0.0",
"xhr": "^2.0.4"
},
"deprecated": false,
"description": "tiny http client for Node and the browser",
"devDependencies": {
"budo": "^11.0.1",
"faucet": "0.0.1",
"standard": "^10.0.3",
"tape": "^4.0.1"
},
"homepage": "https://github.com/Jam3/xhr-request",
"keywords": [
"node",
"browser",
"http",
"got",
"request",
"gots",
"nets",
"xhr",
"xmlhttprequest",
"xml",
"http",
"request",
"client",
"https",
"requests",
"xml",
"get",
"query",
"string"
],
"license": "MIT",
"main": "index.js",
"name": "xhr-request",
"repository": {
"type": "git",
"url": "git://github.com/Jam3/xhr-request.git"
},
"scripts": {
"test": "standard && npm run test-node && npm run test-browser",
"test-browser": "budo test/test-browser.js -o",
"test-node": "node test/index.js | faucet"
},
"version": "1.1.0"
}