├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json └── test └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "node" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Koen Punt 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # superagent-use [![Build Status](https://travis-ci.org/koenpunt/superagent-use.svg?branch=master)](https://travis-ci.org/koenpunt/superagent-use) 2 | 3 | Global plugin support for [SuperAgent](https://github.com/visionmedia/superagent/). 4 | 5 | ## Summary 6 | 7 | Instead of manually calling `use()` for every request, `use()` is called automatically for every request. 8 | 9 | ## Example 10 | 11 | ```js 12 | /* The superagent-use module returns a clone of the superagent provided with the new functionality. */ 13 | var agent = require('superagent-use')(require('superagent')); 14 | /* A sample superagent plugin/middleware. */ 15 | var prefix = require('superagent-prefix'); 16 | 17 | agent.use(prefix('https://api.example.com')); 18 | 19 | agent 20 | .post('/auth') 21 | .send({user: 'foo', pass: 'bar123'}) 22 | .on('request', function(req) { 23 | console.log(req.url); // => https://api.example.com/auth 24 | }) 25 | .end(function(err, res) { 26 | // 27 | }); 28 | ``` 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var methods = require('methods'); 2 | var extend = require('extend'); 3 | 4 | module.exports = function(_superagent) { 5 | var superagent = extend({}, _superagent); 6 | 7 | var uses = []; 8 | 9 | superagent.use = function(fn) { 10 | uses.push(fn); 11 | return this; 12 | }; 13 | 14 | if(methods.indexOf('del') === -1) { 15 | methods = methods.slice(0); 16 | methods.push('del'); 17 | } 18 | methods.forEach(function(method) { 19 | superagent[method] = function() { 20 | var request = _superagent[method].apply(superagent, arguments); 21 | uses.forEach(function(use) { 22 | request = request.use(use); 23 | }) 24 | return request; 25 | }; 26 | }); 27 | 28 | return superagent; 29 | }; 30 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "superagent-use", 3 | "version": "0.1.0", 4 | "description": "Global plugin support for SuperAgent", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "NODE_ENV=test mocha" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/koenpunt/superagent-use" 12 | }, 13 | "keywords": [ 14 | "superagent", 15 | "use", 16 | "global", 17 | "plugins" 18 | ], 19 | "author": "Koen Punt ", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/koenpunt/superagent-use/issues" 23 | }, 24 | "homepage": "https://github.com/koenpunt/superagent-use.git", 25 | "devDependencies": { 26 | "mocha": "~2.4.5", 27 | "should": "~8.2.2", 28 | "superagent": "~2.2.0" 29 | }, 30 | "dependencies": { 31 | "extend": "^3.0.0", 32 | "methods": "~1.1.2" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var should = require('should'); 2 | var superagent = require('superagent'); 3 | 4 | describe('superagent-use', function() { 5 | var agent; 6 | 7 | var prefix = 'http://example.com'; 8 | var prefixMiddleware = function(req) { 9 | if(req.url[0] === '/') { 10 | req.url = prefix + req.url; 11 | } 12 | return req; 13 | }; 14 | 15 | beforeEach(function() { 16 | agent = require('..')(superagent); 17 | }); 18 | 19 | it('should apply plugin to all requests', function() { 20 | agent 21 | .use(prefixMiddleware); 22 | 23 | var req1 = agent.get('/'); 24 | req1.request()._headers.host.should.equal('example.com'); 25 | 26 | var req2 = agent.patch('/update'); 27 | req2.request()._headers.host.should.equal('example.com'); 28 | }); 29 | 30 | it('should be chainable', function() { 31 | var req = agent 32 | .use(prefixMiddleware) 33 | .get('/'); 34 | 35 | req.request()._headers.host.should.equal('example.com'); 36 | }); 37 | 38 | it('should return a new instance of superagent', function() { 39 | var withPrefix = require('..')(superagent); 40 | withoutPrefix = require('..')(superagent); 41 | 42 | withPrefix.use(prefixMiddleware); 43 | 44 | withPrefix.get('/').request()._headers.host.should.equal('example.com'); 45 | withoutPrefix.get('/').request()._headers.host.should.not.equal('example.com'); 46 | }); 47 | }); 48 | --------------------------------------------------------------------------------