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

fuel-rest

Package Overview
Dependencies
Maintainers
3
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fuel-rest - npm Package Compare versions

Comparing version 0.8.0 to 1.0.0

LICENSE

169

lib/fuel-rest.js

@@ -27,10 +27,10 @@ /**

var version = require( '../package.json').version;
var helpers = require( './helpers' );
var request = require( 'request');
var _ = require( 'lodash' );
var FuelAuth = require( 'fuel-auth' );
var version = require('../package.json').version;
var helpers = require('./helpers');
var request = require('request');
var _ = require('lodash');
var FuelAuth = require('fuel-auth');
var Promiser = (typeof Promise === 'undefined') ? require('bluebird') : Promise;
var FuelRest = function( options ) {
var FuelRest = function(options) {
'use strict';

@@ -41,8 +41,8 @@

// use fuel auth instance if applicable
if( authOptions instanceof FuelAuth ) {
if(authOptions instanceof FuelAuth) {
this.AuthClient = authOptions;
} else {
try {
this.AuthClient = new FuelAuth( authOptions );
} catch( err ) {
this.AuthClient = new FuelAuth(authOptions);
} catch(err) {
throw err;

@@ -64,85 +64,88 @@ }

FuelRest.prototype.apiRequest = function( options, callback ) {
FuelRest.prototype.apiRequest = function(options, callback) {
'use strict';
// we need a callback
if( !_.isFunction( callback ) ) {
throw new TypeError( 'callback argument is required' );
}
var self = this;
// we need options
if( !_.isPlainObject( options ) ) {
throw new TypeError( 'options argument is required' );
if(!_.isPlainObject(options)) {
throw new TypeError('options argument is required');
}
this.AuthClient.getAccessToken( _.clone( options.auth ), function( err, body ) {
var localError, retry, authOptions;
return self.AuthClient
.getAccessToken(_.clone(options.auth))
.then(function(authResponse) {
return new Promiser(function(resolve, reject) {
var authOptions;
var jsonRequested;
var localError;
var retry;
if( err ) {
helpers.deliverResponse( 'error', err, callback, 'FuelAuth' );
return;
}
if(!authResponse.accessToken) {
localError = new Error('No access token');
localError.res = authResponse;
reject(localError);
return;
}
// if there's no access token we have a problem
if( !body.accessToken ) {
localError = new Error( 'No access token' );
localError.res = body;
helpers.deliverResponse( 'error', localError, callback, 'FuelAuth' );
return;
}
// retry request?
retry = options.retry || false;
authOptions = _.clone(options.auth);
// retry request?
retry = options.retry || false;
authOptions = _.clone( options.auth );
// clean up
delete options.retry;
delete options.auth;
// clean up
delete options.retry;
delete options.auth;
options.uri = helpers.resolveUri(self.origin, options.uri);
options.headers = _.merge({}, self.defaultHeaders, options.headers);
options.headers.Authorization = options.headers.Authorization || 'Bearer ' + authResponse.accessToken;
// if we don't have a fully qualified URL let's make one
options.uri = helpers.resolveUri( this.origin, options.uri );
request(options, function(err, res, body) {
var parsedBody, restResponse;
// merge headers
options.headers = _.merge( {}, this.defaultHeaders, options.headers );
if(err) {
reject(err);
return;
}
// adding the bearer token
options.headers.Authorization = options.headers.Authorization || 'Bearer ' + body.accessToken;
// check if we should retry req
if(helpers.isValid401(res) && retry) {
options.auth = authOptions;
self.apiRequest(options, callback);
return;
}
// send request to api
request( options, function( err, res, body ) {
var parsedBody;
// checking to make sure it's json from api
jsonRequested = res.headers['content-type'] && res.headers[ 'content-type'].split(';')[ 0].toLowerCase() === 'application/json';
if(!jsonRequested) {
localError = new Error('API did not return JSON');
helpers.cbRespond('error', localError, callback);
reject(err);
return;
}
if( err ) {
helpers.deliverResponse( 'error', err, callback, 'Request Module inside apiRequest' );
return;
}
// trying to parse body
try {
parsedBody = JSON.parse(body);
} catch(err) {
parsedBody = body;
}
// check if we should retry req
if( helpers.check401header( res ) && !!retry ) {
options.auth = authOptions;
this.apiRequest( options, callback );
return;
}
restResponse = {
res: res
, body: parsedBody
};
// checking to make sure it's json from api
if( !res.headers[ 'content-type' ] || res.headers[ 'content-type' ].split( ';' )[ 0 ].toLowerCase() !== 'application/json' ) {
helpers.deliverResponse( 'error', new Error( 'API did not return JSON' ), callback, 'Fuel REST' );
return;
}
// trying to parse body
try {
parsedBody = JSON.parse( body );
} catch( err ) {
parsedBody = body;
}
helpers.deliverResponse( 'response', { res: res, body: parsedBody }, callback );
}.bind( this ) );
}.bind( this ) );
helpers.cbRespond('response', restResponse, callback);
resolve(restResponse);
});
});
})
.catch(function(err) {
helpers.cbRespond('error', err, callback);
return err;
});
};
FuelRest.prototype.get = function( options, callback ) {
FuelRest.prototype.get = function(options, callback) {
'use strict';

@@ -153,6 +156,6 @@

this.apiRequest( options, callback );
return this.apiRequest(options, callback);
};
FuelRest.prototype.post = function( options, callback ) {
FuelRest.prototype.post = function(options, callback) {
'use strict';

@@ -163,6 +166,6 @@

this.apiRequest( options, callback );
return this.apiRequest(options, callback);
};
FuelRest.prototype.put = function( options, callback ) {
FuelRest.prototype.put = function(options, callback) {
'use strict';

@@ -173,6 +176,6 @@

this.apiRequest( options, callback );
return this.apiRequest(options, callback);
};
FuelRest.prototype.patch = function( options, callback ) {
FuelRest.prototype.patch = function(options, callback) {
'use strict';

@@ -183,6 +186,6 @@

this.apiRequest( options, callback );
return this.apiRequest(options, callback);
};
FuelRest.prototype.delete = function( options, callback ) {
FuelRest.prototype.delete = function(options, callback) {
'use strict';

@@ -193,5 +196,5 @@

this.apiRequest( options, callback );
return this.apiRequest(options, callback);
};
module.exports = FuelRest;
'use strict';
var url = require( 'url' );
var _ = require('lodash');
var url = require('url');
var invalidTypeMsg = 'invalid response type';
module.exports = {
check401header: function( res ) {
var is401 = res.statusCode === 401;
var isFailureFromBadToken = /^Bearer\s.+?invalid_token/.test( res.headers[ 'www-authenticate' ] );
isValid401: function(res) {
var is401 = (res.statusCode === 401);
var isFailureFromBadToken = false;
if(res.headers && res.headers['www-authenticate']) {
isFailureFromBadToken = /^Bearer\s.+?invalid_token/.test(res.headers['www-authenticate']);
}
return is401 && isFailureFromBadToken;
}
, deliverResponse: function( type, data, callback, errorFrom ) {
, resolveUri: function(origin, uri) {
if(origin && uri && !/^http/.test(uri)) {
uri = url.resolve(origin, uri);
}
return uri;
}
, cbRespond: function(type, data, callback) {
if(!_.isFunction(callback)) {
return;
}
// if it's an error and we have where it occured, let's tack it on
if( type === 'error' ) {
if( !!errorFrom ) {
data.errorPropagatedFrom = errorFrom;
}
callback( data, null );
} else if( type === 'response' ) {
callback( null, data );
if(type === 'error') {
callback(data, null);
} else if(type === 'response') {
callback(null, data);
} else {
callback(invalidTypeMsg, null);
}
}
, resolveUri: function( origin, uri ) {
if( !/^http/.test( uri ) ) {
uri = url.resolve( origin, uri );
}
return uri;
}
};
{
"name": "fuel-rest",
"version": "0.8.0",
"version": "1.0.0",
"description": "Node Library for performing REST API calls with ExactTarget Fuel.",
"main": "./lib/fuel-rest.js",
"scripts": {
"test": "grunt jshint && ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js"
"test": "grunt && ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js"
},

@@ -14,14 +14,15 @@ "repository": {

"author": "Alex Vernacchia <avernacchia@exacttarget.com> (http://babble.byvernacchia.com)",
"license" : "BSD-3-Clause",
"license": "BSD-3-Clause",
"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",
"grunt-jscs": "~1.8.0",
"mocha": "~2.2.1",
"promise": "~6.1.0",
"sinon": "~1.13.0"
},
"dependencies": {
"bluebird": "~2.9.25",
"fuel-auth": "~1.0.0",

@@ -28,0 +29,0 @@ "lodash": "~3.5.0",

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