Adding a plugin interface to "request" that allows modifications of request parameters and response data.
Why?
Working with the popular module request for your http requests is awesome. But sometimes you might need to go beyond the standard features.
Install
$ npm install --save oniyi-http-client
Usage
var tough = require('tough-cookie');
var OniyiHttpClient = require('oniyi-http-client');
var client = new OniyiHttpClient();
client.registerPlugin('async-cookie-jar')
.registerPlugin({
name: 'plugin-1',
onRequest: function(params) {
params.headers = params.headers || {};
params.headers.foo = 'bar';
return params;
}
})
.registerPlugin({
name: 'plugin-2',
storesData: true,
onRequest: function(params, store, callback) {
setTimeout(function() {
params.headers = params.headers || {};
params.headers['plugin-2'] = 'plugin-2';
store.name = 'Bam Bam!';
callback(null, params);
}, 500);
},
callback: function(next, store, err, response, body) {
console.log('Name in this plugin\'s store: %s', store.name);
next.call(this, err, response, body);
}
});
client.makeRequest('https://www.npmjs.com', {
jar: new tough.CookieJar(asyncStore)
}, function(err, response, body){
});
License
MIT © Benjamin Kroeger