@eccenca/superagent
Advanced tools
Comparing version 1.4.0 to 2.0.0-alpha.1
@@ -1,5 +0,5 @@ | ||
var path = require('path'); | ||
const path = require('path'); | ||
module.exports = { | ||
testEntryPoint: path.join(__dirname, 'test', 'index.js'), | ||
testEntryPoint: path.join(__dirname, 'test', '**', '*.test.js'), | ||
webpackConfig: { | ||
@@ -6,0 +6,0 @@ production: { |
@@ -6,2 +6,12 @@ # Change Log | ||
## [Unreleased] | ||
TODO: add at least one Added, Changed, Deprecated, Removed, Fixed or Security section | ||
## [2.0.0] 2018-02-08 | ||
### Changed | ||
- Migrated from `rx@4` to `rxjs@5` | ||
- Updated dependencies | ||
## [1.4.0] 2018-01-23 | ||
@@ -8,0 +18,0 @@ |
@@ -157,10 +157,9 @@ module.exports = | ||
if (err) { | ||
err.response = res; | ||
observable.onError(err); | ||
observable.error(err); | ||
} else { | ||
observable.onNext(res); | ||
observable.next(res); | ||
} | ||
observable.onCompleted(); | ||
observable.complete(); | ||
}); | ||
@@ -177,3 +176,3 @@ }); | ||
module.exports = require("rx"); | ||
module.exports = require("rxjs/Rx"); | ||
@@ -202,7 +201,6 @@ /***/ }), | ||
//import array of all superagent http methods | ||
// import array of all superagent http methods | ||
var superagentGlobalPlugin = function superagentGlobalPlugin(superagent) { | ||
// This is a map of global superagent plugins | ||
@@ -214,3 +212,3 @@ var globalPlugins = {}; | ||
* Adds a plugin to each request if fn is a function, deletes it otherwise | ||
* | ||
* | ||
* @param {String} key | ||
@@ -221,3 +219,3 @@ * @param {Function|*} fn | ||
superagent.useForEachRequest = function (key, fn) { | ||
// | ||
// | ||
if (_lodash2.default.isFunction(fn)) { | ||
@@ -233,3 +231,2 @@ globalPlugins[key] = fn; | ||
_methods2.default.forEach(function (method) { | ||
var oldMethod = superagent[method]; | ||
@@ -267,3 +264,2 @@ | ||
function fixHeaders(callback, err, res) { | ||
if (res) { | ||
@@ -278,5 +274,4 @@ delete res.header; | ||
// other headers fails. | ||
//res.header['content-type'] = res.xhr.getResponseHeader('content-type'); | ||
// res.header['content-type'] = res.xhr.getResponseHeader('content-type'); | ||
} else { | ||
res.rawHeaders = res.request.res.rawHeaders; | ||
@@ -294,3 +289,2 @@ } | ||
module.exports = function (superagent) { | ||
var oldEnd = superagent.Request.prototype.end; | ||
@@ -349,12 +343,9 @@ | ||
//var rawHeaders = getRawHeaders(str); | ||
return _.chain(rawHeaders).chunk(2).sortBy(function (value) { | ||
return value[0]; | ||
}).reduce(function (result, current) { | ||
var field = current[0].toLowerCase(); | ||
var value = current[1]; | ||
if (typeof result[field] === "string") { | ||
if (typeof result[field] === 'string') { | ||
result[field] += ', ' + value; | ||
@@ -386,2 +377,6 @@ } else { | ||
var _proxy = __webpack_require__(13); | ||
var _proxy2 = _interopRequireDefault(_proxy); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -392,6 +387,4 @@ | ||
function httpProblemHandler(callback, err, res) { | ||
// Adapt new errors (problem+json) to the old format and inform the user about deprecation. | ||
if (err) { | ||
var detail = err.message; | ||
@@ -404,30 +397,24 @@ var title = 'An Error occurred'; | ||
if (_lodash2.default.get(err, 'response.type') === 'application/problem+json') { | ||
title = _lodash2.default.get(err, 'response.body.title', title); | ||
detail = _lodash2.default.get(err, 'response.body.detail', detail); | ||
} | ||
//We try to fit all non-http errors in the HTTP error scheme | ||
else { | ||
} else { | ||
// We try to fit all non-http errors in the HTTP error scheme | ||
var responseText = _lodash2.default.get(err, 'response.text', ''); | ||
var responseMessage = _lodash2.default.get(err, 'response.body.message', ''); | ||
var responseText = _lodash2.default.get(err, 'response.text', ''); | ||
var responseMessage = _lodash2.default.get(err, 'response.body.message', ''); | ||
// TODO: Which errors are also handled by superagent ? -> https://github.com/visionmedia/superagent | ||
// Handle Superagent Timeout Errors | ||
if (err.code === 'ECONNABORTED') { | ||
title = 'A timeout error occurred'; | ||
} | ||
// TODO: Which errors are also handled by superagent ? -> https://github.com/visionmedia/superagent | ||
// Handle Superagent Timeout Errors | ||
if (err.code === 'ECONNABORTED') { | ||
title = 'A timeout error occurred'; | ||
} else if (!_lodash2.default.isEmpty(responseMessage)) { | ||
// Handle HTTP Errors which have a body formed {message: '...'} | ||
else if (!_lodash2.default.isEmpty(responseMessage)) { | ||
detail = responseMessage; | ||
} | ||
// Handle plain HTTP text errors | ||
else if (!_lodash2.default.isEmpty(responseText)) { | ||
detail = responseText; | ||
} | ||
// Handle empty HTTP Errors with status code | ||
else if (_lodash2.default.isNumber(err.statusCode)) { | ||
detail = 'HTTP Error: ' + detail; | ||
} | ||
detail = responseMessage; | ||
} else if (!_lodash2.default.isEmpty(responseText)) { | ||
// Handle plain HTTP text errors | ||
detail = responseText; | ||
} else if (_lodash2.default.isNumber(err.statusCode)) { | ||
// Handle empty HTTP Errors with status code | ||
detail = 'HTTP Error: ' + detail; | ||
} | ||
} | ||
@@ -440,3 +427,3 @@ err.title = title; | ||
err = new Proxy(err, { | ||
err = new _proxy2.default(err, { | ||
get: function get(t, n) { | ||
@@ -446,5 +433,5 @@ if (n === 'message') { | ||
return t.title + '\n' + t.detail; | ||
} else { | ||
return t[n]; | ||
} | ||
return t[n]; | ||
} | ||
@@ -458,3 +445,2 @@ }); | ||
module.exports = function (superagent) { | ||
var oldEnd = superagent.Request.prototype.end; | ||
@@ -475,3 +461,9 @@ | ||
/***/ }), | ||
/* 13 */ | ||
/***/ (function(module, exports) { | ||
module.exports = require("proxy-polyfill/src/proxy"); | ||
/***/ }) | ||
/******/ ]); |
{ | ||
"name": "@eccenca/superagent", | ||
"version": "1.4.0", | ||
"version": "2.0.0-alpha.1", | ||
"description": "Extended version of superagent.js, pimped with RX, better Link header parsing and HTTP Problem support", | ||
@@ -31,13 +31,14 @@ "main": "es5/component.js", | ||
"devDependencies": { | ||
"@eccenca/dotfiles": "^2.3.1", | ||
"@eccenca/dotfiles": "^2.3.2", | ||
"@eccenca/gulp-tasks": "^3.15.0", | ||
"ecc-test-helpers": "^15.2.0", | ||
"fs-extra": "^5.0.0", | ||
"globby": "^7.1.1", | ||
"gulp": "^3.9.0", | ||
"nock": "^9.1.6" | ||
"nock": "^9.1.6", | ||
"should": "^13.2.1" | ||
}, | ||
"dependencies": { | ||
"lodash": "^4.13.1", | ||
"lodash": "^4.17.5", | ||
"methods": "^1.1.2", | ||
"proxy-polyfill": "^0.2.0", | ||
"rxjs": "^5.5.6", | ||
"superagent": "^3.8.2", | ||
@@ -44,0 +45,0 @@ "warning": "^3.0.0" |
const _ = require('lodash'); | ||
function getRawHeaders(string) { | ||
var lines = string.split(/\r?\n/); | ||
const lines = string.split(/\r?\n/); | ||
var res = []; | ||
const res = []; | ||
for (var i = 0, len = lines.length; i < len; ++i) { | ||
var line = lines[i]; | ||
for (let i = 0, len = lines.length; i < len; ++i) { | ||
const line = lines[i]; | ||
if (!_.isEmpty(line)) { | ||
var index = line.indexOf(':'); | ||
const index = line.indexOf(':'); | ||
var field = line.slice(0, index); | ||
var val = _.trim(line.slice(index + 1)); | ||
const field = line.slice(0, index); | ||
const val = _.trim(line.slice(index + 1)); | ||
@@ -21,9 +20,7 @@ res.push(field); | ||
} | ||
} | ||
return res; | ||
} | ||
module.exports = getRawHeaders; |
@@ -5,14 +5,11 @@ const _ = require('lodash'); | ||
//var rawHeaders = getRawHeaders(str); | ||
return _.chain(rawHeaders) | ||
.chunk(2) | ||
.sortBy((value) => value[0]) | ||
.sortBy(value => value[0]) | ||
.reduce((result, current) => { | ||
const field = current[0].toLowerCase(); | ||
const value = current[1]; | ||
var field = current[0].toLowerCase(); | ||
var value = current[1]; | ||
if (typeof result[field] === "string") { | ||
result[field] += ', ' + value; | ||
if (typeof result[field] === 'string') { | ||
result[field] += `, ${value}`; | ||
} else { | ||
@@ -25,5 +22,4 @@ result[field] = value; | ||
.value(); | ||
} | ||
module.exports = parseHeader; |
import _ from 'lodash'; | ||
//import array of all superagent http methods | ||
// import array of all superagent http methods | ||
import methods from 'methods'; | ||
@@ -9,4 +9,3 @@ | ||
const superagentGlobalPlugin = (superagent => { | ||
const superagentGlobalPlugin = superagent => { | ||
// This is a map of global superagent plugins | ||
@@ -18,3 +17,3 @@ const globalPlugins = {}; | ||
* Adds a plugin to each request if fn is a function, deletes it otherwise | ||
* | ||
* | ||
* @param {String} key | ||
@@ -25,3 +24,3 @@ * @param {Function|*} fn | ||
superagent.useForEachRequest = function(key, fn) { | ||
// | ||
// | ||
if (_.isFunction(fn)) { | ||
@@ -36,9 +35,8 @@ globalPlugins[key] = fn; | ||
// Apply method for each superagent method like .get and .post | ||
methods.forEach(function(method) { | ||
methods.forEach(method => { | ||
const oldMethod = superagent[method]; | ||
var oldMethod = superagent[method]; | ||
superagent[method] = function() { | ||
var request = oldMethod.apply(superagent, arguments); | ||
_.forEach(globalPlugins, function(plugin) { | ||
let request = oldMethod.apply(superagent, arguments); | ||
_.forEach(globalPlugins, plugin => { | ||
request = request.use(plugin); | ||
@@ -49,6 +47,4 @@ }); | ||
}); | ||
}; | ||
}); | ||
export default superagentGlobalPlugin; |
@@ -5,3 +5,2 @@ const rawHeaders = require('./getRawHeaders'); | ||
function fixHeaders(callback, err, res) { | ||
if (res) { | ||
@@ -16,17 +15,12 @@ delete res.header; | ||
// other headers fails. | ||
//res.header['content-type'] = res.xhr.getResponseHeader('content-type'); | ||
// res.header['content-type'] = res.xhr.getResponseHeader('content-type'); | ||
} else { | ||
res.rawHeaders = res.request.res.rawHeaders; | ||
} | ||
res.headers = res.header = parseHeaders(res.rawHeaders) | ||
res.headers = res.header = parseHeaders(res.rawHeaders); | ||
res._setHeaderProperties(res.headers); | ||
} | ||
callback(err, res); | ||
@@ -36,3 +30,2 @@ } | ||
module.exports = function(superagent) { | ||
const oldEnd = superagent.Request.prototype.end; | ||
@@ -39,0 +32,0 @@ |
import _ from 'lodash'; | ||
import warning from 'warning'; | ||
import Proxy from 'proxy-polyfill/src/proxy' | ||
@@ -8,6 +9,4 @@ const DEPRECATION_WARNING = `@eccenca/superagent: You used .message of to access Error details. | ||
function httpProblemHandler(callback, err, res) { | ||
// Adapt new errors (problem+json) to the old format and inform the user about deprecation. | ||
if (err) { | ||
let detail = err.message; | ||
@@ -20,9 +19,6 @@ let title = 'An Error occurred'; | ||
if (_.get(err, 'response.type') === 'application/problem+json') { | ||
title = _.get(err, 'response.body.title', title); | ||
detail = _.get(err, 'response.body.detail', detail); | ||
} | ||
//We try to fit all non-http errors in the HTTP error scheme | ||
else { | ||
} else { | ||
// We try to fit all non-http errors in the HTTP error scheme | ||
const responseText = _.get(err, 'response.text', ''); | ||
@@ -35,16 +31,12 @@ const responseMessage = _.get(err, 'response.body.message', ''); | ||
title = 'A timeout error occurred'; | ||
} | ||
// Handle HTTP Errors which have a body formed {message: '...'} | ||
else if (!_.isEmpty(responseMessage)) { | ||
} else if (!_.isEmpty(responseMessage)) { | ||
// Handle HTTP Errors which have a body formed {message: '...'} | ||
detail = responseMessage; | ||
} | ||
// Handle plain HTTP text errors | ||
else if (!_.isEmpty(responseText)) { | ||
} else if (!_.isEmpty(responseText)) { | ||
// Handle plain HTTP text errors | ||
detail = responseText; | ||
} else if (_.isNumber(err.statusCode)) { | ||
// Handle empty HTTP Errors with status code | ||
detail = `HTTP Error: ${detail}`; | ||
} | ||
// Handle empty HTTP Errors with status code | ||
else if (_.isNumber(err.statusCode)) { | ||
detail = 'HTTP Error: ' + detail; | ||
} | ||
} | ||
@@ -59,13 +51,11 @@ | ||
err = new Proxy(err, { | ||
get: function(t, n) { | ||
get(t, n) { | ||
if (n === 'message') { | ||
warning(!__DEBUG__, DEPRECATION_WARNING); | ||
return t.title + '\n' + t.detail; | ||
return `${t.title}\n${t.detail}`; | ||
} | ||
else { | ||
return t[n]; | ||
} | ||
} | ||
return t[n]; | ||
}, | ||
}); | ||
} | ||
@@ -77,3 +67,2 @@ | ||
module.exports = function(superagent) { | ||
const oldEnd = superagent.Request.prototype.end; | ||
@@ -80,0 +69,0 @@ |
@@ -1,18 +0,17 @@ | ||
var Rx = require('rx'); | ||
const Rx = require('rxjs/Rx'); | ||
module.exports = function(superagent) { | ||
superagent.Request.prototype.observe = function() { | ||
var self = this; | ||
const self = this; | ||
// wrap into observable and return | ||
return Rx.Observable.create(function(observable) { | ||
self.end(function(err, res) { | ||
return Rx.Observable.create(observable => { | ||
self.end((err, res) => { | ||
if (err) { | ||
err.response = res; | ||
observable.onError(err); | ||
observable.error(err); | ||
} else { | ||
observable.onNext(res); | ||
observable.next(res); | ||
} | ||
observable.onCompleted(); | ||
observable.complete(); | ||
}); | ||
@@ -19,0 +18,0 @@ }); |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
23632
6
6
536
2
+ Addedproxy-polyfill@^0.2.0
+ Addedrxjs@^5.5.6
+ Addedproxy-polyfill@0.2.0(transitive)
+ Addedrxjs@5.5.12(transitive)
+ Addedsymbol-observable@1.0.1(transitive)
Updatedlodash@^4.17.5