superagent-defaults
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -5,3 +5,3 @@ { | ||
"description": "Create some defaults for superagent requests", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"keywords": [], | ||
@@ -19,2 +19,2 @@ "dependencies": { | ||
] | ||
} | ||
} |
66
index.js
@@ -7,12 +7,12 @@ /** | ||
var request = require('superagent') | ||
, Emitter = require('emitter') | ||
, methods = require('methods', './methods'); | ||
var request = require('superagent'); | ||
var Emitter = require('emitter'); | ||
var methods = require('methods', './methods'); | ||
/** | ||
* Expose `Context`. | ||
*/ | ||
module.exports = Context; | ||
/** | ||
@@ -23,31 +23,43 @@ * Initialize a new `Context`. | ||
*/ | ||
function Context() { | ||
if (!(this instanceof Context)) return new Context; | ||
if (!(this instanceof Context)) return new Context(); | ||
this.headers = []; | ||
this.authCredentials = {}; | ||
} | ||
/** | ||
* Inherit from `Emitter` | ||
*/ | ||
Emitter(Context.prototype); | ||
/** | ||
* Set default auth credentials | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.auth = function (user, pass) { | ||
this.authCredentials.user = user; | ||
this.authCredentials.pass = pass; | ||
}; | ||
/** | ||
* Add a default header to the context | ||
* | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.set = function() { | ||
this.headers.push(arguments); | ||
return this; | ||
} | ||
}; | ||
/** | ||
* Set the default headers on the req | ||
* | ||
* | ||
* @api private | ||
*/ | ||
Context.prototype.applyHeaders = function(req) { | ||
@@ -57,19 +69,25 @@ each(this.headers, function(header) { | ||
}); | ||
} | ||
}; | ||
// generate HTTP verb methods | ||
each(methods, function(method){ | ||
var name = 'delete' == method ? 'del' : method; | ||
method = method.toUpperCase(); | ||
Context.prototype[name] = function(url, fn){ | ||
var req = request(method, url); | ||
var auth = this.authCredentials; | ||
// Do the attaching here | ||
this.applyHeaders(req); | ||
// Call superagent's auth method | ||
if(auth.hasOwnProperty('user') && auth.hasOwnProperty('pass')) { | ||
req.auth(auth.user, auth.pass); | ||
} | ||
// Tell the listeners we've created a new request | ||
this.emit('request', req); | ||
fn && req.end(fn); | ||
@@ -76,0 +94,0 @@ return req; |
{ | ||
"name": "superagent-defaults", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Create some defaults for superagent requests", | ||
@@ -27,3 +27,4 @@ "main": "index.js", | ||
"should": "~1.2.2", | ||
"mocha": "~1.11.0" | ||
"mocha": "~1.11.0", | ||
"superagent": "*" | ||
}, | ||
@@ -30,0 +31,0 @@ "peerDependencies": { |
@@ -18,2 +18,3 @@ superagent-defaults [![Build Status](https://travis-ci.org/CamShaft/superagent-defaults.png)](https://travis-ci.org/CamShaft/superagent-defaults) | ||
.set('my-default-header', 'my-default-value') | ||
.auth('myUsername', 'myPassword') | ||
.on(function (req){ | ||
@@ -20,0 +21,0 @@ console.log(req.url); |
/** | ||
* Module dependencies | ||
*/ | ||
var should = require('should') | ||
, context = require('..'); | ||
var should = require('should'); | ||
var context = require('..'); | ||
describe('superagent-context', function() { | ||
@@ -36,2 +37,12 @@ | ||
it('should apply default auth', function() { | ||
superagent | ||
.auth('abc','cde'); | ||
var req = superagent.get('http://example.com'); | ||
var expectedAuthHeader = 'Basic ' + (new Buffer('abc:cde')).toString('base64'); | ||
req.request()._headers.authorization.should.equal(expectedAuthHeader); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35976
9
1442
38
3
7
1