diff --git a/index.js b/index.js index f028680..7dddf08 100644 --- a/index.js +++ b/index.js @@ -17,10 +17,16 @@ module.exports = function(_superagent) { } methods.forEach(function(method) { superagent[method] = function() { - var request = _superagent[method].apply(superagent, arguments); + var args = [].slice.call(arguments), cb; + if (typeof args[args.length-1] === 'function') { + cb = args[args.length-1]; + args = args.slice(0, -1); + } + var request = _superagent[method].apply(superagent, args); uses.forEach(function(use) { request = request.use(use); }) + if (cb) request.end(cb) return request; }; }); diff --git a/test/index.js b/test/index.js index 035dcfd..5624ed5 100644 --- a/test/index.js +++ b/test/index.js @@ -44,4 +44,14 @@ describe('superagent-use', function() { withPrefix.get('/').request()._headers.host.should.equal('example.com'); withoutPrefix.get('/').request()._headers.host.should.not.equal('example.com'); }); + + it('should work with method(...args, cb)', function(done) { + var agent = require('..')(superagent) + .use(req => (req.end = fn => fn(null, 'foo'), req)); + + agent.get('bar', function(err, res) { + res.should.equal('foo'); + done(); + }); + }); });