oniyi-http-client
Advanced tools
Comparing version 0.0.3 to 0.1.0
'use strict'; | ||
var util = require('util'); | ||
const util = require('util'); | ||
var async = require('async'); | ||
var _ = require('lodash'); | ||
const async = require('async'); | ||
const _ = require('lodash'); | ||
@@ -14,5 +14,7 @@ function applyPlugins(req, plugins, params, callback) { | ||
// ensure async nature of plugin.load function | ||
plugins = plugins.map(function (plugin) { | ||
const asyncPlugins = plugins.map((plugin) => { | ||
if (plugin.load && plugin.load.length === 2) { | ||
plugin.load = async.asyncify(plugin.load); | ||
return _.assign(plugin, { | ||
load: async.asyncify(plugin.load), | ||
}); | ||
} | ||
@@ -22,12 +24,9 @@ return plugin; | ||
async.reduce(plugins, _.cloneDeep(params), function (currentParams, plugin, iteratorCallback) { | ||
var err; | ||
return async.reduce(asyncPlugins, _.cloneDeep(params), (currentParams, plugin, iteratorCallback) => { | ||
let reduceError; | ||
// keep track of the currently executed plugin | ||
params.currentPlugin = plugin.name; | ||
// verify that we have a name property | ||
if (typeof plugin.name !== 'string') { | ||
err = new TypeError('plugin.name must be a string'); | ||
return iteratorCallback(err); | ||
reduceError = new TypeError('plugin.name must be a string'); | ||
return iteratorCallback(reduceError); | ||
} | ||
@@ -37,4 +36,4 @@ | ||
if (!_.isFunction(plugin.load)) { | ||
err = new TypeError(util.format('Plugin "%s": property "load" must be of type function', plugin.name)); | ||
return iteratorCallback(err); | ||
reduceError = new TypeError(util.format('Plugin "%s": property "load" must be of type function', plugin.name)); | ||
return iteratorCallback(reduceError); | ||
} | ||
@@ -44,4 +43,3 @@ | ||
return plugin.load(req, currentParams, iteratorCallback); | ||
}, function (err, newParams) { | ||
}, (err, newParams) => { | ||
if (err) { | ||
@@ -48,0 +46,0 @@ return req.emit('error', err); |
167
lib/index.js
'use strict'; | ||
// core modules | ||
var util = require('util'); | ||
var path = require('path'); | ||
const util = require('util'); | ||
const path = require('path'); | ||
// npm modules | ||
var _ = require('lodash'); | ||
var request = require('request'); | ||
var tough = require('tough-cookie'); | ||
var pkg = require(path.resolve(__dirname, '..', 'package.json')); | ||
var logger = require('oniyi-logger')(util.format('%s@v%s', pkg.name, pkg.version)); | ||
const _ = require('lodash'); | ||
const request = require('request'); | ||
const tough = require('tough-cookie'); | ||
const pkg = require(path.resolve(__dirname, '..', 'package.json')); | ||
const logger = require('oniyi-logger')(util.format('%s@v%s', pkg.name, pkg.version)); | ||
// others | ||
var Request = require('./request'); | ||
const Request = require('./request'); | ||
function OniyiHttpClient(options) { | ||
if (!(this instanceof OniyiHttpClient)) { | ||
return new OniyiHttpClient(options); | ||
} | ||
function OniyiHttpClient(params) { | ||
const options = _.assign({}, params || {}); | ||
options = options || {}; | ||
this.defaults = options.defaults || {}; | ||
this.plugins = []; | ||
const client = {}; | ||
const defaults = options.defaults || {}; | ||
const plugins = []; | ||
this.requestFunction = request.defaults(this.defaults); | ||
} | ||
function mountPlugin(pluginSpec) { | ||
// when called without arguments, return immediately | ||
if (!pluginSpec) { | ||
logger.warn('use: invalid pluginSpec argument provided', pluginSpec); | ||
return client; | ||
} | ||
OniyiHttpClient.prototype.use = function usePlugin(plugin) { | ||
var self = this; | ||
let plugin = pluginSpec; | ||
// if plugin argument is a string, load built-in plugin | ||
if (typeof pluginSpec === 'string') { | ||
const pluginPath = path.resolve(__dirname, '..', 'plugins', pluginSpec.toLowerCase()); | ||
// when called without arguments, return immediately | ||
if (!plugin) { | ||
logger.warn('usePlugin: invalid plugin argument provided', plugin); | ||
return self; | ||
} | ||
logger.debug('use: plugin argument is of type "string'); | ||
logger.debug('use: resolved plugin path "%s"', pluginPath); | ||
// make sure we have an array to push plugins to | ||
if (!Array.isArray(self.plugins)) { | ||
self.plugins = []; | ||
} | ||
plugin = require(pluginPath); | ||
} | ||
// if plugin argument is a string, load built-in plugin | ||
if (typeof plugin === 'string') { | ||
plugin = path.resolve(__dirname, '..', 'plugins', plugin.toLowerCase()); | ||
// check plugin type | ||
// should have name property | ||
// should have onRequest or callback properties of type function | ||
if (!_.isPlainObject(plugin)) { | ||
logger.warn('use: failed to resolve plugin argument to plain object', plugin); | ||
return client; | ||
} | ||
logger.debug('usePlugin: plugin argument is of type "string'); | ||
logger.debug('usePlugin: resolved plugin path "%s"', plugin); | ||
plugin = require(plugin); | ||
plugins.push(plugin); | ||
logger.debug('use: added plugin "%s" on position "%d"', plugin.name, plugins.length); | ||
return client; | ||
} | ||
// check plugin type | ||
// should have name property | ||
// should have onRequest or callback properties of type function | ||
if (!_.isPlainObject(plugin)) { | ||
logger.warn('usePlugin: failed to resolve plugin argument to plain object', plugin); | ||
return self; | ||
} | ||
function jar(store) { | ||
// when there is no store or a sync store provided, use default method from request module | ||
if (!store || store.synchronous) { | ||
return request.jar(store); | ||
} | ||
self.plugins.push(plugin); | ||
logger.debug('usePlugin: added plugin "%s" on position "%d"', plugin.name, self.plugins.length); | ||
return self; | ||
}; | ||
OniyiHttpClient.prototype.jar = function (store) { | ||
// when there is no store or a sync store provided, use default method from request module | ||
if (!store || store.synchronous) { | ||
return request.jar(store); | ||
// for async stores, return a new tough-cookie jar | ||
return new tough.CookieJar(store); | ||
} | ||
// for async stores, return a new tough-cookie jar | ||
return new tough.CookieJar(store); | ||
}; | ||
function makeRequest(uri, reqOptions, reqCallback) { | ||
// initialize provided arguments into params object | ||
const initialParams = request.initParams(uri, reqOptions, reqCallback); | ||
OniyiHttpClient.prototype.makeRequest = function () { | ||
var self = this; | ||
// call request directly, if there are no plugins loaded in this client | ||
if (!Array.isArray(plugins) || plugins.length < 1) { | ||
logger.info('no plugins loaded, uri: %s', initialParams.uri); | ||
return new request.Request(_.defaults({}, defaults, initialParams)); | ||
} | ||
// initialize provided arguments into params object | ||
var initialParams = request.initParams.apply(null, arguments); | ||
// merge params with defaults | ||
const requestParams = _.merge({ | ||
method: 'GET', | ||
}, defaults, initialParams); | ||
// call request directly, if there are no plugins loaded in this client | ||
if (!Array.isArray(self.plugins) || self.plugins.length < 1) { | ||
logger.info('no plugins loaded, uri:', initialParams.uri.href); | ||
return new self.requestFunction.Request(initialParams); | ||
return new Request(requestParams, plugins); | ||
} | ||
// merge params with defaults | ||
var params = _.merge({ | ||
method: 'GET' | ||
}, self.defaults, initialParams); | ||
// Sugar methods | ||
const sugarMethods = ['get', 'put', 'post', 'del', 'head', 'options'].reduce((sugar, method) => { | ||
const requestMethod = method === 'del' ? 'DELETE' : method.toUpperCase(); | ||
/* eslint-disable no-param-reassign */ | ||
sugar[method] = (uri, reqOptions, reqCallback) => { | ||
// initialize provided arguments into params object | ||
// set request method | ||
const requestParams = _.assign(request.initParams(uri, reqOptions, reqCallback), { | ||
method: requestMethod, | ||
}); | ||
return new Request(params, self.plugins); | ||
}; | ||
// make actual request | ||
return makeRequest(requestParams, requestParams.callback); | ||
}; | ||
/* eslint-enable no-param-reassign */ | ||
// Sugar methods | ||
['get', 'put', 'post', 'del', 'head', 'options'].forEach(function makeSugarMethod(method) { | ||
var requestMethod = method === 'del' ? 'DELETE' : method.toUpperCase(); | ||
return sugar; | ||
}, {}); | ||
OniyiHttpClient.prototype[method] = function () { | ||
// initialize provided arguments into params object | ||
var params = request.initParams.apply(null, arguments); | ||
// init any plugins provided on client creation | ||
if (Array.isArray(options.plugins) && options.plugins.lenth > 0) { | ||
options.plugins.forEach((pluginSpec) => { | ||
mountPlugin(pluginSpec); | ||
}); | ||
} | ||
// set request method | ||
params.method = requestMethod; | ||
_.assign(client, { | ||
use: mountPlugin, | ||
jar, | ||
makeRequest, | ||
}, sugarMethods); | ||
// make actual request | ||
return this.makeRequest(params, params.callback); | ||
}; | ||
}); | ||
// finally return the client instance | ||
return client; | ||
} | ||
module.exports = OniyiHttpClient; |
'use strict'; | ||
// core modules | ||
var util = require('util'); | ||
var path = require('path'); | ||
var stream = require('stream'); | ||
var url = require('url'); | ||
const util = require('util'); | ||
const path = require('path'); | ||
const stream = require('stream'); | ||
const url = require('url'); | ||
// npm modules | ||
var request = require('request'); | ||
var pkg = require(path.resolve(__dirname, '..', 'package.json')); | ||
var logger = require('oniyi-logger')(util.format('%s@v%s:request', pkg.name, pkg.version)); | ||
const request = require('request'); | ||
const pkg = require(path.resolve(__dirname, '..', 'package.json')); | ||
const logger = require('oniyi-logger')(util.format('%s@v%s:request', pkg.name, pkg.version)); | ||
const _ = require('lodash'); | ||
var applyPlugins = require('./helpers/apply-plugins'); | ||
const applyPlugins = require('./helpers/apply-plugins'); | ||
function Request(options, plugins) { | ||
var self = this; | ||
const self = this; | ||
@@ -27,12 +28,13 @@ // become a duplex stream | ||
Request.prototype.init = function requestInit(options, plugins) { | ||
var self = this; | ||
Request.prototype.init = function requestInit(params, plugins) { | ||
const self = this; | ||
self.dests = self.dests || []; | ||
const options = _.assign({}, params); | ||
// keep a reference to the originally provided callback function | ||
var callback = options.callback; | ||
const callback = options.callback; | ||
// make sure, the original callback is only called once | ||
var callbackCalled = false; | ||
options.callback = function () { | ||
let callbackCalled = false; | ||
options.callback = (err, response, body) => { | ||
if (callbackCalled) { | ||
@@ -44,3 +46,3 @@ logger.warn('multiple attempts to call request callback function'); | ||
callbackCalled = true; | ||
return callback.apply(null, arguments); | ||
return callback(err, response, body); | ||
}; | ||
@@ -104,7 +106,9 @@ | ||
if (options.uri.protocol === 'unix:') { | ||
/* 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`')); | ||
/* eslint-enable max-len */ | ||
} | ||
// let plugins manipulate the params object | ||
applyPlugins(self, plugins, options, function (pluginError, modifiedParams) { | ||
return applyPlugins(self, plugins, options, (pluginError, modifiedParams) => { | ||
if (pluginError) { | ||
@@ -132,3 +136,3 @@ // something went wrong in one of the plugins | ||
// if (src.headers) { | ||
// for (var i in src.headers) { | ||
// for (const i in src.headers) { | ||
// if (!self.hasHeader(i)) { | ||
@@ -154,13 +158,13 @@ // self.setHeader(i, src.headers[i]) | ||
Request.prototype.start = function start() { | ||
var self = this; | ||
const self = this; | ||
self.req.on('response', self.onRequestResponse.bind(self)); | ||
self.req.on('error', self.onRequestError.bind(self)); | ||
self.req.on('drain', function () { | ||
self.req.on('drain', () => { | ||
self.emit('drain'); | ||
}); | ||
self.req.on('socket', function (socket) { | ||
self.req.on('socket', (socket) => { | ||
self.emit('socket', socket); | ||
}); | ||
self.on('end', function () { | ||
self.on('end', () => { | ||
if (self.req.connection) { | ||
@@ -174,4 +178,4 @@ self.req.connection.removeListener('error', connectionErrorHandler); | ||
// Stream API | ||
Request.prototype.pipe = function (dest, opts) { | ||
var self = this; | ||
Request.prototype.pipe = (dest, opts) => { | ||
const self = this; | ||
@@ -195,4 +199,4 @@ if (self.response) { | ||
Request.prototype.write = function () { | ||
var self = this; | ||
Request.prototype.write = function() { | ||
const self = this; | ||
if (self._aborted) { | ||
@@ -208,4 +212,4 @@ return; | ||
Request.prototype.end = function (chunk) { | ||
var self = this; | ||
Request.prototype.end = function(chunk) { | ||
const self = this; | ||
if (self._aborted) { | ||
@@ -224,4 +228,4 @@ return; | ||
Request.prototype.pause = function () { | ||
var self = this; | ||
Request.prototype.pause = function() { | ||
const self = this; | ||
if (!self.responseContent) { | ||
@@ -234,4 +238,4 @@ self._paused = true; | ||
Request.prototype.resume = function () { | ||
var self = this; | ||
Request.prototype.resume = function() { | ||
const self = this; | ||
if (!self.responseContent) { | ||
@@ -244,4 +248,4 @@ self._paused = false; | ||
Request.prototype.destroy = function () { | ||
var self = this; | ||
Request.prototype.destroy = function() { | ||
const self = this; | ||
if (!self._ended) { | ||
@@ -248,0 +252,0 @@ self.end(); |
{ | ||
"name": "oniyi-http-client", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Adding a plugin interface to \"request\" that allows modifications of request parameters and response data", | ||
@@ -23,11 +23,12 @@ "homepage": "", | ||
"devDependencies": { | ||
"eslint": "^1.0.0", | ||
"gulp": "^3.6.0", | ||
"gulp-eslint": "^1.0.0", | ||
"eslint": "^2.7.0", | ||
"eslint-config-oniyi": "^2.0.2", | ||
"gulp": "^3.9.1", | ||
"gulp-coveralls": "^0.1.4", | ||
"gulp-eslint": "^2.0.0", | ||
"gulp-exclude-gitignore": "^1.0.0", | ||
"gulp-istanbul": "^0.10.3", | ||
"gulp-mocha": "^2.0.0", | ||
"gulp-plumber": "^1.0.0", | ||
"gulp-nsp": "^2.1.0", | ||
"gulp-coveralls": "^0.1.0" | ||
"gulp-istanbul": "^0.10.4", | ||
"gulp-mocha": "^2.2.0", | ||
"gulp-nsp": "^2.4.0", | ||
"gulp-plumber": "^1.1.0" | ||
}, | ||
@@ -40,8 +41,8 @@ "scripts": { | ||
"dependencies": { | ||
"async": "^1.5.2", | ||
"lodash": "^4.5.1", | ||
"oniyi-logger": "^0.3.1", | ||
"request": "2.69.0", | ||
"tough-cookie": "2.2.1" | ||
"async": "^2.0.0-rc.3", | ||
"lodash": "^4.10.0", | ||
"oniyi-logger": "^0.4.2", | ||
"request": "2.71.0", | ||
"tough-cookie": "2.2.2" | ||
} | ||
} |
# oniyi-http-client | ||
[![NPM version][npm-image]][npm-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url] | ||
> Adding a plugin interface to "request" that allows modifications of request parameters and response data | ||
> Adding a plugin interface to [request](https://www.npmjs.com/package/request) that allows modifications of request parameters and response data | ||
@@ -5,0 +5,0 @@ ## Installation |
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
19299
397
0
10
3
+ Addedbl@1.1.2(transitive)
+ Addedoniyi-logger@0.4.2(transitive)
+ Addedqs@6.1.2(transitive)
+ Addedrequest@2.71.0(transitive)
+ Addedtough-cookie@2.2.2(transitive)
- Removedasync@1.5.2(transitive)
- Removedbl@1.0.3(transitive)
- Removedoniyi-logger@0.3.1(transitive)
- Removedqs@6.0.4(transitive)
- Removedrequest@2.69.0(transitive)
- Removedtough-cookie@2.2.1(transitive)
Updatedasync@^2.0.0-rc.3
Updatedlodash@^4.10.0
Updatedoniyi-logger@^0.4.2
Updatedrequest@2.71.0
Updatedtough-cookie@2.2.2