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 3.0.0 to 3.0.1

gulp/conf/gulp.conf.js

228

lib/fuel-rest.js

@@ -1,2 +0,2 @@

/*
/*
* Copyright (c) 2018, salesforce.com, inc.

@@ -7,150 +7,144 @@ * All rights reserved.

*/
'use strict';
var FuelAuth = require('fuel-auth');
var helpers = require('./helpers');
var Promiser = (typeof Promise === 'undefined') ? require('bluebird') : Promise;
var request = require('request');
var version = require('../package.json').version;
const FuelAuth = require('fuel-auth');
const helpers = require('./helpers');
const request = require('request');
const version = require('../package.json').version;
var clone = require('lodash.clone');
var isPlainObject = require('lodash.isplainobject');
var merge = require('lodash.merge');
const clone = require('lodash.clone');
const isPlainObject = require('lodash.isplainobject');
var methods = ['get', 'post', 'put', 'patch', 'delete'];
const methods = ['get', 'post', 'put', 'patch', 'delete'];
var FuelRest = function(options) {
var authOptions;
class FuelRest {
constructor(options) {
const authOptions = (options && options.auth) || {};
if(!(this instanceof FuelRest)) {
return new FuelRest(options);
// use fuel auth instance if applicable
if (authOptions instanceof FuelAuth) {
this.AuthClient = authOptions;
} else {
try {
this.AuthClient = new FuelAuth(authOptions);
} catch (err) {
throw err;
}
}
this.version = version;
this.origin = options.origin || options.restEndpoint || 'https://www.exacttargetapis.com';
this.defaultHeaders = Object.assign(
{
'User-Agent': `node-fuel/${this.version}`,
'Content-Type': 'application/json'
},
options.headers
);
}
apiRequest(options, callback) {
if (!isPlainObject(options)) {
throw new TypeError('options argument is required');
}
authOptions = options && options.auth || {};
// use fuel auth instance if applicable
if(authOptions instanceof FuelAuth) {
this.AuthClient = authOptions;
} else {
try {
this.AuthClient = new FuelAuth(authOptions);
} catch(err) {
throw err;
if (typeof callback === 'function') {
return this._processRequest(options, callback);
}
}
this.version = version;
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);
};
FuelRest.prototype.apiRequest = function(options, callback) {
if(!isPlainObject(options)) {
throw new TypeError('options argument is required');
return new Promise((resolve, reject) => {
this._processRequest(options, (err, response) => {
if (err) {
return reject(err);
}
resolve(response);
});
});
}
_processRequest(options, callback) {
this.AuthClient.getAccessToken(clone(options.auth), (err, authResponse) => {
let retry = false;
const consolidatedOpts = {};
if(typeof callback === 'function') {
return this._processRequest(options, callback);
}
if (err) {
callback(err, null);
return;
}
return new Promiser(function(resolve, reject) {
this._processRequest(options, function(err, response) {
if(err) {
return reject(err);
if (!authResponse.accessToken) {
let localError = new Error('No access token');
localError.res = authResponse;
callback(localError, null);
return;
}
resolve(response);
});
}.bind(this));
};
FuelRest.prototype._processRequest = function(options, callback) {
this.AuthClient.getAccessToken(clone(options.auth), function(err, authResponse) {
var authOptions;
var localError;
var retry = false;
var consolidatedOpts = {};
const authOptions = clone(options.auth);
if(err) {
callback(err, null);
return;
}
options.uri = helpers.resolveUri(this.origin, options.uri);
options.headers = Object.assign({}, this.defaultHeaders, options.headers);
if(!authResponse.accessToken) {
localError = new Error('No access token');
localError.res = authResponse;
callback(localError, null);
return;
}
if (!options.headers.Authorization) {
options.headers.Authorization = 'Bearer ' + authResponse.accessToken;
retry = options.retry || false;
}
authOptions = clone(options.auth);
delete options.retry;
delete options.auth;
options.uri = helpers.resolveUri(this.origin, options.uri);
options.headers = merge({}, this.defaultHeaders, options.headers);
consolidatedOpts.req = options;
consolidatedOpts.auth = authOptions;
consolidatedOpts.accessToken = authResponse.accessToken;
consolidatedOpts.retry = retry;
if(!options.headers.Authorization) {
options.headers.Authorization = 'Bearer ' + authResponse.accessToken;
retry = options.retry || false;
}
this._makeRequest(consolidatedOpts, callback);
});
}
_makeRequest(consolidatedOpts, callback) {
const requestOptions = consolidatedOpts.req;
delete options.retry;
delete options.auth;
request(requestOptions, (err, res, body) => {
let parsedBody;
let isResponseJson;
consolidatedOpts.req = options;
consolidatedOpts.auth = authOptions;
consolidatedOpts.accessToken = authResponse.accessToken;
consolidatedOpts.retry = retry;
if (err) {
callback(err, null);
return;
}
this._makeRequest(consolidatedOpts, callback);
}.bind(this));
};
// 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;
}
FuelRest.prototype._makeRequest = function(consolidatedOpts, callback) {
var requestOptions = consolidatedOpts.req;
isResponseJson =
res.headers['content-type'] &&
res.headers['content-type'].split(';')[0].toLowerCase() === 'application/json';
request(requestOptions, function(err, res, body) {
var parsedBody;
var isResponseJson;
if (!isResponseJson) {
callback(new Error('API did not return JSON'), null);
return;
}
if(err) {
callback(err, null);
return;
}
// trying to parse body
try {
parsedBody = JSON.parse(body);
} catch (err) {
parsedBody = body;
}
// 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;
}
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;
}
// trying to parse body
try {
parsedBody = JSON.parse(body);
} catch(err) {
parsedBody = body;
}
callback(null, {
res: res
, body: parsedBody
callback(null, {
res: res,
body: parsedBody
});
});
}.bind(this));
};
}
}
// setup simple http methods
methods.forEach(function(method) {
methods.forEach(method => {
FuelRest.prototype[method] = function(options, callback) {
options.method = method.toUpperCase();
options.retry = true;
options.retry = true;

@@ -157,0 +151,0 @@ return this.apiRequest(options, callback);

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

var url = require('url');
const url = require('url');
module.exports = {
isValid401: function(res) {
var is401 = (res.statusCode === 401);
var isFailureFromBadToken = false;
isValid401(res) {
const is401 = res.statusCode === 401;
let isFailureFromBadToken = false;
if(res.headers && res.headers['www-authenticate']) {
if (res.headers && res.headers['www-authenticate']) {
isFailureFromBadToken = /^Bearer\s.+?invalid_token/.test(res.headers['www-authenticate']);

@@ -23,5 +23,5 @@ }

return is401 && isFailureFromBadToken;
}
, resolveUri: function(origin, uri) {
if(origin && uri && !/^http/.test(uri)) {
},
resolveUri(origin, uri) {
if (origin && uri && !/^http/.test(uri)) {
uri = url.resolve(origin, uri);

@@ -28,0 +28,0 @@ }

{
"name": "fuel-rest",
"version": "3.0.0",
"version": "3.0.1",
"description": "Node library for performing REST API calls to Salesforce Marketing Cloud (formerly ExactTarget).",
"main": "./lib/fuel-rest.js",
"scripts": {
"test": "grunt && node ./node_modules/mocha/bin/mocha --reporter=spec test/specs/*.js"
"test": "node ./node_modules/.bin/gulp ci"
},
"engines": {
"node": ">= 4"
},
"repository": {

@@ -20,8 +23,11 @@ "type": "git",

"devDependencies": {
"body-parser": "~1.12.0",
"babel-core": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"body-parser": "~1.18.2",
"chai": "~2.1.1",
"grunt": "~0.4.5",
"grunt-bump": "~0.3.0",
"grunt-contrib-jshint": "~0.11.0",
"grunt-jscs": "~1.8.0",
"gulp": "^3.9.1",
"gulp-jshint": "^2.1.0",
"gulp-mocha": "^5.0.0",
"gulp-workflow": "^1.1.5",
"jshint": "^2.9.5",
"lodash": "~3.10.1",

@@ -32,9 +38,7 @@ "mocha": "~2.2.1",

"dependencies": {
"bluebird": "~3.3.5",
"fuel-auth": "~2.1.6",
"lodash.clone": "~4.3.2",
"fuel-auth": "^3.0.2",
"lodash.clone": "~4.5.0",
"lodash.isplainobject": "~4.0.4",
"lodash.merge": "~4.3.5",
"request": "~2.75.0"
"request": "~2.83.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