Skip to content

Commit

Permalink
Merge pull request request#1030 from tikotzky/add-recursive-defaults
Browse files Browse the repository at this point in the history
Allow recursive request.defaults
  • Loading branch information
nylen committed Aug 28, 2014
2 parents af75f93 + aa5395e commit 38c534d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,22 @@ There are also shorthand methods for different HTTP METHODs and some other conve

This method returns a wrapper around the normal request API that defaults to whatever options you pass in to it.

**Note:** You can call `.defaults()` on the wrapper that is returned from `request.defaults` to add/override defaults that were previously defaulted.

For example:
```javascript
//requests using baseRequest() will set the 'x-token' header
var baseRequest = request.defaults({
headers: {x-token: 'my-token'}
})

//requests using specialRequest() will include the 'x-token' header set in
//baseRequest and will also include the 'special' header
var specialRequest = baseRequest.defaults({
headers: {special: 'special value'}
})
```

### request.put

Same as `request()`, but defaults to `method: "PUT"`.
Expand Down
20 changes: 11 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,17 @@ request.defaults = function (options, requester) {
}
return d
}
var de = def(request)
de.get = def(request.get)
de.patch = def(request.patch)
de.post = def(request.post)
de.put = def(request.put)
de.head = def(request.head)
de.del = def(request.del)
de.cookie = def(request.cookie)
de.jar = request.jar

var de = def(this)
de.get = def(this.get)
de.patch = def(this.patch)
de.post = def(this.post)
de.put = def(this.put)
de.head = def(this.head)
de.del = def(this.del)
de.cookie = def(this.cookie)
de.jar = this.jar
de.defaults = this.defaults
return de
}

Expand Down
31 changes: 31 additions & 0 deletions tests/test-defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,37 @@ s.listen(s.port, function () {
counter += 1;
});

s.on('/get_recursive1', function (req, resp) {
assert.equal(req.headers.foo, 'bar1');
assert.equal(req.method, 'GET');
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end('TESTING!');
});

s.on('/get_recursive2', function (req, resp) {
assert.equal(req.headers.foo, 'bar1');
assert.equal(req.headers.baz, 'bar2');
assert.equal(req.method, 'GET');
resp.writeHead(200, {'Content-Type': 'text/plain'});
resp.end('TESTING!');
});

// test recursive defaults (string, function)
var defaultsOne = request.defaults({headers:{foo:"bar1"}});
var defaultsTwo = defaultsOne.defaults({headers:{baz:"bar2"}});

defaultsOne(s.url + '/get_recursive1', function (e, r, b){
if (e) throw e;
assert.deepEqual("TESTING!", b);
counter += 1;
});

defaultsTwo(s.url + '/get_recursive2', function (e, r, b){
if (e) throw e;
assert.deepEqual("TESTING!", b);
counter += 1;
});

s.on('/get_custom', function(req, resp) {
assert.equal(req.headers.foo, 'bar');
assert.equal(req.headers.x, 'y');
Expand Down

0 comments on commit 38c534d

Please sign in to comment.