Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add superagent request builder #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/http/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
RangeParser: require('./range-parser').RangeParser,
request: require('./request')
request: require('./request'),
requestBuilder: require('./request-builder').requestBuilder
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought to put this under util and drop the http directory completely. So there would be 2 different request modules at the same time so that people can easily migrate deprecated requests calls to new ones.

};
55 changes: 55 additions & 0 deletions lib/http/request-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
var methods = require('methods')
, _ = require('lodash');

function RequestBuilder (agent) {
this._agent = agent;
this._plugins = [];
}

RequestBuilder.prototype.use = function (plugin) {
this._plugins.push(plugin);
return this;
}

// Create prototype method for each superagent request method.
// Superagent uses similar implementation to generate http request
// verb method names.
var verbs = methods.concat('del');
_.each(verbs, function (verb) {
RequestBuilder.prototype[verb] = function () {
var request = this._agent[verb].apply(this._agent, arguments);

_.each(this._plugins, function (plugin) {
request.use(plugin);
});

return request;
}
});

/**
* Superagent request builder.
*
* Builder allows to chain superagent plugins before calling request
* factory method. Superagent request is created with chained plugins
* when builder's http request factory method is called.
*
* Example:
*
* var requestBuilder = require('dodo/http').requestBuilder
* , superagent = require('superagent')
* , prefix = require('superagent-prefix');
*
* var request = requestBuilder(superagent)
* .use(prefix('http://localhost:8080'));
*
* request.get('/some/resource')
* .then(function (res) {
* console.log(res);
* });
*/
module.exports = {
requestBuilder: function (agent) {
return new RequestBuilder(agent);
}
};
10 changes: 8 additions & 2 deletions lib/utils/TestHelper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var main = require('../app/express/main')
, request = require('../http/request')
, superagent = require('superagent')
, requestBuilder = require('../http').requestBuilder
, path = require('path')
, _ = require('lodash');

Expand All @@ -19,10 +20,15 @@ var main = require('../app/express/main')
*/
function TestHelper(config) {
this.serverUrl = getAppAddress(config);
this.request = request(this.serverUrl);
this.app = main.createApp(config);

var self = this;

this.request = requestBuilder(superagent)
.use(function (req) {
req.url = self.serverUrl + req.url;
});

before(function () {
return main.startApp(self.app);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
classUtils: require('./class-utils'),
mergeConfig: require('./merge-config').mergeConfig,
MultiRequire: require('./multi-require').MultiRequire,
multiRequire: require('./multi-require').multiRequire,
runCommandInPromise: require('./run-command-in-promise').runCommandInPromise,
TestHelper: require('./TestHelper').TestHelper
};
2 changes: 1 addition & 1 deletion lib/utils/multi-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ MultiRequire.prototype.doRequire_ = function(callback) {
* @returns {MultiRequire}
*/
module.exports = {
MultiRequire: function(pathPattern) {
multiRequire: function(pathPattern) {
return new MultiRequire(pathPattern);
}
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"files": [
"README.md",
"LICENSE",
"lib/*"
"lib/*",
"*.js"
],
"dependencies": {
"aws-sdk": "2.1.26",
Expand All @@ -40,10 +41,12 @@
"fs-extra": "0.18.2",
"glob": "5.0.5",
"lodash": "^3.6.0",
"methods": "^1.1.2",
"minimist": "1.1.1",
"moment": "2.10.2",
"mv": "2.0.3",
"node-uuid": "1.4.3",
"superagent": "^2.3.0",
"tv4": "1.1.9",
"tv4-formats": "1.0.0",
"underscore.string": "^3.0.0"
Expand Down
31 changes: 31 additions & 0 deletions tests/http/request-builder.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var _ = require('lodash')
, methods = require('methods')
, expect = require('chai').expect
, requestBuilder = require('../../lib/http').requestBuilder;

describe('request builder', function () {

it('should call use for each plugin when creating request', function () {
var agent = {
args: {}
};

agent.get = function (url) {
agent.args.get = url;
return agent;
}

agent.use = function (plugin) {
agent.args.use = plugin;
return agent;
}

var request = requestBuilder(agent)
.use('foo')
.get('http://example.com');

expect(agent.args.use).to.equal('foo');
expect(agent.args.get).to.equal('http://example.com');
});

});