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

fuel-rest

Package Overview
Dependencies
Maintainers
2
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 1.0.0 to 2.0.0

README.md

214

lib/fuel-rest.js
/**
* Copyright (c) 2014​, salesforce.com, inc.
* Copyright (c) 2015​, salesforce.com, inc.
* All rights reserved.

@@ -26,13 +26,17 @@ *

*/
'use strict';
var version = require('../package.json').version;
var FuelAuth = require('fuel-auth');
var helpers = require('./helpers');
var Promiser = (typeof Promise === 'undefined') ? require('bluebird') : Promise;
var request = require('request');
var _ = require('lodash');
var FuelAuth = require('fuel-auth');
var Promiser = (typeof Promise === 'undefined') ? require('bluebird') : Promise;
var version = require('../package.json').version;
var clone = require('lodash.clone');
var isPlainObject = require('lodash.isplainobject');
var merge = require('lodash.merge');
var methods = ['get', 'post', 'put', 'patch', 'delete'];
var FuelRest = function(options) {
'use strict';
var authOptions = options && options.auth || {};

@@ -51,144 +55,120 @@

// adding version to object
this.version = version;
// setting up default headers
this.defaultHeaders = _.merge({
this.origin = options.origin || options.restEndpoint || 'https://www.exacttargetapis.com';
this.defaultHeaders = merge({
'User-Agent': 'node-fuel/' + this.version
, 'Content-Type': 'application/json'
}, options.headers);
this.origin = options.origin || options.restEndpoint || 'https://www.exacttargetapis.com';
};
FuelRest.prototype.apiRequest = function(options, callback) {
'use strict';
var self = this;
// we need options
if(!_.isPlainObject(options)) {
if(!isPlainObject(options)) {
throw new TypeError('options argument is required');
}
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(typeof callback === 'function') {
return this._processRequest(options, callback);
}
if(!authResponse.accessToken) {
localError = new Error('No access token');
localError.res = authResponse;
reject(localError);
return;
}
return new Promiser(function(resolve, reject) {
this._processRequest(options, function(err, response) {
if(err) {
return reject(err);
}
resolve(response);
});
}.bind(this));
};
// retry request?
retry = options.retry || false;
authOptions = _.clone(options.auth);
FuelRest.prototype._processRequest = function(options, callback) {
this.AuthClient.getAccessToken(clone(options.auth), function(err, authResponse) {
var authOptions;
var localError;
var retry = false;
var consolidatedOpts = {};
// clean up
delete options.retry;
delete options.auth;
if(err) {
callback(err, null);
return;
}
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(!authResponse.accessToken) {
localError = new Error('No access token');
localError.res = authResponse;
callback(localError, null);
return;
}
request(options, function(err, res, body) {
var parsedBody, restResponse;
authOptions = clone(options.auth);
if(err) {
reject(err);
return;
}
options.uri = helpers.resolveUri(this.origin, options.uri);
options.headers = merge({}, this.defaultHeaders, options.headers);
// check if we should retry req
if(helpers.isValid401(res) && retry) {
options.auth = authOptions;
self.apiRequest(options, callback);
return;
}
if(!options.headers.Authorization) {
options.headers.Authorization = 'Bearer ' + authResponse.accessToken;
retry = options.retry || false;
}
// 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;
}
delete options.retry;
delete options.auth;
// trying to parse body
try {
parsedBody = JSON.parse(body);
} catch(err) {
parsedBody = body;
}
consolidatedOpts.req = options;
consolidatedOpts.auth = authOptions;
consolidatedOpts.accessToken = authResponse.accessToken;
consolidatedOpts.retry = retry;
restResponse = {
res: res
, body: parsedBody
};
helpers.cbRespond('response', restResponse, callback);
resolve(restResponse);
});
});
})
.catch(function(err) {
helpers.cbRespond('error', err, callback);
return err;
});
this._makeRequest(consolidatedOpts, callback);
}.bind(this));
};
FuelRest.prototype.get = function(options, callback) {
'use strict';
FuelRest.prototype._makeRequest = function(consolidatedOpts, callback) {
var requestOptions = consolidatedOpts.req;
options.method = 'GET';
options.retry = true;
request(requestOptions, function(err, res, body) {
var parsedBody;
var isResponseJson;
return this.apiRequest(options, callback);
};
if(err) {
callback(err, null);
return;
}
FuelRest.prototype.post = function(options, callback) {
'use strict';
// check if we should retry req
if(helpers.isValid401(res) && consolidatedOpts.retry) {
this.AuthClient.invalidateToken(consolidatedOpts.accessToken);
requestOptions.auth = consolidatedOpts.auth;
this.apiRequest(requestOptions, callback);
return;
}
options.method = 'POST';
options.retry = true;
isResponseJson = res.headers['content-type'] && res.headers[ 'content-type'].split(';')[ 0].toLowerCase() === 'application/json';
if(!isResponseJson) {
callback(new Error('API did not return JSON'), null);
return;
}
return this.apiRequest(options, callback);
};
// trying to parse body
try {
parsedBody = JSON.parse(body);
} catch(err) {
parsedBody = body;
}
FuelRest.prototype.put = function(options, callback) {
'use strict';
options.method = 'PUT';
options.retry = true;
return this.apiRequest(options, callback);
callback(null, {
res: res
, body: parsedBody
});
}.bind(this));
};
FuelRest.prototype.patch = function(options, callback) {
'use strict';
// setup simple http methods
methods.forEach(function(method) {
FuelRest.prototype[method] = function(options, callback) {
options.method = method.toUpperCase();
options.retry = true;
options.method = 'PATCH';
options.retry = true;
return this.apiRequest(options, callback);
};
});
return this.apiRequest(options, callback);
};
FuelRest.prototype.delete = function(options, callback) {
'use strict';
options.method = 'DELETE';
options.retry = true;
return this.apiRequest(options, callback);
};
module.exports = FuelRest;
'use strict';
var _ = require('lodash');
var url = require('url');
var invalidTypeMsg = 'invalid response type';
module.exports = {

@@ -25,16 +22,2 @@ isValid401: function(res) {

}
, 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') {
callback(data, null);
} else if(type === 'response') {
callback(null, data);
} else {
callback(invalidTypeMsg, null);
}
}
};
{
"name": "fuel-rest",
"version": "1.0.0",
"description": "Node Library for performing REST API calls with ExactTarget Fuel.",
"version": "2.0.0",
"description": "Node library for performing REST API calls to Salesforce Marketing Cloud (formerly ExactTarget).",
"main": "./lib/fuel-rest.js",
"scripts": {
"test": "grunt && ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js"
"test": "grunt && node ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js"
},
"repository": {
"type": "git",
"url": "git@github.com:ExactTarget/Fuel-Node-REST.git"
"url": "git@github.com:salesforcefuel/FuelSDK-Node-REST.git"
},
"author": "Alex Vernacchia <avernacchia@exacttarget.com> (http://babble.byvernacchia.com)",
"license": "BSD-3-Clause",
"contributors": [
"Alex Vernacchia <avernacchia@exacttarget.com> (http://babble.byvernacchia.com)",
"Nathan Boyd <nboyd@salesforce.com>"
],
"devDependencies": {

@@ -22,2 +26,3 @@ "body-parser": "~1.12.0",

"grunt-jscs": "~1.8.0",
"lodash": "~3.10.1",
"mocha": "~2.2.1",

@@ -27,7 +32,9 @@ "sinon": "~1.13.0"

"dependencies": {
"bluebird": "~2.9.25",
"fuel-auth": "~1.0.0",
"lodash": "~3.5.0",
"request": "~2.53.0"
"bluebird": "~3.0.5",
"fuel-auth": "~2.1.0",
"lodash.clone": "~3.0.3",
"lodash.isplainobject": "~3.2.0",
"lodash.merge": "~3.3.2",
"request": "~2.65.0"
}
}

Sorry, the diff of this file is not supported yet

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