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

Refactor defaults #1520

Merged
merged 3 commits into from Apr 5, 2015
Merged

Refactor defaults #1520

merged 3 commits into from Apr 5, 2015

Conversation

simov
Copy link
Member

@simov simov commented Mar 30, 2015

This is a follow up to #1518

Much more simplified defaults and params initialization in index.js. Basically flattened the logic, also moved the wrap closure outside of the defaults method and I left it in index.js because I think it's very closely related to the request method, and the convenience http verb methods. Also completely dropped all of the object extend closures, as I don't think they add any substantial value to that piece of code.

/cc @nylen @FredKSchott @mikeal

Simplify defaults method +
Move wrap closure outside of the defaults method
@@ -73,77 +81,65 @@ request.cookie = function (str) {
return cookies.parse(str)
}

request.defaults = function (options, requester) {
function wrap (method, options, requester) {
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap is a little vague, it took me a while to understand what this function was doing. Is there a better name here that could provide a little more context? wrapDefaults? wrapRequestMethods?

Also, some documentation on expected arguments would be equally helpful, especially since it looks like we support a lot of different ways to call this (method = string, function, or request, request is optional, etc).

@FredKSchott
Copy link
Contributor

👍 with comments

@simov
Copy link
Member Author

simov commented Apr 1, 2015

I managed to squeeze the code a bit further by removing redundant data structure returned by initParams

Regarding your comment @FredKSchott about passing a string to the wrapRequestMehod function, I can't figure out a good way to remove the string parameter from there without introducing complexity in another place.

The reason for passing string there is to be able to set the params.method which is later used in the request function to abort the request on HEAD + body.

Similar to the way the http verb methods set the method name after calling initParams, the wrapRequestMethod is doing exactly the same, again after the initParams was called.

Here is the catch - on wrapped http verb method + requester, the default http verb method is never called, because the requester takes precedence. Therefore the request method is never set, so the request can't be aborted in the request function (and we have a test for that too).

simov added a commit that referenced this pull request Apr 5, 2015
@simov simov merged commit cfe9f5d into request:master Apr 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants