Comparing version 1.0.0 to 2.0.0
/** | ||
* 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
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a 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
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
13939
5
0
140
6
9
162
1
+ Addedlodash.clone@~3.0.3
+ Addedlodash.isplainobject@~3.2.0
+ Addedlodash.merge@~3.3.2
+ Addedansi-regex@2.1.1(transitive)
+ Addedansi-styles@2.2.1(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@0.2.01.0.0(transitive)
+ Addedasync@2.6.4(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.6.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedbl@1.0.31.1.2(transitive)
+ Addedbluebird@3.0.63.3.5(transitive)
+ Addedcaseless@0.11.0(transitive)
+ Addedchalk@1.1.3(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedextend@3.0.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@1.0.12.0.0(transitive)
+ Addedfuel-auth@2.1.7(transitive)
+ Addedgenerate-function@2.3.1(transitive)
+ Addedgenerate-object-property@1.2.0(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-validator@2.0.6(transitive)
+ Addedhas-ansi@2.0.0(transitive)
+ Addedhawk@3.1.3(transitive)
+ Addedhttp-signature@0.11.01.1.1(transitive)
+ Addedis-my-ip-valid@1.0.1(transitive)
+ Addedis-my-json-valid@2.20.6(transitive)
+ Addedis-property@1.0.2(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjsonpointer@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedlodash._arraycopy@3.0.0(transitive)
+ Addedlodash._arrayeach@3.0.0(transitive)
+ Addedlodash._baseassign@3.2.0(transitive)
+ Addedlodash._baseclone@3.3.04.5.7(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basefor@3.0.3(transitive)
+ Addedlodash._bindcallback@3.0.1(transitive)
+ Addedlodash._createassigner@3.1.1(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash._stack@4.1.3(transitive)
+ Addedlodash.clone@3.0.3(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.isplainobject@3.2.04.0.6(transitive)
+ Addedlodash.istypedarray@3.0.6(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlodash.keysin@3.0.84.2.0(transitive)
+ Addedlodash.merge@3.3.24.3.5(transitive)
+ Addedlodash.rest@4.0.5(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedlodash.toplainobject@3.0.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.8.2(transitive)
+ Addedpinkie@2.0.4(transitive)
+ Addedpinkie-promise@2.0.1(transitive)
+ Addedprocess-nextick-args@1.0.7(transitive)
+ Addedpunycode@1.4.1(transitive)
+ Addedqs@5.2.16.2.4(transitive)
+ Addedreadable-stream@2.0.6(transitive)
+ Addedrequest@2.65.02.75.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedstrip-ansi@3.0.1(transitive)
+ Addedsupports-color@2.0.0(transitive)
+ Addedtough-cookie@2.2.22.3.4(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedverror@1.10.0(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedlodash@~3.5.0
- Removedasync@0.9.2(transitive)
- Removedaws-sign2@0.5.0(transitive)
- Removedbl@0.9.5(transitive)
- Removedbluebird@2.9.34(transitive)
- Removedcaseless@0.9.0(transitive)
- Removedcombined-stream@0.0.7(transitive)
- Removeddelayed-stream@0.0.5(transitive)
- Removedforever-agent@0.5.2(transitive)
- Removedform-data@0.2.0(transitive)
- Removedfuel-auth@1.0.1(transitive)
- Removedhawk@2.3.1(transitive)
- Removedhttp-signature@0.10.1(transitive)
- Removedisarray@0.0.1(transitive)
- Removedlodash@3.5.0(transitive)
- Removedmime-db@1.12.0(transitive)
- Removedmime-types@2.0.14(transitive)
- Removedoauth-sign@0.6.0(transitive)
- Removedqs@2.3.3(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedrequest@2.53.0(transitive)
- Removedtldts@6.1.70(transitive)
- Removedtldts-core@6.1.70(transitive)
- Removedtough-cookie@5.0.0(transitive)
Updatedbluebird@~3.0.5
Updatedfuel-auth@~2.1.0
Updatedrequest@~2.65.0