Skip to content

Commit

Permalink
Add support for .get/.post/... helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tcort committed May 8, 2016
1 parent 9e29831 commit 0cb9e61
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ You can use the `defaults` method to provide default options like so:
var request = require('requestretry').defaults({ json: true, retryStrategy: myRetryStrategy });
```

## Convenience methods

As with `request`, several helpers are provided for various HTTP methods:

* `request.get(url)` - same as `request(options, callback)` or `request(options)`.
* `request.head(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `HEAD`.
* `request.post(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `POST`.
* `request.put(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `PUT`.
* `request.patch(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `PATCH`.
* `request.del(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `DELETE`.
* `request.delete(url)` - same as `request(options, callback)` or `request(options)`, but it defaults `options.method` to `DELETE`.

## [Changelog](CHANGELOG.md)

## Donate
Expand Down
31 changes: 30 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,41 @@ function Factory(options, f) {
return req;
}

// adds a helper for HTTP method `verb` to object `obj`
function makeHelper(obj, verb) {
obj[verb] = function helper(options, f) {
if (typeof options === 'object') {
options.method = verb.toUpperCase();
} else if (typeof options === 'string') {
options = {
method: verb.toUpperCase(),
uri: options
};
}
return obj(options, f);
};
}

function defaults(defaultOptions, defaultF) {
var factory = function (options, f) {
if (typeof options === "string") {
options = { url: options };
options = { uri: options };
}
return Factory.apply(null, [ _.defaults({}, options, defaultOptions), f || defaultF ]);
};

factory.defaults = function (newDefaultOptions, newDefaultF) {
return defaults.apply(null, [ _.defaults({}, newDefaultOptions, defaultOptions), newDefaultF || defaultF ]);
};

factory.Request = Request;
factory.RetryStrategies = RetryStrategies;

['get', 'head', 'post', 'put', 'patch', 'delete'].forEach(function (verb) {
makeHelper(factory, verb);
});
factory.del = factory['delete'];

return factory;
}

Expand All @@ -158,3 +181,9 @@ module.exports = Factory;
Factory.defaults = defaults;
Factory.Request = Request;
Factory.RetryStrategies = RetryStrategies;

// define .get/.post/... helpers
['get', 'head', 'post', 'put', 'patch', 'delete'].forEach(function (verb) {
makeHelper(Factory, verb);
});
Factory.del = Factory['delete'];
4 changes: 1 addition & 3 deletions test/attempts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ var t = require('chai').assert;

describe('Request attempts', function () {
it('should show 1 attempt after a successful call', function (done) {
request({
url: 'http://www.filltext.com/?rows=1', // return 1 row of data
}, function (err, response, body) {
request.get('http://www.filltext.com/?rows=1', function (err, response, body) {
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.attempts, 1);
done();
Expand Down
7 changes: 4 additions & 3 deletions test/defaults.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('Defaults', function () {
json: true,
qs: { d: "foo" }
});
r({ url: 'http://www.filltext.com/?rows=1', qs: { d: "{index}" } }, function (err, response, body) {
r.get({ url: 'http://www.filltext.com/?rows=1', qs: { d: "{index}" } }, function (err, response, body) {
t.strictEqual(response.statusCode, 200);
t.strictEqual(body[0].d, 1);
done();
Expand All @@ -45,9 +45,10 @@ describe('Defaults', function () {
json: true
});
var level2 = level1.defaults({
qs: { d: "{index}" }
qs: { d: "{index}" },
fullResponse: false
});
level2({ uri: '/?rows=1', fullResponse: false }).then(function (body) {
level2.get('/?rows=1').then(function (body) {
t.strictEqual(body[0].d, 1);
done();
});
Expand Down
97 changes: 97 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
'use strict';

var http = require('http');
var request = require('../').defaults({ json: true });
var t = require('chai').assert;

describe('Helpers', function () {

var server;
var url;

before(function (done) {
server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ method: req.method }));
res.end();
});
server.listen(0, 'localhost', function () {
url = 'http://' + server.address().address + ':' + server.address().port;
done();
});
});

it('should provide .get()', function (done) {
request.get(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'GET');
t.strictEqual(body.method, 'GET');
done();
});
});

it('should provide .head()', function (done) {
request.head(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'HEAD');
t.strictEqual(body, undefined);
done();
});
});

it('should provide .post()', function (done) {
request.post(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'POST');
t.strictEqual(body.method, 'POST');
done();
});
});

it('should provide .put()', function (done) {
request.put(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'PUT');
t.strictEqual(body.method, 'PUT');
done();
});
});

it('should provide .patch()', function (done) {
request.patch(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'PATCH');
t.strictEqual(body.method, 'PATCH');
done();
});
});

it('should provide .delete()', function (done) {
request.delete(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'DELETE');
t.strictEqual(body.method, 'DELETE');
done();
});
});

it('should provide .del()', function (done) {
request.del(url, function (err, resp, body) {
t.strictEqual(resp.statusCode, 200);
t.strictEqual(resp.request.method, 'DELETE');
t.strictEqual(body.method, 'DELETE');
done();
});
});

after(function (done) {
if (server) {
server.close(function () {
done();
});
} else {
done();
}
});

});
2 changes: 1 addition & 1 deletion test/promises.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('Promises support', function () {
});

it('should resolve with just the response body on success', function (done) {
request({
request.get({
url: 'http://www.filltext.com/?rows=1', // return 1 row of data
fullResponse: false, // to resolve with just the response body
})
Expand Down

0 comments on commit 0cb9e61

Please sign in to comment.