From 74b4df012b9735c9740ed1d71e9a3a1c86b604c5 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 15:04:56 +0200 Subject: [PATCH 1/9] Remove "disableCache" as it is unused --- flagsmith-core.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index 3261e9c..3e029a5 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -127,7 +127,6 @@ const FlagsmithCore = class { init({ environmentID, api, - disableCache, onError, }) { if (!environmentID) { @@ -137,7 +136,6 @@ const FlagsmithCore = class { this.environmentID = environmentID; this.api = api || "https://api.bullet-train.io/api/v1/"; - this.disableCache = disableCache; this.onError = onError; } From 5bc173b3baf07a3f5f8ac2f0b2b4ee6b811db109 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 15:16:29 +0200 Subject: [PATCH 2/9] Add "prettier" to use the same format along the entire project --- .prettierrc.js | 9 +++++++++ package-lock.json | 8 +++++++- package.json | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .prettierrc.js diff --git a/.prettierrc.js b/.prettierrc.js new file mode 100644 index 0000000..80a762e --- /dev/null +++ b/.prettierrc.js @@ -0,0 +1,9 @@ +module.exports = { + bracketSpacing: true, + printWidth: 100, + singleQuote: true, + tabWidth: 4, + trailingComma: 'none', + useTabs: false, + arrowParens: 'avoid' +}; diff --git a/package-lock.json b/package-lock.json index f82f748..97819ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "flagsmith-nodejs", - "version": "1.0.8", + "version": "1.0.9", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -8,6 +8,12 @@ "version": "2.1.2", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz", "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=" + }, + "prettier": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz", + "integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==", + "dev": true } } } diff --git a/package.json b/package.json index 23ee71d..158463d 100644 --- a/package.json +++ b/package.json @@ -41,5 +41,8 @@ "license": "MIT", "dependencies": { "node-fetch": "^2.1.2" + }, + "devDependencies": { + "prettier": "^2.2.1" } } From eb0e594f5a6778c3b4943010e844a776a7b8fde8 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 15:21:42 +0200 Subject: [PATCH 3/9] Reformat the project using prettier --- .prettierignore | 1 + CONTRIBUTING.md | 9 +-- LICENCE.md | 2 +- example/README.md | 20 +++---- example/server/api/index.js | 25 ++++---- example/server/index.js | 7 +-- flagsmith-core.js | 112 ++++++++++++++++++++---------------- index.d.ts | 42 +++++++------- index.js | 2 +- package.json | 3 + 10 files changed, 117 insertions(+), 106 deletions(-) create mode 100644 .prettierignore diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..94a2dd1 --- /dev/null +++ b/.prettierignore @@ -0,0 +1 @@ +*.json \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6bebc39..48d8ad0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,4 +1,5 @@ # Contributing + We're always looking to improve this project, open source contribution is encouraged so long as they adhere to our guidelines. # Pull Requests @@ -7,7 +8,7 @@ The Solid State team will be monitoring for pull requests. When we get one, a me **A couple things to keep in mind:** - - If you've changed APIs, update the documentation. - - Keep the code style (indents, wrapping) consistent. - - If your PR involves a lot of commits, squash them using ```git rebase -i``` as this makes it easier for us to review. - - Keep lines under 80 characters. \ No newline at end of file +- If you've changed APIs, update the documentation. +- Keep the code style (indents, wrapping) consistent. +- If your PR involves a lot of commits, squash them using `git rebase -i` as this makes it easier for us to review. +- Keep lines under 80 characters. diff --git a/LICENCE.md b/LICENCE.md index e57593a..be8d20d 100644 --- a/LICENCE.md +++ b/LICENCE.md @@ -9,4 +9,4 @@ Redistribution and use in source and binary forms, with or without modification, 3. Neither the name of the Sentry nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/example/README.md b/example/README.md index c89c13a..c221528 100644 --- a/example/README.md +++ b/example/README.md @@ -1,21 +1,21 @@ -Flagsmith example -================================== +# Flagsmith example - -Getting Started ---------------- +## Getting Started # Setup via cli -```npm i ssg-node -g``` +`npm i ssg-node -g` -```ssg-node PROJECT_NAME``` +`ssg-node PROJECT_NAME` # Run -```$ npm start``` + +`$ npm start` # Nodemon (Restart server on changes) -```npm run dev``` + +`npm run dev` # The project -- ``/server/api`` contains a simple express api that interacts with Flagsmith + +- `/server/api` contains a simple express api that interacts with Flagsmith diff --git a/example/server/api/index.js b/example/server/api/index.js index f3932ff..db60be3 100644 --- a/example/server/api/index.js +++ b/example/server/api/index.js @@ -1,6 +1,6 @@ const Router = require('express').Router; -const environmentID = "uCDQzKWgejrutqSYYsKWen"; -const flagsmith = require("flagsmith-nodejs"); +const environmentID = 'uCDQzKWgejrutqSYYsKWen'; +const flagsmith = require('flagsmith-nodejs'); flagsmith.init({ environmentID @@ -10,24 +10,21 @@ module.exports = () => { const api = Router(); api.get('/', (req, res) => { - flagsmith.getValue("font_size") - .then((font_size) => { - res.json({font_size}) - }); + flagsmith.getValue('font_size').then(font_size => { + res.json({ font_size }); + }); }); api.get('/flags', (req, res) => { - flagsmith.getFlags() - .then((flags) => { - res.json(flags) - }); + flagsmith.getFlags().then(flags => { + res.json(flags); + }); }); api.get('/:user', (req, res) => { - flagsmith.getValue("font_size", "flagsmith_sample_user") - .then((font_size) => { - res.json({font_size}) - }); + flagsmith.getValue('font_size', 'flagsmith_sample_user').then(font_size => { + res.json({ font_size }); + }); }); return api; diff --git a/example/server/index.js b/example/server/index.js index 778994a..2b1b21c 100755 --- a/example/server/index.js +++ b/example/server/index.js @@ -16,16 +16,15 @@ app.use(bodyParser.json()); // api router app.use('/', api()); - app.server.listen(PORT); console.log('Server started on port ' + app.server.address().port); console.log(); -console.log('Go to http://localhost:'+PORT+'/'); +console.log('Go to http://localhost:' + PORT + '/'); console.log('To get an example feature state'); console.log(); -console.log('Go to http://localhost:'+PORT+'/flagsmith_sample_user'); +console.log('Go to http://localhost:' + PORT + '/flagsmith_sample_user'); console.log('To get an example feature state for a user'); -console.log('Go to http://localhost:'+PORT+'/flags'); +console.log('Go to http://localhost:' + PORT + '/flags'); console.log('To get an example response for getFlags'); module.exports = app; diff --git a/flagsmith-core.js b/flagsmith-core.js index 3e029a5..0a99623 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -1,7 +1,6 @@ let fetch; const FlagsmithCore = class { - constructor(props) { fetch = props.fetch; @@ -23,30 +22,28 @@ const FlagsmithCore = class { 'x-environment-key': environmentID } }; - if (method !== "GET") { + if (method !== 'GET') { options.headers['Content-Type'] = 'application/json; charset=utf-8'; } - return fetch(url, options) - .then(res => { - return res.json() - .then(result => { - if (res.status < 200 || res.status >= 400) { - Promise.reject(new Error(result.detail)) - } else return result; - }) + return fetch(url, options).then(res => { + return res.json().then(result => { + if (res.status < 200 || res.status >= 400) { + Promise.reject(new Error(result.detail)); + } else return result; }); + }); }; } - getFlagsForUser (identity) { + getFlagsForUser(identity) { const { onError, api } = this; if (!identity) { - onError && onError({message: 'getFlagsForUser() called without a user identity'}); + onError && onError({ message: 'getFlagsForUser() called without a user identity' }); return Promise.reject('getFlagsForUser() called without a user identity'); } - const handleResponse = (res) => { + const handleResponse = res => { // Handle server response let flags = {}; res.flags.forEach(feature => { @@ -61,21 +58,22 @@ const FlagsmithCore = class { return this.getJSON(api + 'identities/?identifier=' + identity) .then(res => { return handleResponse(res); - }).catch(({ message }) => { + }) + .catch(({ message }) => { onError && onError({ message }); return Promise.reject(message); }); } - getUserIdentity (identity) { + getUserIdentity(identity) { const { onError, api } = this; if (!identity) { - onError && onError({message: 'getUserIdentity() called without a user identity'}); + onError && onError({ message: 'getUserIdentity() called without a user identity' }); return Promise.reject('getUserIdentity() called without a user identity'); } - const handleResponse = (res) => { + const handleResponse = res => { // Handle server response let flags = {}; let traits = {}; @@ -85,7 +83,7 @@ const FlagsmithCore = class { value: feature.feature_state_value }; }); - res.traits.forEach(({trait_key, trait_value}) => { + res.traits.forEach(({ trait_key, trait_value }) => { traits[trait_key.toLowerCase().replace(/ /g, '_')] = trait_value; }); return { flags, traits }; @@ -94,7 +92,8 @@ const FlagsmithCore = class { return this.getJSON(api + 'identities/?identifier=' + identity) .then(res => { return handleResponse(res); - }).catch(({ message }) => { + }) + .catch(({ message }) => { onError && onError({ message }); return Promise.reject(message); }); @@ -103,7 +102,7 @@ const FlagsmithCore = class { getFlags() { const { onError, api } = this; - const handleResponse = (res) => { + const handleResponse = res => { // Handle server response let flags = {}; res.forEach(feature => { @@ -115,55 +114,52 @@ const FlagsmithCore = class { return flags; }; - return this.getJSON(api + "flags/") + return this.getJSON(api + 'flags/') .then(res => { return handleResponse(res); - }).catch(({ message }) => { + }) + .catch(({ message }) => { onError && onError({ message }); return Promise.reject(message); }); - }; + } - init({ - environmentID, - api, - onError, - }) { + init({ environmentID, api, 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.api = api || 'https://api.bullet-train.io/api/v1/'; this.onError = onError; } - getValue (key, userId) { + getValue(key, userId) { if (userId) { - return this.getFlagsForUser(userId).then((flags) => { + return this.getFlagsForUser(userId).then(flags => { return this.getValueFromFeatures(key, flags); - }) + }); } else { - return this.getFlags().then((flags) => { + return this.getFlags().then(flags => { return this.getValueFromFeatures(key, flags); }); } } - hasFeature (key, userId) { + hasFeature(key, userId) { if (userId) { - return this.getFlagsForUser(userId).then((flags) => { + return this.getFlagsForUser(userId).then(flags => { return this.checkFeatureEnabled(key, flags); - }) + }); } else { - return this.getFlags().then((flags) => { + return this.getFlags().then(flags => { return this.checkFeatureEnabled(key, flags); }); } } - getValueFromFeatures (key, flags) { + getValueFromFeatures(key, flags) { if (!flags) { return null; } @@ -177,7 +173,7 @@ const FlagsmithCore = class { return res; } - checkFeatureEnabled (key, flags) { + checkFeatureEnabled(key, flags) { if (!flags) { return false; } @@ -190,37 +186,51 @@ const FlagsmithCore = class { return res; } - getTrait (identity, key) { + getTrait(identity, key) { const { onError } = this; if (!identity || !key) { - onError && onError({message: `getTrait() called without a ${!identity ? 'user identity' : 'trait key'}`}); - return Promise.reject(`getTrait() called without a ${!identity ? 'user identity' : 'trait key'}`); + onError && + onError({ + message: `getTrait() called without a ${ + !identity ? 'user identity' : 'trait key' + }` + }); + return Promise.reject( + `getTrait() called without a ${!identity ? 'user identity' : 'trait key'}` + ); } return this.getUserIdentity(identity) - .then(({traits}) => traits[key]) + .then(({ traits }) => traits[key]) .catch(({ message }) => { onError && onError({ message }); return Promise.reject(message); }); } - setTrait (identity, key, value) { + setTrait(identity, key, value) { const { onError, api } = this; if (!identity || !key) { - onError && onError({message: `setTrait() called without a ${!identity ? 'user identity' : 'trait key'}`}); - return Promise.reject(`setTrait() called without a ${!identity ? 'user identity' : 'trait key'}`); + onError && + onError({ + message: `setTrait() called without a ${ + !identity ? 'user identity' : 'trait key' + }` + }); + return Promise.reject( + `setTrait() called without a ${!identity ? 'user identity' : 'trait key'}` + ); } const body = { - "identity": { - "identifier": identity + identity: { + identifier: identity }, - "trait_key": key, - "trait_value": value - } + trait_key: key, + trait_value: value + }; return this.getJSON(`${api}traits/`, 'POST', JSON.stringify(body)) .then(() => this.getUserIdentity(identity)) diff --git a/index.d.ts b/index.d.ts index 49c3ec9..27c6bd1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,51 +3,51 @@ declare module 'flagsmith-nodejs' { * Initialise the sdk against a particular environment */ export function init(config: { - environmentID: string - onError?: Function - defaultFlags?: string[] - api?: string - }): void + environmentID: string; + onError?: Function; + defaultFlags?: string[]; + api?: string; + }): void; /** * Get the whether a flag is enabled e.g. flagsmith.hasFeature("powerUserFeature") */ - export function hasFeature(key: string): Promise + export function hasFeature(key: string): Promise; /** * Get the value of a whether a flag is enabled for a user e.g. flagsmith.hasFeature("powerUserFeature", 1234) */ - export function hasFeature(key: string, userId: string): Promise + export function hasFeature(key: string, userId: string): Promise; /** * Get the value of a particular remote config e.g. flagsmith.getValue("font_size") */ - export function getValue(key: string): Promise + export function getValue(key: string): Promise; /** * Get the value of a particular remote config for a specified user e.g. flagsmith.getValue("font_size", 1234) */ - export function getValue(key: string, userId: string): Promise + export function getValue(key: string, userId: string): Promise; /** * Trigger a manual fetch of the environment features */ - export function getFlags(): Promise + export function getFlags(): Promise; /** * Trigger a manual fetch of the environment features for a given user id */ - export function getFlagsForUser(userId: string): Promise + export function getFlagsForUser(userId: string): Promise; /** * Trigger a manual fetch of both the environment features and users' traits for a given user id */ - export function getUserIdentity(userId: string): Promise + export function getUserIdentity(userId: string): Promise; /** * Trigger a manual fetch of a specific trait for a given user id */ - export function getTrait(userId: string, key: string): Promise + export function getTrait(userId: string, key: string): Promise; /** * Set a specific trait for a given user id @@ -55,24 +55,24 @@ declare module 'flagsmith-nodejs' { export function setTrait( userId: string, key: string, - value: string|number|boolean - ): IUserIdentity + value: string | number | boolean + ): IUserIdentity; interface IFeature { - enabled: boolean - value?: string|number|boolean + enabled: boolean; + value?: string | number | boolean; } interface IFlags { - [key: string]: IFeature + [key: string]: IFeature; } interface ITraits { - [key: string]: string + [key: string]: string; } interface IUserIdentity { - flags: IFeature - traits: ITraits + flags: IFeature; + traits: ITraits; } } diff --git a/index.js b/index.js index 13d7748..89597ef 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,5 @@ const fetch = require('node-fetch').default; const core = require('./flagsmith-core'); -const flagsmith = core({fetch: fetch}); +const flagsmith = core({ fetch }); module.exports = flagsmith; diff --git a/package.json b/package.json index 158463d..f2e65fe 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,9 @@ } ], "license": "MIT", + "scripts": { + "lint": "prettier --write ." + }, "dependencies": { "node-fetch": "^2.1.2" }, From a3c043a52e40b03a20a0941fb4243712b97c68e4 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 15:30:20 +0200 Subject: [PATCH 4/9] Move "init" method near the constructor --- flagsmith-core.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index 0a99623..dbb2144 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -35,6 +35,17 @@ const FlagsmithCore = class { }; } + init({ environmentID, api, 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.onError = onError; + } + getFlagsForUser(identity) { const { onError, api } = this; @@ -124,17 +135,6 @@ const FlagsmithCore = class { }); } - init({ environmentID, api, 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.onError = onError; - } - getValue(key, userId) { if (userId) { return this.getFlagsForUser(userId).then(flags => { From eb84a4f506fd2e44ab581e750081d398a726bcbc Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 16:25:37 +0200 Subject: [PATCH 5/9] Implement a cache mechanism to improve response latency --- flagsmith-core.js | 56 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index dbb2144..a0ee4b8 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -35,7 +35,7 @@ const FlagsmithCore = class { }; } - init({ environmentID, api, onError }) { + init({ environmentID, api, onError, cache }) { if (!environmentID) { throw new Error('Please specify a environment id'); } @@ -44,9 +44,32 @@ const FlagsmithCore = class { this.api = api || 'https://api.bullet-train.io/api/v1/'; this.onError = onError; + + if (cache) { + const missingMethods = []; + + ['has', 'get', 'set'].forEach(method => { + if (!cache[method]) missingMethods.push(method); + }); + + if (missingMethods.length > 0) { + throw new Error( + `Please implement the following methods in your cache: ${missingMethods.join( + ', ' + )}` + ); + } + } + this.cache = cache; } - getFlagsForUser(identity) { + async getFlagsForUser(identity) { + const cacheKey = `flags-${identity}`; + + if (this.cache && (await this.cache.has(cacheKey))) { + return this.cache.get(cacheKey); + } + const { onError, api } = this; if (!identity) { @@ -54,7 +77,7 @@ const FlagsmithCore = class { return Promise.reject('getFlagsForUser() called without a user identity'); } - const handleResponse = res => { + const handleResponse = async res => { // Handle server response let flags = {}; res.flags.forEach(feature => { @@ -63,6 +86,9 @@ const FlagsmithCore = class { value: feature.feature_state_value }; }); + + if (this.cache) await this.cache.set(cacheKey, flags); + return flags; }; @@ -76,7 +102,13 @@ const FlagsmithCore = class { }); } - getUserIdentity(identity) { + async getUserIdentity(identity) { + const cacheKey = `flags_traits-${identity}`; + + if (this.cache && (await this.cache.has(cacheKey))) { + return this.cache.get(cacheKey); + } + const { onError, api } = this; if (!identity) { @@ -84,7 +116,7 @@ const FlagsmithCore = class { return Promise.reject('getUserIdentity() called without a user identity'); } - const handleResponse = res => { + const handleResponse = async res => { // Handle server response let flags = {}; let traits = {}; @@ -97,6 +129,9 @@ const FlagsmithCore = class { res.traits.forEach(({ trait_key, trait_value }) => { traits[trait_key.toLowerCase().replace(/ /g, '_')] = trait_value; }); + + if (this.cache) await this.cache.set(cacheKey, { flags, traits }); + return { flags, traits }; }; @@ -110,10 +145,14 @@ const FlagsmithCore = class { }); } - getFlags() { + async getFlags() { + if (this.cache && (await this.cache.has('flags'))) { + return this.cache.get('flags'); + } + const { onError, api } = this; - const handleResponse = res => { + const handleResponse = async res => { // Handle server response let flags = {}; res.forEach(feature => { @@ -122,6 +161,9 @@ const FlagsmithCore = class { value: feature.feature_state_value }; }); + + if (this.cache) await this.cache.set('flags', flags); + return flags; }; From cb4eb496da8f84e4a542129f07ba03d886e98dc0 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 16:27:32 +0200 Subject: [PATCH 6/9] Use the example to show how to use the "cache" options --- example/package-lock.json | 24 ++++++++++++------------ example/package.json | 2 +- example/server/api/index.js | 6 +++++- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/example/package-lock.json b/example/package-lock.json index 3047701..12f0331 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -59,6 +59,11 @@ "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" }, + "clone": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.2.tgz", + "integrity": "sha1-G39Ln1kfHo+DZwQBYANFoCiHQ18=" + }, "commander": { "version": "2.17.1", "resolved": "https://registry.npmjs.org/commander/-/commander-2.17.1.tgz", @@ -288,14 +293,6 @@ } } }, - "flagsmith-nodejs": { - "version": "1.0.8", - "resolved": "https://registry.npmjs.org/flagsmith-nodejs/-/flagsmith-nodejs-1.0.8.tgz", - "integrity": "sha512-8P61TYk9odceeczMT7Ex2UGTQe86fYbF7g4oWtLCcR/aIc6rn0OZQQxSRRxwlcararL4EcCJDjJdsvYddxudDw==", - "requires": { - "node-fetch": "^2.1.2" - } - }, "forwarded": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", @@ -470,10 +467,13 @@ "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.1.tgz", "integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw==" }, - "node-fetch": { - "version": "2.6.1", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" + "node-cache": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/node-cache/-/node-cache-5.1.2.tgz", + "integrity": "sha512-t1QzWwnk4sjLWaQAS8CHgOJ+RAfmHpxFWmc36IWTiWHQfs0w5JDMBS1b1ZxQteo0vVVuWJvIUKHDkkeK7vIGCg==", + "requires": { + "clone": "2.x" + } }, "object-assign": { "version": "4.1.1", diff --git a/example/package.json b/example/package.json index b280137..95b32c6 100644 --- a/example/package.json +++ b/example/package.json @@ -15,7 +15,7 @@ "npm": "3.10.x" }, "dependencies": { - "flagsmith-nodejs": "^1.0.8", + "node-cache": "^5.1.2", "ssg-node-express": "4.16.4", "ssg-util": "0.0.3" }, diff --git a/example/server/api/index.js b/example/server/api/index.js index db60be3..e7d6137 100644 --- a/example/server/api/index.js +++ b/example/server/api/index.js @@ -1,9 +1,13 @@ const Router = require('express').Router; const environmentID = 'uCDQzKWgejrutqSYYsKWen'; -const flagsmith = require('flagsmith-nodejs'); +const flagsmith = require('../../../'); +const NodeCache = require('node-cache'); flagsmith.init({ environmentID + /*cache: new NodeCache({ + stdTTL: 5 + })*/ }); module.exports = () => { From 2ad85c58d52ec0426b8a7476087eb01694f99de4 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 18:00:46 +0200 Subject: [PATCH 7/9] Rewrite terser methods with async/await --- flagsmith-core.js | 319 ++++++++++++++++++++-------------------------- index.js | 6 +- 2 files changed, 138 insertions(+), 187 deletions(-) diff --git a/flagsmith-core.js b/flagsmith-core.js index a0ee4b8..8b31174 100644 --- a/flagsmith-core.js +++ b/flagsmith-core.js @@ -1,38 +1,53 @@ -let fetch; - -const FlagsmithCore = class { - constructor(props) { - fetch = props.fetch; - - this.checkFeatureEnabled = this.checkFeatureEnabled.bind(this); - this.getFlags = this.getFlags.bind(this); - this.getFlagsForUser = this.getFlagsForUser.bind(this); - this.getUserIdentity = this.getUserIdentity.bind(this); - this.getValue = this.getValue.bind(this); - this.getValueFromFeatures = this.getValueFromFeatures.bind(this); - this.hasFeature = this.hasFeature.bind(this); - this.init = this.init.bind(this); - - this.getJSON = function (url, method, body) { - const { environmentID } = this; - const options = { - method: method || 'GET', - body, - headers: { - 'x-environment-key': environmentID - } +const fetch = require('node-fetch'); + +module.exports = class FlagsmithCore { + normalizeFlags(flags) { + const _flags = {}; + + for (const { feature, enabled, feature_state_value } of flags) { + const normalizedKey = feature.name.toLowerCase().replace(/ /g, '_'); + _flags[normalizedKey] = { + enabled, + value: feature_state_value }; - if (method !== 'GET') { - options.headers['Content-Type'] = 'application/json; charset=utf-8'; + } + + return _flags; + } + + normalizeTraits(traits) { + const _traits = {}; + + for (const { trait_key, trait_value } of traits) { + const normalizedKey = trait_key.toLowerCase().replace(/ /g, '_'); + _traits[normalizedKey] = trait_value; + } + + return _traits; + } + + async getJSON(url, method, body) { + const { environmentID } = this; + const options = { + method: method || 'GET', + body, + headers: { + 'x-environment-key': environmentID } - return fetch(url, options).then(res => { - return res.json().then(result => { - if (res.status < 200 || res.status >= 400) { - Promise.reject(new Error(result.detail)); - } else return result; - }); - }); }; + + if (method !== 'GET') { + options.headers['Content-Type'] = 'application/json; charset=utf-8'; + } + + const res = await fetch(url, options); + const result = await res.json(); + + if (res.status >= 400) { + throw new Error(result.detail); + } + + return result; } init({ environmentID, api, onError, cache }) { @@ -42,7 +57,7 @@ const FlagsmithCore = class { this.environmentID = environmentID; - this.api = api || 'https://api.bullet-train.io/api/v1/'; + this.api = api || 'https://api.flagsmith.com/api/v1'; this.onError = onError; if (cache) { @@ -60,9 +75,30 @@ const FlagsmithCore = class { ); } } + this.cache = cache; } + async getFlags() { + if (this.cache && (await this.cache.has('flags'))) { + return this.cache.get('flags'); + } + + const { onError, api } = this; + + try { + const flags = await this.getJSON(`${api}/flags/`); + const normalizedFlags = this.normalizeFlags(flags); + + if (this.cache) await this.cache.set('flags', normalizedFlags); + + return normalizedFlags; + } catch (err) { + onError && onError({ message: err.message }); + throw err; + } + } + async getFlagsForUser(identity) { const cacheKey = `flags-${identity}`; @@ -73,33 +109,22 @@ const FlagsmithCore = class { const { onError, api } = this; if (!identity) { - onError && onError({ message: 'getFlagsForUser() called without a user identity' }); - return Promise.reject('getFlagsForUser() called without a user identity'); + const errMsg = 'getFlagsForUser() called without a user identity'; + onError && onError({ message: errMsg }); + throw new Error(errMsg); } - const handleResponse = async res => { - // Handle server response - let flags = {}; - res.flags.forEach(feature => { - flags[feature.feature.name.toLowerCase().replace(/ /g, '_')] = { - enabled: feature.enabled, - value: feature.feature_state_value - }; - }); + try { + const { flags } = await this.getJSON(`${api}/identities/?identifier=${identity}`); + const normalizedFlags = this.normalizeFlags(flags); - if (this.cache) await this.cache.set(cacheKey, flags); + if (this.cache) await this.cache.set(cacheKey, normalizedFlags); - return flags; - }; - - return this.getJSON(api + 'identities/?identifier=' + identity) - .then(res => { - return handleResponse(res); - }) - .catch(({ message }) => { - onError && onError({ message }); - return Promise.reject(message); - }); + return normalizedFlags; + } catch (err) { + onError && onError({ message: err.message }); + throw err; + } } async getUserIdentity(identity) { @@ -112,158 +137,89 @@ const FlagsmithCore = class { const { onError, api } = this; if (!identity) { - onError && onError({ message: 'getUserIdentity() called without a user identity' }); - return Promise.reject('getUserIdentity() called without a user identity'); + const errMsg = 'getUserIdentity() called without a user identity'; + onError && onError({ message: errMsg }); + throw new Error(errMsg); } - const handleResponse = async res => { - // Handle server response - let flags = {}; - let traits = {}; - res.flags.forEach(feature => { - flags[feature.feature.name.toLowerCase().replace(/ /g, '_')] = { - enabled: feature.enabled, - value: feature.feature_state_value - }; - }); - res.traits.forEach(({ trait_key, trait_value }) => { - traits[trait_key.toLowerCase().replace(/ /g, '_')] = trait_value; - }); - - if (this.cache) await this.cache.set(cacheKey, { flags, traits }); + try { + const { flags, traits } = await this.getJSON( + `${api}/identities/?identifier=${identity}` + ); - return { flags, traits }; - }; + const normalizedFlags = this.normalizeFlags(flags); + const normalizedTraits = this.normalizeTraits(traits); + const res = { flags: normalizedFlags, traits: normalizedTraits }; - return this.getJSON(api + 'identities/?identifier=' + identity) - .then(res => { - return handleResponse(res); - }) - .catch(({ message }) => { - onError && onError({ message }); - return Promise.reject(message); - }); - } + if (this.cache) await this.cache.set(cacheKey, res); - async getFlags() { - if (this.cache && (await this.cache.has('flags'))) { - return this.cache.get('flags'); + return res; + } catch (err) { + onError && onError({ message: err.message }); + throw err; } + } - const { onError, api } = this; - - const handleResponse = async res => { - // Handle server response - let flags = {}; - res.forEach(feature => { - flags[feature.feature.name.toLowerCase().replace(/ /g, '_')] = { - enabled: feature.enabled, - value: feature.feature_state_value - }; - }); - - if (this.cache) await this.cache.set('flags', flags); - - return flags; - }; + async getValue(key, userId) { + const flags = userId ? await this.getFlagsForUser(userId) : await this.getFlags(); - return this.getJSON(api + 'flags/') - .then(res => { - return handleResponse(res); - }) - .catch(({ message }) => { - onError && onError({ message }); - return Promise.reject(message); - }); + return this.getValueFromFeatures(key, flags); } - getValue(key, userId) { - if (userId) { - return this.getFlagsForUser(userId).then(flags => { - return this.getValueFromFeatures(key, flags); - }); - } else { - return this.getFlags().then(flags => { - return this.getValueFromFeatures(key, flags); - }); - } - } + async hasFeature(key, userId) { + const flags = userId ? await this.getFlagsForUser(userId) : await this.getFlags(); - hasFeature(key, userId) { - if (userId) { - return this.getFlagsForUser(userId).then(flags => { - return this.checkFeatureEnabled(key, flags); - }); - } else { - return this.getFlags().then(flags => { - return this.checkFeatureEnabled(key, flags); - }); - } + return this.checkFeatureEnabled(key, flags); } getValueFromFeatures(key, flags) { - if (!flags) { - return null; - } + if (!flags) return null; + const flag = flags[key]; - let res = null; - if (flag) { - res = flag.value; - } - //todo record check for value - return res; + //todo record check for value + return flag ? flag.value : null; } checkFeatureEnabled(key, flags) { - if (!flags) { - return false; - } - const flag = flags[key]; - let res = false; - if (flag && flag.enabled) { - res = true; - } + if (!flags) return false; - return res; + const flag = flags[key]; + return flag && flag.enabled; } - getTrait(identity, key) { + async getTrait(identity, key) { const { onError } = this; if (!identity || !key) { - onError && - onError({ - message: `getTrait() called without a ${ - !identity ? 'user identity' : 'trait key' - }` - }); - return Promise.reject( - `getTrait() called without a ${!identity ? 'user identity' : 'trait key'}` - ); + const errMsg = `getTrait() called without a ${ + !identity ? 'user identity' : 'trait key' + }`; + onError && onError({ message: errMsg }); + throw new Error(errMsg); } - return this.getUserIdentity(identity) - .then(({ traits }) => traits[key]) - .catch(({ message }) => { - onError && onError({ message }); - return Promise.reject(message); - }); + try { + const { traits } = await this.getUserIdentity(identity); + return traits[key]; + } catch (err) { + onError && onError({ message: err.message }); + throw err; + } } - setTrait(identity, key, value) { + async setTrait(identity, key, value) { const { onError, api } = this; if (!identity || !key) { + const errMsg = `setTrait() called without a ${ + !identity ? 'user identity' : 'trait key' + }`; onError && onError({ - message: `setTrait() called without a ${ - !identity ? 'user identity' : 'trait key' - }` + message: errMsg }); - return Promise.reject( - `setTrait() called without a ${!identity ? 'user identity' : 'trait key'}` - ); + throw new Error(errMsg); } const body = { @@ -274,15 +230,12 @@ const FlagsmithCore = class { trait_value: value }; - return this.getJSON(`${api}traits/`, 'POST', JSON.stringify(body)) - .then(() => this.getUserIdentity(identity)) - .catch(({ message }) => { - onError && onError({ message }); - return Promise.reject(message); - }); + try { + await this.getJSON(`${api}/traits/`, 'POST', JSON.stringify(body)); + return await this.getUserIdentity(identity); + } catch (err) { + onError && onError({ message: err.message }); + throw err; + } } }; - -module.exports = function ({ fetch }) { - return new FlagsmithCore({ fetch }); -}; diff --git a/index.js b/index.js index 89597ef..54eefae 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,3 @@ -const fetch = require('node-fetch').default; -const core = require('./flagsmith-core'); -const flagsmith = core({ fetch }); +const FlagsmithCore = require('./flagsmith-core'); -module.exports = flagsmith; +module.exports = new FlagsmithCore(); From 8bc4a0dca94cd2442b70f9387e3697bce48dd243 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 18:01:24 +0200 Subject: [PATCH 8/9] Rewrite the example using async/await --- example/server/api/index.js | 21 +++++++++------------ example/server/index.js | 2 -- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/example/server/api/index.js b/example/server/api/index.js index e7d6137..7a4afc4 100644 --- a/example/server/api/index.js +++ b/example/server/api/index.js @@ -13,22 +13,19 @@ flagsmith.init({ module.exports = () => { const api = Router(); - api.get('/', (req, res) => { - flagsmith.getValue('font_size').then(font_size => { - res.json({ font_size }); - }); + api.get('/', async (req, res) => { + const font_size = await flagsmith.getValue('font_size'); + res.json({ font_size }); }); - api.get('/flags', (req, res) => { - flagsmith.getFlags().then(flags => { - res.json(flags); - }); + api.get('/flags', async (req, res) => { + const flags = await flagsmith.getFlags(); + res.json(flags); }); - api.get('/:user', (req, res) => { - flagsmith.getValue('font_size', 'flagsmith_sample_user').then(font_size => { - res.json({ font_size }); - }); + api.get('/:user', async (req, res) => { + const font_size = await flagsmith.getValue('font_size', req.params.user); + res.json({ font_size }); }); return api; diff --git a/example/server/index.js b/example/server/index.js index 2b1b21c..7e0014e 100755 --- a/example/server/index.js +++ b/example/server/index.js @@ -1,5 +1,3 @@ -global.fetch = require('fetchify')(Promise).fetch; // polyfil - const http = require('http'); const express = require('express'); const api = require('./api'); From 39060c4b2ad5419f52d5a25962a7d43c0ed5a947 Mon Sep 17 00:00:00 2001 From: Thomas Jouannic Date: Mon, 12 Apr 2021 18:48:26 +0200 Subject: [PATCH 9/9] Add "cache" type to the declaration file --- example/server/api/index.js | 1 + index.d.ts | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/example/server/api/index.js b/example/server/api/index.js index 7a4afc4..cbcd75e 100644 --- a/example/server/api/index.js +++ b/example/server/api/index.js @@ -5,6 +5,7 @@ const NodeCache = require('node-cache'); flagsmith.init({ environmentID + // this is an example of a user-defined cache /*cache: new NodeCache({ stdTTL: 5 })*/ diff --git a/index.d.ts b/index.d.ts index 27c6bd1..eeac23b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -7,6 +7,7 @@ declare module 'flagsmith-nodejs' { onError?: Function; defaultFlags?: string[]; api?: string; + cache?: ICache; }): void; /** @@ -75,4 +76,10 @@ declare module 'flagsmith-nodejs' { flags: IFeature; traits: ITraits; } + + interface ICache { + has(key: string): boolean | Promise; + get(key: string): any | Promise; + set(key: string, val: any): void | Promise; + } }