Comparing version 1.0.1 to 2.0.0
/** | ||
* Copyright (c) 2014, salesforce.com, inc. | ||
* Copyright (c) 2015, salesforce.com, inc. | ||
* All rights reserved. | ||
@@ -27,21 +27,21 @@ * | ||
var _ = require( 'lodash' ); | ||
var PromisePoly = require( 'bluebird' ); | ||
var request = require( 'request' ); | ||
var version = require( '../package.json').version; | ||
var _ = require('lodash'); | ||
var Promise = (typeof Promise === 'undefined') ? require('bluebird') : Promise; | ||
var request = require('request'); | ||
var version = require('../package.json').version; | ||
function FuelAuth ( options ) { | ||
function FuelAuth(options) { | ||
'use strict'; | ||
//make sure clientId and clientSecret are available and not empty | ||
if( Boolean( options ) ) { | ||
if( !options.clientId || !options.clientSecret ) { | ||
throw new Error( 'clientId or clientSecret is missing or invalid' ); | ||
if(Boolean(options)) { | ||
if(!options.clientId || !options.clientSecret) { | ||
throw new Error('clientId or clientSecret is missing or invalid'); | ||
} | ||
if( !_.isString( options.clientId ) || !_.isString( options.clientSecret ) ) { | ||
throw new Error( 'clientId or clientSecret must be strings' ); | ||
if(!_.isString(options.clientId) || !_.isString(options.clientSecret)) { | ||
throw new Error('clientId or clientSecret must be strings'); | ||
} | ||
} else { | ||
throw new Error( 'options are required. see readme.' ); | ||
throw new Error('options are required. see readme.'); | ||
} | ||
@@ -55,3 +55,2 @@ | ||
this.expiration = null; | ||
this.Promiser = (typeof Promise === 'undefined') ? PromisePoly : Promise; | ||
this.refreshToken = options.refreshToken; | ||
@@ -62,26 +61,35 @@ this.scope = options.scope; | ||
FuelAuth.prototype.getAccessToken = function( options, callback ) { | ||
FuelAuth.prototype.getAccessToken = function(options, callback) { | ||
'use strict'; | ||
var done = callback; | ||
var getNewToken; | ||
var requestOptions = {}; | ||
if( _.isFunction( options ) ) { | ||
callback = options; | ||
} else if( _.isPlainObject( options ) ) { | ||
requestOptions = options; | ||
options = options || {}; | ||
if(typeof options === 'function') { | ||
done = options; | ||
options = {}; | ||
} | ||
getNewToken = this.isExpired() || (requestOptions.force || false); | ||
if(done !== undefined && typeof done !== 'function') { | ||
throw new TypeError('argument callback must be a function'); | ||
} | ||
delete requestOptions.force; | ||
getNewToken = this.isExpired() || (options.force || false); | ||
if(!callback) { | ||
return new this.Promiser(function(resolve, reject) { | ||
this._processRequest(getNewToken, requestOptions, null, { resolve: resolve, reject: reject }); | ||
}.bind(this)); | ||
delete options.force; | ||
if(done) { | ||
return this._processRequest(getNewToken, options, done); | ||
} | ||
// well we have a callback it looks like | ||
this._processRequest(getNewToken, requestOptions, callback, null); | ||
return new Promise(function(resolve, reject) { | ||
this._processRequest(getNewToken, options, function(err, data) { | ||
if(err) { | ||
return reject(err); | ||
} | ||
resolve(data); | ||
}); | ||
}.bind(this)); | ||
}; | ||
@@ -94,3 +102,3 @@ | ||
// if current atomic time is equal or after exp, or we don't have a token, return true | ||
if( ( this.expiration && this.expiration <= process.hrtime()[0] ) || !this.accessToken ) { | ||
if((this.expiration && this.expiration <= process.hrtime()[0]) || !this.accessToken) { | ||
expired = true; | ||
@@ -102,19 +110,4 @@ } | ||
FuelAuth.prototype._deliver = function(data, err, cb, promise) { | ||
FuelAuth.prototype._processRequest = function(getNewToken, options, callback) { | ||
'use strict'; | ||
if(cb) { | ||
if(err) { | ||
cb(data, null); | ||
} else { | ||
cb(null, data); | ||
} | ||
} else if(promise) { | ||
promise(data); | ||
} else { | ||
throw new Error('You are using this wrong'); | ||
} | ||
}; | ||
FuelAuth.prototype._processRequest = function(getNewToken, options, callback, promiseObj) { | ||
'use strict'; | ||
var data; | ||
@@ -125,7 +118,7 @@ | ||
.then(function(body) { | ||
this._deliver(body, false, callback, promiseObj && promiseObj.resolve); | ||
}.bind(this)) | ||
callback(null, body); | ||
}) | ||
.catch(function(err) { | ||
this._deliver(err, true, callback, promiseObj && promiseObj.reject); | ||
}.bind(this)); | ||
callback(err, null); | ||
}); | ||
} else { | ||
@@ -137,7 +130,7 @@ data = { | ||
this._deliver(data, false, callback, promiseObj && promiseObj.resolve); | ||
callback(null, data); | ||
} | ||
}; | ||
FuelAuth.prototype._requestToken = function( requestOptions ) { | ||
FuelAuth.prototype._requestToken = function(requestOptions) { | ||
'use strict'; | ||
@@ -157,8 +150,8 @@ | ||
_.merge( options, requestOptions ); // deepMerge options received from getAccessToken if they're there | ||
_.merge(options, requestOptions); | ||
if( this.refreshToken ) { | ||
if(this.refreshToken) { | ||
// adding refresh token to json if it's there | ||
options.json.refreshToken = this.refreshToken; | ||
} else if ( this.scope ) { | ||
} else if (this.scope) { | ||
// adding scope to json if it's there | ||
@@ -169,12 +162,10 @@ // it's not valid to use both scope and a refresh token | ||
return new this.Promiser(function(resolve, reject) { | ||
request( options, function ( err, res, body ) { | ||
if( err ) { | ||
reject(err); | ||
return; | ||
return new Promise(function(resolve, reject) { | ||
request(options, function (err, res, body) { | ||
if(err) { | ||
return reject(err); | ||
} | ||
// setting variables on object created to be used later | ||
if( body && body.refreshToken ) { | ||
if(body && body.refreshToken) { | ||
this.refreshToken = body.refreshToken; | ||
@@ -184,11 +175,10 @@ } | ||
this.accessToken = body.accessToken || null; | ||
this.expiration = ( body.expiresIn ) ? process.hrtime()[0] + body.expiresIn : null; | ||
this.expiration = (body.expiresIn) ? process.hrtime()[0] + body.expiresIn : null; | ||
resolve(body); | ||
}.bind( self ) ); // binding function to FuelAuthClient so we can have a good context inside callback | ||
}.bind(self)); | ||
}); | ||
}; | ||
// exporting module | ||
module.exports = FuelAuth; |
{ | ||
"name": "fuel-auth", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Node Library for Authenticating with ExactTarget Fuel. Used for authenticating REST and SOAP APIs.", | ||
"main": "./lib/fuel-auth.js", | ||
"scripts": { | ||
"test": "grunt jshint && ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js" | ||
"test": "grunt jshint && node ./node_modules/mocha/bin/mocha --reporter=spec test/*.js" | ||
}, | ||
@@ -21,8 +21,7 @@ "repository": { | ||
"devDependencies": { | ||
"body-parser": "~1.12.0", | ||
"chai": "~2.1.1", | ||
"grunt": "~0.4.5", | ||
"grunt-bump": "~0.3.0", | ||
"grunt-contrib-jshint": "~0.11.0", | ||
"grunt": "~0.4.5", | ||
"mocha": "~2.2.1", | ||
"nock": "~2.6.0", | ||
"sinon": "~1.13.0" | ||
@@ -29,0 +28,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
6
0
76799
525
1
0