From 0cef0603e5928312a2c2b6e65f01a19758e542d5 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Tue, 22 Jan 2019 14:21:55 +0400 Subject: [PATCH 01/12] add errors config, enable configure errors --- lib/dispatcher.js | 22 +++++++++++++------- lib/errors/config.js | 34 +++++++++++++++++++++++++++++++ lib/errors/index.js | 2 ++ lib/errors/rate_limit_enforced.js | 1 + 4 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 lib/errors/config.js diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 40dfc423..f3628ec8 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -6,13 +6,21 @@ var querystring = require('querystring'); var VERSION = require('../package.json').version; -var STATUS_MAP = Object.keys(errors).reduce(function(map, key) { - var error = new errors[key](null); - if (error.status) { - map[error.status] = errors[key]; - } - return map; -}, {}); +var STATUS_MAP = null; + +setTimeout(function() { + STATUS_MAP = Object.keys(errors).reduce(function(map, key) { + if (key === 'config') { + return map; + } + var args = errors.config[key]; + var error = new errors[key](args); + if (error.status) { + map[error.status] = errors[key]; + } + return map; + }, {}); +}, 0); // TODO: Provide same set of options for each request as for configuration, // so the config at construction time is just the "defaults" diff --git a/lib/errors/config.js b/lib/errors/config.js new file mode 100644 index 00000000..71e229f7 --- /dev/null +++ b/lib/errors/config.js @@ -0,0 +1,34 @@ +const config = {}; + +config.Forbidden = null; + +config.InvalidRequest = null; + +config.NoAuthorization = null; + +config.NotFound = null; + +config.PremiumOnly = null; + +config.RateLimitEnforced = { + // retryAfterSeconds + retry_after: 2, +}; + +config.ServerError = null; + +// set full configuration for error +config.setConfig = function(error_name, error_config) { + this[error_name] = error_config; +}; + +// add single configuration for error +config.addConfig = function(error_name, single_config_name, single_config) { + if (!this[error_name]) { + this[error_name] = {}; + } + + this[error_name][single_config_name] = single_config; +}; + +module.exports = config; diff --git a/lib/errors/index.js b/lib/errors/index.js index 4071a9f3..c64376a0 100644 --- a/lib/errors/index.js +++ b/lib/errors/index.js @@ -1,3 +1,5 @@ +exports.config = require('./config'); + exports.Forbidden = require('./forbidden'); exports.InvalidRequest = require('./invalid_request'); exports.NoAuthorization = require('./no_authorization'); diff --git a/lib/errors/rate_limit_enforced.js b/lib/errors/rate_limit_enforced.js index b156dcb8..007ae860 100644 --- a/lib/errors/rate_limit_enforced.js +++ b/lib/errors/rate_limit_enforced.js @@ -6,6 +6,7 @@ function RateLimitEnforced(value) { AsanaError.call(this, 'Rate Limit Enforced'); this.status = 429; this.value = value; + this.retryAfterSeconds = value && parseInt(value.retry_after, 10); } From a91966caa667f5b00ecd89157061f590585a0fa6 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Tue, 22 Jan 2019 14:29:53 +0400 Subject: [PATCH 02/12] fix lint errors --- lib/errors/config.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/errors/config.js b/lib/errors/config.js index 71e229f7..0fbae2fc 100644 --- a/lib/errors/config.js +++ b/lib/errors/config.js @@ -1,4 +1,4 @@ -const config = {}; +var config = {}; config.Forbidden = null; @@ -18,17 +18,17 @@ config.RateLimitEnforced = { config.ServerError = null; // set full configuration for error -config.setConfig = function(error_name, error_config) { - this[error_name] = error_config; +config.setConfig = function(errorName, errorConfig) { + this[errorName] = errorConfig; }; // add single configuration for error -config.addConfig = function(error_name, single_config_name, single_config) { - if (!this[error_name]) { - this[error_name] = {}; +config.addConfig = function(errorName, singleConfigName, singleConfig) { + if (!this[errorName]) { + this[errorName] = {}; } - this[error_name][single_config_name] = single_config; + this[errorName][singleConfigName] = singleConfig; }; module.exports = config; From 0aec291ba1be9b90a45d24cc9243abea7071eab6 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Tue, 22 Jan 2019 14:30:35 +0400 Subject: [PATCH 03/12] fix lint errors --- lib/errors/config.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/errors/config.js b/lib/errors/config.js index 0fbae2fc..3d824acc 100644 --- a/lib/errors/config.js +++ b/lib/errors/config.js @@ -10,10 +10,7 @@ config.NotFound = null; config.PremiumOnly = null; -config.RateLimitEnforced = { - // retryAfterSeconds - retry_after: 2, -}; +config.RateLimitEnforced = null; config.ServerError = null; From 6c00b0a00a849bea6cc06f7931ccc327bfe18f4b Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Tue, 22 Jan 2019 14:33:45 +0400 Subject: [PATCH 04/12] fix test --- test/dispatcher_spec.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/dispatcher_spec.js b/test/dispatcher_spec.js index 82170e4c..83816a68 100644 --- a/test/dispatcher_spec.js +++ b/test/dispatcher_spec.js @@ -169,6 +169,9 @@ describe('Dispatcher', function() { }); Object.keys(errors).forEach(function(key) { + if (key === 'config') { + return; + } it('should create an error for ' + key, function() { var request = sinon.stub(); var err = new errors[key](); From 6e6737e6431e102d85efa2721699c03534f53c07 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Tue, 22 Jan 2019 14:36:19 +0400 Subject: [PATCH 05/12] delete addConfig, change setConfig implementation --- lib/errors/config.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/errors/config.js b/lib/errors/config.js index 3d824acc..fee426ec 100644 --- a/lib/errors/config.js +++ b/lib/errors/config.js @@ -14,18 +14,13 @@ config.RateLimitEnforced = null; config.ServerError = null; -// set full configuration for error config.setConfig = function(errorName, errorConfig) { - this[errorName] = errorConfig; -}; - -// add single configuration for error -config.addConfig = function(errorName, singleConfigName, singleConfig) { if (!this[errorName]) { - this[errorName] = {}; + config[errorName] = {}; } - - this[errorName][singleConfigName] = singleConfig; + Object.keys(errorConfig, function(errorKey) { + config[errorKey] = errorConfig[errorKey]; + }); }; module.exports = config; From c2a34a1ce3574e7535edfcb0602b1bb0f594cd24 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Wed, 23 Jan 2019 12:55:22 +0400 Subject: [PATCH 06/12] Change package name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0cb26a1f..4a46a804 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "asana", + "name": "@screenful/asana", "version": "0.16.6", "description": "Official NodeJS and BrowserJS client for the Asana API", "main": "index.js", From 41e0b7ba59b6bbc6cbc9ed8b4672503c767bb2ff Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Wed, 23 Jan 2019 14:40:28 +0400 Subject: [PATCH 07/12] improve logic --- index.js | 1 + lib/config/index.js | 9 +++++++++ lib/dispatcher.js | 26 ++++++++++---------------- lib/errors/config.js | 26 -------------------------- lib/errors/index.js | 2 -- package.json | 2 +- test/dispatcher_spec.js | 3 --- 7 files changed, 21 insertions(+), 48 deletions(-) create mode 100644 lib/config/index.js delete mode 100644 lib/errors/config.js diff --git a/index.js b/index.js index 71fa4696..5ba21021 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,4 @@ +exports.config = require('./lib/config'); exports.Client = require('./lib/client'); exports.Dispatcher = require('./lib/dispatcher'); exports.auth = require('./lib/auth'); diff --git a/lib/config/index.js b/lib/config/index.js new file mode 100644 index 00000000..954514a6 --- /dev/null +++ b/lib/config/index.js @@ -0,0 +1,9 @@ +var errors = require('../errors'); + +var config = {}; + +config.setConfig = function(errorName, errorConfig) { + errors[errorName].config = errorConfig; +}; + +module.exports = config; diff --git a/lib/dispatcher.js b/lib/dispatcher.js index f3628ec8..31fcda23 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -6,21 +6,13 @@ var querystring = require('querystring'); var VERSION = require('../package.json').version; -var STATUS_MAP = null; - -setTimeout(function() { - STATUS_MAP = Object.keys(errors).reduce(function(map, key) { - if (key === 'config') { - return map; - } - var args = errors.config[key]; - var error = new errors[key](args); - if (error.status) { - map[error.status] = errors[key]; - } - return map; - }, {}); -}, 0); +var STATUS_MAP = Object.keys(errors).reduce(function(map, key) { + var error = new errors[key](null); + if (error.status) { + map[error.status] = errors[key]; + } + return map; +}, {}); // TODO: Provide same set of options for each request as for configuration, // so the config at construction time is just the "defaults" @@ -166,7 +158,9 @@ Dispatcher.prototype.dispatch = function(params, dispatchOptions) { return reject(err); } if (STATUS_MAP[res.statusCode]) { - var error = new STATUS_MAP[res.statusCode](payload); + var error_config = STATUS_MAP[res.statusCode].config; + var args = Object.assign(payload, error_config); + var error = new STATUS_MAP[res.statusCode](args); if (me.retryOnRateLimit && error instanceof (errors.RateLimitEnforced)) { diff --git a/lib/errors/config.js b/lib/errors/config.js deleted file mode 100644 index fee426ec..00000000 --- a/lib/errors/config.js +++ /dev/null @@ -1,26 +0,0 @@ -var config = {}; - -config.Forbidden = null; - -config.InvalidRequest = null; - -config.NoAuthorization = null; - -config.NotFound = null; - -config.PremiumOnly = null; - -config.RateLimitEnforced = null; - -config.ServerError = null; - -config.setConfig = function(errorName, errorConfig) { - if (!this[errorName]) { - config[errorName] = {}; - } - Object.keys(errorConfig, function(errorKey) { - config[errorKey] = errorConfig[errorKey]; - }); -}; - -module.exports = config; diff --git a/lib/errors/index.js b/lib/errors/index.js index c64376a0..4071a9f3 100644 --- a/lib/errors/index.js +++ b/lib/errors/index.js @@ -1,5 +1,3 @@ -exports.config = require('./config'); - exports.Forbidden = require('./forbidden'); exports.InvalidRequest = require('./invalid_request'); exports.NoAuthorization = require('./no_authorization'); diff --git a/package.json b/package.json index 4a46a804..a7b2368d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@screenful/asana", - "version": "0.16.6", + "version": "0.16.8", "description": "Official NodeJS and BrowserJS client for the Asana API", "main": "index.js", "scripts": { diff --git a/test/dispatcher_spec.js b/test/dispatcher_spec.js index 83816a68..82170e4c 100644 --- a/test/dispatcher_spec.js +++ b/test/dispatcher_spec.js @@ -169,9 +169,6 @@ describe('Dispatcher', function() { }); Object.keys(errors).forEach(function(key) { - if (key === 'config') { - return; - } it('should create an error for ' + key, function() { var request = sinon.stub(); var err = new errors[key](); From 925a01314e0c95a6d8b508652e8942076f848928 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Wed, 23 Jan 2019 14:41:56 +0400 Subject: [PATCH 08/12] Update package.json --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index a7b2368d..0cb26a1f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@screenful/asana", - "version": "0.16.8", + "name": "asana", + "version": "0.16.6", "description": "Official NodeJS and BrowserJS client for the Asana API", "main": "index.js", "scripts": { From 3cd9bc758b0fb5882ee3aadbd85c3e37c9aed39e Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Wed, 23 Jan 2019 14:45:24 +0400 Subject: [PATCH 09/12] Fix lint issues --- lib/dispatcher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index 31fcda23..9f0a1f15 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -158,8 +158,8 @@ Dispatcher.prototype.dispatch = function(params, dispatchOptions) { return reject(err); } if (STATUS_MAP[res.statusCode]) { - var error_config = STATUS_MAP[res.statusCode].config; - var args = Object.assign(payload, error_config); + var errorConfig = STATUS_MAP[res.statusCode].config; + var args = Object.assign(payload, errorConfig); var error = new STATUS_MAP[res.statusCode](args); if (me.retryOnRateLimit && From 3fae431a6452ff91dc301fe0488bcb0891019416 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Wed, 23 Jan 2019 14:48:39 +0400 Subject: [PATCH 10/12] Update rate_limit_enforced.js --- lib/errors/rate_limit_enforced.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/errors/rate_limit_enforced.js b/lib/errors/rate_limit_enforced.js index 007ae860..f37e96f7 100644 --- a/lib/errors/rate_limit_enforced.js +++ b/lib/errors/rate_limit_enforced.js @@ -6,10 +6,9 @@ function RateLimitEnforced(value) { AsanaError.call(this, 'Rate Limit Enforced'); this.status = 429; this.value = value; - this.retryAfterSeconds = value && parseInt(value.retry_after, 10); } util.inherits(RateLimitEnforced, Error); -module.exports = RateLimitEnforced; \ No newline at end of file +module.exports = RateLimitEnforced; From bf6621f6684b6e8e153a41f3a5fbb5b220105011 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Thu, 14 Jan 2021 11:26:16 +0400 Subject: [PATCH 11/12] Update dispatcher.js --- lib/dispatcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index ee51f0bb..ee28e231 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -260,7 +260,7 @@ Dispatcher.prototype.dispatch = function(params, dispatchOptions) { } if (STATUS_MAP[res.statusCode]) { - var errorConfig = STATUS_MAP[res.statusCode].config; + var errorConfig = STATUS_MAP[res.statusCode].config || {}; var args = Object.assign(payload, errorConfig); var error = new STATUS_MAP[res.statusCode](args, res); From 149ad466ceeaf35712ad0ea44defe900d9322026 Mon Sep 17 00:00:00 2001 From: Nairi Harutyunyan Date: Thu, 14 Jan 2021 11:30:00 +0400 Subject: [PATCH 12/12] Update dispatcher.js --- lib/dispatcher.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dispatcher.js b/lib/dispatcher.js index ee28e231..7fbf371a 100644 --- a/lib/dispatcher.js +++ b/lib/dispatcher.js @@ -260,8 +260,8 @@ Dispatcher.prototype.dispatch = function(params, dispatchOptions) { } if (STATUS_MAP[res.statusCode]) { - var errorConfig = STATUS_MAP[res.statusCode].config || {}; - var args = Object.assign(payload, errorConfig); + var errorConfig = STATUS_MAP[res.statusCode].config; + var args = Object.assign({}, payload, errorConfig); var error = new STATUS_MAP[res.statusCode](args, res); if (me.retryOnRateLimit &&