Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

superagent-defaults

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

superagent-defaults - npm Package Compare versions

Comparing version 0.1.4 to 0.1.5

superagent-defaults.js

4

component.json

@@ -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": {

]
}
}

@@ -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);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc