oniyi-http-client
Advanced tools
Comparing version 1.1.1 to 1.2.0
'use strict'; | ||
// node core modules | ||
const util = require('util'); | ||
// 3rd party modules | ||
const async = require('async'); | ||
const _ = require('lodash'); | ||
// internal modules | ||
function applyPlugins(req, plugins, params, callback) { | ||
@@ -23,27 +27,32 @@ if (!Array.isArray(plugins)) { | ||
return async.reduce(asyncPlugins, _.cloneDeep(params), (currentParams, plugin, iteratorCallback) => { | ||
let reduceError; | ||
return async.reduce( | ||
asyncPlugins, | ||
Object.assign({}, params), | ||
(currentParams, plugin, iteratorCallback) => { | ||
let reduceError; | ||
// verify that we have a name property | ||
if (typeof plugin.name !== 'string') { | ||
reduceError = new TypeError('plugin.name must be a string'); | ||
return iteratorCallback(reduceError); | ||
} | ||
// verify that we have a name property | ||
if (typeof plugin.name !== 'string') { | ||
reduceError = new TypeError('plugin.name must be a string'); | ||
return iteratorCallback(reduceError); | ||
} | ||
// verify the load property exists and is of type function | ||
if (!_.isFunction(plugin.load)) { | ||
reduceError = new TypeError(util.format('Plugin "%s": property "load" must be of type function', plugin.name)); | ||
return iteratorCallback(reduceError); | ||
} | ||
// verify the load property exists and is of type function | ||
if (!_.isFunction(plugin.load)) { | ||
reduceError = new TypeError(util.format('Plugin "%s": property "load" must be of type function', plugin.name)); | ||
return iteratorCallback(reduceError); | ||
} | ||
// call the plugin's load function | ||
return plugin.load(req, currentParams, iteratorCallback); | ||
}, (err, newParams) => { | ||
if (err) { | ||
return req.emit('error', err); | ||
// call the plugin's load function | ||
return plugin.load(req, currentParams, iteratorCallback); | ||
}, | ||
(err, newParams) => { | ||
if (err) { | ||
return req.emit('error', err); | ||
} | ||
return callback(null, newParams); | ||
} | ||
return callback(null, newParams); | ||
}); | ||
); | ||
} | ||
module.exports = applyPlugins; |
'use strict'; | ||
// node core modules | ||
// 3rd party modules | ||
// internal modules | ||
const applyPlugins = require('./apply-plugins'); | ||
@@ -4,0 +9,0 @@ |
@@ -36,3 +36,3 @@ 'use strict'; | ||
plugin = require(pluginPath); // eslint-disable-line global-require | ||
plugin = require(pluginPath); // eslint-disable-line global-require,import/no-dynamic-require | ||
} | ||
@@ -68,6 +68,10 @@ | ||
// merge params with defaults | ||
let requestParams = _.defaultsDeep({}, | ||
initialParams, { | ||
let requestParams = _.defaultsDeep( | ||
{}, | ||
initialParams, | ||
{ | ||
method: 'GET', | ||
}, defaults); | ||
}, | ||
defaults | ||
); | ||
@@ -116,8 +120,12 @@ // augment requestParams to properly handle absolute uris | ||
Object.assign(client, { | ||
use: mountPlugin, | ||
jar, | ||
makeRequest, | ||
getDefaults, | ||
}, sugarMethods); | ||
Object.assign( | ||
client, | ||
{ | ||
use: mountPlugin, | ||
jar, | ||
makeRequest, | ||
getDefaults, | ||
}, | ||
sugarMethods | ||
); | ||
@@ -124,0 +132,0 @@ // finally return the client instance |
@@ -33,3 +33,3 @@ 'use strict'; | ||
// keep a reference to the originally provided callback function | ||
const callback = options.callback; | ||
const { callback } = options; | ||
@@ -97,3 +97,6 @@ // make sure, the original callback is only called once | ||
/* eslint-disable max-len */ | ||
return self.emit('error', new Error('`unix://` URL scheme is no longer supported. Please use the format `http://unix:SOCKET:PATH`')); | ||
return self.emit( | ||
'error', | ||
new Error('`unix://` URL scheme is no longer supported. Please use the format `http://unix:SOCKET:PATH`') | ||
); | ||
/* eslint-enable max-len */ | ||
@@ -113,128 +116,4 @@ } | ||
}); | ||
// self.on('pipe', function (src) { | ||
// if (self.ntick && self._started) { | ||
// self.emit('error', new Error('You cannot pipe to this stream after the outbound request has started.')); | ||
// } | ||
// self.src = src; | ||
// if (isReadStream(src)) { | ||
// if (!self.hasHeader('content-type')) { | ||
// self.setHeader('content-type', mime.lookup(src.path)); | ||
// } | ||
// } else { | ||
// if (src.headers) { | ||
// for (const i in src.headers) { | ||
// if (!self.hasHeader(i)) { | ||
// self.setHeader(i, src.headers[i]) | ||
// } | ||
// } | ||
// } | ||
// if (self._json && !self.hasHeader('content-type')) { | ||
// self.setHeader('content-type', 'application/json'); | ||
// } | ||
// if (src.method && !self.explicitMethod) { | ||
// self.method = src.method; | ||
// } | ||
// } | ||
// // self.on('pipe', function () { | ||
// // console.error('You have already piped to this stream. Pipeing twice is likely to break the request.') | ||
// // }) | ||
// }); | ||
}; | ||
Request.prototype.start = function start() { | ||
const self = this; | ||
self.req.on('response', self.onRequestResponse.bind(self)); | ||
self.req.on('error', self.onRequestError.bind(self)); | ||
self.req.on('drain', () => { | ||
self.emit('drain'); | ||
}); | ||
self.req.on('socket', (socket) => { | ||
self.emit('socket', socket); | ||
}); | ||
self.on('end', () => { | ||
if (self.req.connection) { | ||
self.req.connection.removeListener('error', connectionErrorHandler); | ||
} | ||
}); | ||
self.emit('request', self.req); | ||
}; | ||
// Stream API | ||
Request.prototype.pipe = function pipe(dest, opts) { | ||
const self = this; | ||
if (self.response) { | ||
if (self._destdata) { | ||
self.emit('error', new Error('You cannot pipe after data has been emitted from the response.')); | ||
} else if (self._ended) { | ||
self.emit('error', new Error('You cannot pipe after the response has been ended.')); | ||
} else { | ||
stream.Stream.prototype.pipe.call(self, dest, opts); | ||
self.pipeDest(dest); | ||
return dest; | ||
} | ||
} else { | ||
self.dests.push(dest); | ||
stream.Stream.prototype.pipe.call(self, dest, opts); | ||
return dest; | ||
} | ||
}; | ||
Request.prototype.write = function write(...args) { | ||
const self = this; | ||
if (self._aborted) { | ||
return; | ||
} | ||
if (!self._started) { | ||
self.start(); | ||
} | ||
return self.req.write.apply(self.req, args); | ||
}; | ||
Request.prototype.end = function end(chunk) { | ||
const self = this; | ||
if (self._aborted) { | ||
return; | ||
} | ||
if (chunk) { | ||
self.write(chunk); | ||
} | ||
if (!self._started) { | ||
self.start(); | ||
} | ||
self.req.end(); | ||
}; | ||
Request.prototype.pause = function pause(...args) { | ||
const self = this; | ||
if (!self.responseContent) { | ||
self._paused = true; | ||
} else { | ||
self.responseContent.pause.apply(self.responseContent, args); | ||
} | ||
}; | ||
Request.prototype.resume = function resume(...args) { | ||
const self = this; | ||
if (!self.responseContent) { | ||
self._paused = false; | ||
} else { | ||
self.responseContent.resume.apply(self.responseContent, args); | ||
} | ||
}; | ||
Request.prototype.destroy = function destroy() { | ||
const self = this; | ||
if (!self._ended) { | ||
self.end(); | ||
} else if (self.response) { | ||
self.response.destroy(); | ||
} | ||
}; | ||
module.exports = Request; |
{ | ||
"name": "oniyi-http-client", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "Adding a plugin interface to \"request\" that allows modifications of request parameters and response data", | ||
"homepage": "", | ||
"author": { | ||
"name": "Benjamin Kroeger", | ||
"email": "benjamin.kroeger@gmail.com", | ||
"url": "" | ||
}, | ||
"homepage": "https://github.com/benkroeger/oniyi-http-client#readme", | ||
"author": "Benjamin Kroeger <benjamin.kroeger@gmail.com>", | ||
"files": [ | ||
@@ -21,27 +17,27 @@ "lib", | ||
], | ||
"repository": "git@github.com:benkroeger/oniyi-http-client.git", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+ssh://git@github.com/benkroeger/oniyi-http-client.git" | ||
}, | ||
"devDependencies": { | ||
"eslint": "^3.5.0", | ||
"eslint-config-oniyi": "^4.2.0", | ||
"gulp": "3.9.1", | ||
"gulp-coveralls": "0.1.4", | ||
"gulp-eslint": "3.0.1", | ||
"gulp-exclude-gitignore": "1.0.0", | ||
"gulp-istanbul": "1.1.1", | ||
"gulp-mocha": "3.0.1", | ||
"gulp-nsp": "2.4.2", | ||
"gulp-plumber": "1.1.0" | ||
"eslint": "^4.8.0", | ||
"eslint-config-oniyi": "^5.0.2" | ||
}, | ||
"scripts": { | ||
"prepublish": "gulp prepublish", | ||
"test": "gulp" | ||
"lint": "eslint --ignore-path .gitignore ." | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"async": "2.0.1", | ||
"lodash": "4.15.0", | ||
"async": "^2.5.0", | ||
"lodash": "^4.17.4", | ||
"oniyi-logger": "^1.0.0", | ||
"request": "^2.78.0", | ||
"tough-cookie": "^2.3.2" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/benkroeger/oniyi-http-client/issues" | ||
}, | ||
"directories": { | ||
"test": "test" | ||
} | ||
} |
@@ -67,3 +67,2 @@ 'use strict'; | ||
}); | ||
return; | ||
} | ||
@@ -73,5 +72,4 @@ }); | ||
callback(null, params); | ||
return; | ||
}); | ||
}, | ||
}; |
# oniyi-http-client | ||
[![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url] | ||
[![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] | ||
> Adding a plugin interface to [request](https://www.npmjs.com/package/request) that allows modifications of request parameters and response data | ||
@@ -54,3 +54,3 @@ | ||
This creates a plugin named `plugin-2` which adds a request-header with name and value `plugin-2`. | ||
Also, it stores some data in a local variable and overrides the original callback function | ||
Also, it stores some data in a local variable and overrides the original callback function | ||
to print that stored data on response. Afterwards it calls the original callback function. | ||
@@ -107,3 +107,3 @@ | ||
Apache-2.0 © [Benjamin Kroeger]() | ||
MIT © [Benjamin Kroeger]() | ||
@@ -113,7 +113,3 @@ | ||
[npm-url]: https://npmjs.org/package/oniyi-http-client | ||
[travis-image]: https://travis-ci.org/benkroeger/oniyi-http-client.svg?branch=master | ||
[travis-url]: https://travis-ci.org/benkroeger/oniyi-http-client | ||
[daviddm-image]: https://david-dm.org/benkroeger/oniyi-http-client.svg?theme=shields.io | ||
[daviddm-url]: https://david-dm.org/benkroeger/oniyi-http-client | ||
[coveralls-image]: https://coveralls.io/repos/benkroeger/oniyi-http-client/badge.svg | ||
[coveralls-url]: https://coveralls.io/r/benkroeger/oniyi-http-client |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
2
0
0
1
16909
332
113
+ Addedasync@2.6.4(transitive)
+ Addedlodash@4.17.21(transitive)
- Removedasync@2.0.1(transitive)
- Removedlodash@4.15.0(transitive)
Updatedasync@^2.5.0
Updatedlodash@^4.17.4