Skip to content

Commit

Permalink
Formatting codebase + auto format in precommit
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge committed Apr 16, 2018
1 parent 8cc16d4 commit 1881df0
Show file tree
Hide file tree
Showing 58 changed files with 3,736 additions and 5,702 deletions.
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"printWidth": 100,
"singleQuote": true
}
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
"main": "src/index.js",
"scripts": {
"test": "mocha -R spec $(find ./test -name *.tests.js)",
"test:ci": "istanbul cover _mocha --report lcovonly -R $(find ./test -name *.tests.js) -- -R mocha-multi --reporter-options spec=-,mocha-junit-reporter=-",
"test:ci":
"istanbul cover _mocha --report lcovonly -R $(find ./test -name *.tests.js) -- -R mocha-multi --reporter-options spec=-,mocha-junit-reporter=-",
"test:coverage": "codecov",
"test:watch": "NODE_ENV=test mocha --timeout 5000 $(find ./test -name *.tests.js) --watch",
"jsdoc:generate": "jsdoc --configure .jsdoc.json --verbose",
"release:clean": "node scripts/cleanup.js",
"preversion": "node scripts/prepare.js",
"version": "node scripts/changelog.js && node scripts/jsdocs.js",
"postversion": "npm run release:clean"
"postversion": "npm run release:clean",
"precommit": "pretty-quick --staged"
},
"repository": {
"type": "git",
"url": "https://github.com/auth0/node-auth0"
},
"keywords": [
"auth0",
"api"
],
"keywords": ["auth0", "api"],
"author": "Auth0",
"license": "MIT",
"bugs": {
Expand All @@ -39,6 +38,7 @@
"devDependencies": {
"chai": "^2.2.0",
"codecov": "^2.2.0",
"husky": "^0.14.3",
"istanbul": "^0.4.0",
"jsdoc": "^3.4.0",
"json-loader": "^0.5.4",
Expand All @@ -48,6 +48,8 @@
"mocha-multi": "^0.11.0",
"moment": "^2.18.1",
"nock": "^3.1.1",
"prettier": "^1.12.0",
"pretty-quick": "^1.4.1",
"sinon": "^1.17.1",
"string-replace-webpack-plugin": "0.0.3",
"webpack": "^1.12.14"
Expand Down
43 changes: 22 additions & 21 deletions src/Auth0RestClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var RestClient = require('rest-facade').Client;
var Promise = require('bluebird');
var ArgumentError = require('rest-facade').ArgumentError;

var Auth0RestClient = function (resourceUrl, options, provider) {
var Auth0RestClient = function(resourceUrl, options, provider) {
if (resourceUrl === null || resourceUrl === undefined) {
throw new ArgumentError('Must provide a Resource Url');
}
Expand All @@ -19,53 +19,54 @@ var Auth0RestClient = function (resourceUrl, options, provider) {
this.provider = provider;
this.restClient = new RestClient(resourceUrl, options);

this.wrappedProvider = function (method, args) {
this.wrappedProvider = function(method, args) {
if (!this.provider) {
return this.restClient[method].apply(this.restClient, args);
}

var callback;
if(args && args[args.length -1] instanceof Function){
callback = args[args.length -1];
if (args && args[args.length - 1] instanceof Function) {
callback = args[args.length - 1];
}

var self = this;
return this.provider.getAccessToken()
.then(function (access_token) {
return this.provider
.getAccessToken()
.then(function(access_token) {
self.options.headers['Authorization'] = 'Bearer ' + access_token;
return self.restClient[method].apply(self.restClient, args);
}).catch(function(err){
if(callback){
})
.catch(function(err) {
if (callback) {
return callback(err);
}
return Promise.reject(err);
});
}
};
};

Auth0RestClient.prototype.getAll = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.getAll = function(/* [params], [callback] */) {
return this.wrappedProvider('getAll', arguments);
};


Auth0RestClient.prototype.get = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.get = function(/* [params], [callback] */) {
return this.wrappedProvider('get', arguments);
}
};

Auth0RestClient.prototype.create = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.create = function(/* [params], [callback] */) {
return this.wrappedProvider('create', arguments);
}
};

Auth0RestClient.prototype.patch = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.patch = function(/* [params], [callback] */) {
return this.wrappedProvider('patch', arguments);
}
};

Auth0RestClient.prototype.update = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.update = function(/* [params], [callback] */) {
return this.wrappedProvider('update', arguments);
}
};

Auth0RestClient.prototype.delete = function ( /* [params], [callback] */ ) {
Auth0RestClient.prototype.delete = function(/* [params], [callback] */) {
return this.wrappedProvider('delete', arguments);
}
};

module.exports = Auth0RestClient;
110 changes: 53 additions & 57 deletions src/RetryRestClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

var Promise = require('bluebird');
var retry = require('retry');
var ArgumentError = require('rest-facade').ArgumentError;
Expand All @@ -13,10 +12,10 @@ var DEFAULT_OPTIONS = { maxRetries: 10, enabled: true };
* @memberOf module:management
* @param {Object} restClient RestClient.
* @param {Object} [options] Options for the RetryRestClient.
* @param {Object} [options.enabled:true] Enabled or Disable Retry Policy functionality.
* @param {Number} [options.maxRetries=10] The maximum amount of times to retry the operation. Default is 10.
* @param {Object} [options.enabled:true] Enabled or Disable Retry Policy functionality.
* @param {Number} [options.maxRetries=10] The maximum amount of times to retry the operation. Default is 10.
*/
var RetryRestClient = function(restClient, options){
var RetryRestClient = function(restClient, options) {
if (restClient === null || typeof restClient !== 'object') {
throw new ArgumentError('Must provide RestClient');
}
Expand All @@ -34,54 +33,52 @@ var RetryRestClient = function(restClient, options){
this.restClient = restClient;
this.maxRetries = params.maxRetries;
this.enabled = params.enabled;
}
};

RetryRestClient.prototype.getAll = function ( /* [params], [callback] */ ) {
return this.invoke('getAll', arguments);
RetryRestClient.prototype.getAll = function(/* [params], [callback] */) {
return this.invoke('getAll', arguments);
};

RetryRestClient.prototype.get = function ( /* [params], [callback] */ ) {
return this.invoke('get', arguments);
}
RetryRestClient.prototype.get = function(/* [params], [callback] */) {
return this.invoke('get', arguments);
};

RetryRestClient.prototype.create = function ( /* [params], [callback] */ ) {
return this.invoke('create', arguments);
}
RetryRestClient.prototype.create = function(/* [params], [callback] */) {
return this.invoke('create', arguments);
};

RetryRestClient.prototype.patch = function ( /* [params], [callback] */ ) {
return this.invoke('patch', arguments);
}
RetryRestClient.prototype.patch = function(/* [params], [callback] */) {
return this.invoke('patch', arguments);
};

RetryRestClient.prototype.update = function ( /* [params], [callback] */ ) {
return this.invoke('update', arguments);
}
RetryRestClient.prototype.update = function(/* [params], [callback] */) {
return this.invoke('update', arguments);
};

RetryRestClient.prototype.delete = function ( /* [params], [callback] */ ) {
return this.invoke('delete', arguments);
}
RetryRestClient.prototype.delete = function(/* [params], [callback] */) {
return this.invoke('delete', arguments);
};

RetryRestClient.prototype.invoke = function(method, args){
RetryRestClient.prototype.invoke = function(method, args) {
var cb;
args = Array.prototype.slice.call(args); // convert array-like object to array.
if(args && args[args.length -1] instanceof Function){
cb = args[args.length -1];
if (args && args[args.length - 1] instanceof Function) {
cb = args[args.length - 1];
args.pop(); // Remove the callback
}

var promise = this.handleRetry(method, args);

if (cb instanceof Function) {
promise
.then(cb.bind(null, null))
.catch(cb);
promise.then(cb.bind(null, null)).catch(cb);
return;
}

return promise;
}
};

RetryRestClient.prototype.handleRetry = function(method, args){
if(!this.enabled){
RetryRestClient.prototype.handleRetry = function(method, args) {
if (!this.enabled) {
return this.restClient[method].apply(this.restClient, args);
}

Expand All @@ -93,12 +90,13 @@ RetryRestClient.prototype.handleRetry = function(method, args){
};

var self = this;
var promise = new Promise(function (resolve, reject) {
var promise = new Promise(function(resolve, reject) {
var operation = retry.operation(retryOptions);

operation.attempt(function(){
self.restClient[method].apply(self.restClient, args)
.then(function(body) {
operation.attempt(function() {
self.restClient[method]
.apply(self.restClient, args)
.then(function(body) {
resolve(body);
})
.catch(function(err) {
Expand All @@ -110,51 +108,49 @@ RetryRestClient.prototype.handleRetry = function(method, args){
return promise;
};

RetryRestClient.prototype.invokeRetry = function(err, operation , reject){
RetryRestClient.prototype.invokeRetry = function(err, operation, reject) {
var ratelimits = this.extractRatelimits(err);
if(ratelimits){
if (ratelimits) {
var delay = ratelimits.reset * 1000 - new Date().getTime();
if(delay > 0){
if (delay > 0) {
this.retryWithDelay(delay, operation, err, reject);
}else{
} else {
this.retryWithImmediate(operation, err, reject);
}
}else{
}
} else {
reject(err);
}
}
}
};

RetryRestClient.prototype.extractRatelimits = function(err){
if(err && err.statusCode === 429 && err.originalError && err.originalError.response){
RetryRestClient.prototype.extractRatelimits = function(err) {
if (err && err.statusCode === 429 && err.originalError && err.originalError.response) {
var headers = err.originalError.response.header;
if(headers && headers['x-ratelimit-limit']){
if (headers && headers['x-ratelimit-limit']) {
return {
limit: headers['x-ratelimit-limit'],
remaining: headers['x-ratelimit-remaining'],
reset: headers['x-ratelimit-reset']
}
};
}
}

return;
}
};

RetryRestClient.prototype.retryWithImmediate = function(operation, err, reject){
if(operation.retry(err)){
RetryRestClient.prototype.retryWithImmediate = function(operation, err, reject) {
if (operation.retry(err)) {
return;
}
}
reject(err);
}
};

RetryRestClient.prototype.retryWithDelay = function(delay, operation, err, reject){
RetryRestClient.prototype.retryWithDelay = function(delay, operation, err, reject) {
setTimeout(() => {
if(operation.retry(err)){
if (operation.retry(err)) {
return;
}
reject(err);
}, delay);
}


};

module.exports = RetryRestClient;
Loading

0 comments on commit 1881df0

Please sign in to comment.