From bdd5b009bb01db8a4ec5452f833a7bec7d62cad9 Mon Sep 17 00:00:00 2001 From: Thomas J Date: Thu, 11 Mar 2021 22:34:44 +0100 Subject: [PATCH 1/4] Remove the config file It was injecting a global variable ("config") and was only use to define a default value for the API URL. --- config.js | 12 ------------ env/config-local.js | 3 --- flagsmith-core.js | 5 +---- index.js | 1 - 4 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 config.js delete mode 100644 env/config-local.js diff --git a/config.js b/config.js deleted file mode 100644 index 486302e..0000000 --- a/config.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * Created by kylejohnson on 02/10/2016. - * Global config - */ -const env = process.env.config; -switch (env) { - default: { - config = require('./env/config-local'); - } -} - -module.exports = config; diff --git a/env/config-local.js b/env/config-local.js deleted file mode 100644 index 2fc9581..0000000 --- a/env/config-local.js +++ /dev/null @@ -1,3 +0,0 @@ -module.exports = { - api : "https://api.bullet-train.io/api/v1/" -}; \ No newline at end of file diff --git a/flagsmith-core.js b/flagsmith-core.js index 8e682a2..464ba9e 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -127,16 +127,13 @@ const FlagsmithCore = class { this.environmentID = environmentID; - this.api = api; + this.api = api || "https://api.bullet-train.io/api/v1/"; this.disableCache = disableCache; this.onError = onError; if (!environmentID) { throw ('Please specify a environment id'); } - if (api === undefined) { - this.api = config.api; - } } getValue (key, userId) { diff --git a/index.js b/index.js index ffdf97a..13d7748 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const config = require("./config"); const fetch = require('node-fetch').default; const core = require('./flagsmith-core'); const flagsmith = core({fetch: fetch}); From 36ac149abd8711206aecf9c34ebda506fa02aa8d Mon Sep 17 00:00:00 2001 From: Thomas J Date: Thu, 11 Mar 2021 22:42:08 +0100 Subject: [PATCH 2/4] Prevent setting the environment key if it is not present --- flagsmith-core.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index 464ba9e..87f5f46 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -124,16 +124,15 @@ const FlagsmithCore = class { disableCache, onError, }) { + if (!environmentID) { + throw new Error('Please specify a environment id'); + } this.environmentID = environmentID; this.api = api || "https://api.bullet-train.io/api/v1/"; this.disableCache = disableCache; this.onError = onError; - - if (!environmentID) { - throw ('Please specify a environment id'); - } } getValue (key, userId) { From 17db23f486f4ed92e745b799d00a8d6b9a55b79e Mon Sep 17 00:00:00 2001 From: Thomas J Date: Thu, 11 Mar 2021 22:48:36 +0100 Subject: [PATCH 3/4] Throw an error if the API is returning a non-200 HTTP response We are rejecting with the detail message --- flagsmith-core.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index 87f5f46..580b4d7 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -28,7 +28,14 @@ const FlagsmithCore = class { options.headers['Content-Type'] = 'application/json; charset=utf-8'; } return fetch(url, options) - .then(res => res.json()); + .then(res => { + return res.json() + .then(result => { + if (res.status < 200 || res.status >= 400) { + Promise.reject(new Error(result.detail)) + } else return result; + }) + }); }; } From e3ed367b0f7eb59850aeb62702b89c5e78aae3ec Mon Sep 17 00:00:00 2001 From: Thomas J Date: Thu, 11 Mar 2021 22:51:57 +0100 Subject: [PATCH 4/4] Remove unused variables in Core file --- flagsmith-core.js | 1 - 1 file changed, 1 deletion(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index 580b4d7..3261e9c 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -1,5 +1,4 @@ let fetch; -const BULLET_TRAIN_KEY = "BULLET_TRAIN_DB"; const FlagsmithCore = class {