-
Notifications
You must be signed in to change notification settings - Fork 6
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
maattam
wants to merge
3
commits into
Vincit:master
Choose a base branch
from
maattam:mm-add-superagent-request-builder
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.