superagent-defaults
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -7,2 +7,3 @@ /** | ||
var methods = require('./methods'); | ||
var protoMethods = Object.keys(request.Request.prototype); | ||
@@ -23,31 +24,26 @@ /** | ||
if (!(this instanceof Context)) return new Context(superagent); | ||
this.headers = []; | ||
this.authCredentials = {}; | ||
this.request = superagent || request; | ||
this.stack = []; // store the default operation on the context | ||
} | ||
/** | ||
* Set default auth credentials | ||
* | ||
* @api public | ||
*/ | ||
Context.prototype.auth = function (user, pass) { | ||
this.authCredentials.user = user; | ||
this.authCredentials.pass = pass; | ||
}; | ||
// setup methods for context | ||
/** | ||
* Add a default header to the context | ||
* | ||
* @api public | ||
*/ | ||
each(protoMethods, function(m) { | ||
if (methods.indexOf(m) > -1) { // m is a HttpVerb method , don't record operation | ||
return; | ||
} | ||
Context.prototype.set = function() { | ||
this.headers.push(arguments); | ||
return this; | ||
}; | ||
Context.prototype[m] = function() { | ||
this.stack.push({ | ||
method: m, | ||
args: [].slice.call(arguments) | ||
}); | ||
return this; | ||
} | ||
}); | ||
/** | ||
* Set the default headers on the req | ||
* apply the operations on the context to real Request instance | ||
* | ||
@@ -57,5 +53,5 @@ * @api private | ||
Context.prototype.applyHeaders = function(req) { | ||
each(this.headers, function(header) { | ||
req.set.apply(req, header); | ||
Context.prototype.applyStack = function(req) { | ||
this.stack.forEach(function(op){ // op -> operation | ||
req[op.method].apply(req,op.args); | ||
}); | ||
@@ -72,12 +68,6 @@ }; | ||
var req = this.request(method, url); | ||
var auth = this.authCredentials; | ||
// Do the attaching here | ||
this.applyHeaders(req); | ||
this.applyStack(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 | ||
@@ -84,0 +74,0 @@ this.emit('request', req); |
{ | ||
"name": "superagent-defaults", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "Create some defaults for superagent requests", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
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
0
5977
178