Current Folder
This commit is contained in:
1 parent
8b3382289b
commit
cfcada880d
4379 files changed
No files matched your search
+7
@@ -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
@@ -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
@@ -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
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user