Comparing version 3.0.0 to 3.0.1
@@ -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
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
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
128811
4
10
195
12
1
+ Addedajv@5.5.26.12.6(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedboom@4.3.15.3.3(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedco@4.6.0(transitive)
+ Addedcryptiles@3.2.1(transitive)
+ Addedfast-deep-equal@1.1.03.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedfuel-auth@3.3.0(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.0.35.1.5(transitive)
+ Addedhawk@6.0.2(transitive)
+ Addedhoek@4.3.1(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedjson-schema-traverse@0.3.10.4.1(transitive)
+ Addedlodash.clone@4.5.0(transitive)
+ Addedlodash.merge@4.6.2(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.83.02.88.2(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedsntp@2.1.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
- Removedbluebird@~3.3.5
- Removedlodash.merge@~4.3.5
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedassert-plus@0.2.0(transitive)
- Removedaws-sign2@0.6.0(transitive)
- Removedbl@1.1.2(transitive)
- Removedbluebird@3.3.5(transitive)
- Removedboom@2.10.1(transitive)
- Removedcaseless@0.11.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedcryptiles@2.0.5(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedform-data@2.0.0(transitive)
- Removedfuel-auth@2.1.7(transitive)
- Removedgenerate-function@2.3.1(transitive)
- Removedgenerate-object-property@1.2.0(transitive)
- Removedhar-validator@2.0.6(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedhawk@3.1.3(transitive)
- Removedhoek@2.16.3(transitive)
- Removedhttp-signature@1.1.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedis-my-ip-valid@1.0.1(transitive)
- Removedis-my-json-valid@2.20.6(transitive)
- Removedis-property@1.0.2(transitive)
- Removedisarray@1.0.0(transitive)
- Removedjsonpointer@5.0.1(transitive)
- Removedlodash._baseclone@4.5.7(transitive)
- Removedlodash._stack@4.1.3(transitive)
- Removedlodash.clone@4.3.2(transitive)
- Removedlodash.keysin@4.2.0(transitive)
- Removedlodash.merge@4.3.5(transitive)
- Removedlodash.rest@4.0.5(transitive)
- Removednode-uuid@1.4.8(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@1.0.7(transitive)
- Removedqs@6.2.4(transitive)
- Removedreadable-stream@2.0.6(transitive)
- Removedrequest@2.75.0(transitive)
- Removedsntp@1.0.9(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtunnel-agent@0.4.3(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
Updatedfuel-auth@^3.0.2
Updatedlodash.clone@~4.5.0
Updatedrequest@~2.83.0