1. @nuskin/ns-feature-flags
This library is used to load the feature flags and check if they are enabled
@nuskin/ns-feature-flags
Package: @nuskin/ns-feature-flags
Last modified: Thu, 07 Mar 2024 20:23:53 GMT
Version: 1.5.0
License: MIT
Downloads: 3,318

Install

npm install @nuskin/ns-feature-flags
yarn add @nuskin/ns-feature-flags

ns-feature-flags

A simple library to retrieve feature flags and check if they are enabled or not. This project will be pushed to NPM repo at @nuskin/ns-feature-flags.

Installation

 npm install @nuskin/ns-feature-flags

For adding @nuskin/ns-feature-flags to the package.json:

 npm install @nuskin/ns-feature-flags --save

Or

 yarn add @nuskin/ns-feature-flags

Usage

 // You can also use import as in ES6
const {retrieveToggles, isEnabled} = require('@nuskin/ns-feature-flags');

// Note that it will return a promise when you call retrieveToggles (You can use await or then)
await retrieveToggles(url);

// Once the retrieveToggles are done, you can call isEnabled
let isFeature1Enabled = isEnabled('feature1');

If you want to retrieve the toggles for a particular user, you can pass it an optional userId to retrieveToggles:

 // Note that 'qa' is the userId (an optional field)
await retrieveToggles(url, 'qa');

If you want to always retrieve the toggles for Production without providing the URL, you can do the following:

 // Note that 'qa' is the userId (an optional field)
await retrieveProductionToggles('qa');

Retrieving Toggles Based on the Environment


If you want to retrive the toggles based on the current environment, there is a way to do that.

The retrieveTogglesBasedOnEnvironment takes up to 3 parameters in an options object, all of them optional:

  • hostname - String (optional): The hostname of the current url. Examples: "dev.nuskin.com", "api.cloud.nuskin.com", "localhost". Do NOT use in Node.
  • environment - String (optional): The environment name. Examples: "dev", "test", "stage", "prod". Use for Node.
  • userId - String (optional): Feature toggles will be constrained to this user id.

If you don't provide either the hostname or environment option, the functional will look for "window.location.hostname" and use that.

The default environment toggles that this function will return is production.

Node Example:

 // The options object is optional.
const options = {
  environment: proccess.env.ENVIRONMENT
  userId: 'qa'
};

await retrieveTogglesBasedOnEnvironment(options);

Non-Node Example:

 // The options object is optional.
const options = {
  userId: 'qa'
};

await retrieveTogglesBasedOnEnvironment(options);

Important: Developer should NOT invoke retrieveToggles()/retrieveProductionToggles() repeatedly in an application because it will incur network cost by hitting the feature flags endpoint. It is expected to call retrieveToggles()/retrieveProductionToggles() at the start, then call it again as needed (e.g., polling). However, we have implemented a safe guard such that it will NOT initiate additional network call if retrieveToggles()/retrieveProductionToggles() is called repeatedly within five seconds. Note that this does not prevent multiple network calls if more than one versions of ns-feature-flags are included.

Session Storage User id

To toggle a feature flag in the browser using a user id, you can define a user id in the feature flag dashboard and toggle it setting a key value pair in session storage.

Example

To enable a feature flag that has a userId defined in the dashboard run the following command in your devtools console:

 sessionStorage.setItem('$featureFlagUserId',`{userId}`)

Overriding Flag Values

To explicitly set a flag value, like in an integration test or local development, you can
set a value in the query string or in session storage.

Both the query string and session storage values use the same key names. To enable flags,
the key is $enableFeatureFlags; to disable them, it is $disableFeatureFlags. Both of
these values are a comma-separated list of the flags you want to enable or disable.

Examples

To enable the flags 'foo' and 'bar' and disable the flag 'baz' in the query string,
add this to the URL: ?$enableFeatureFlags=foo,bar&$disableFeatureFlags=baz.

To do the same in session storage, run the following command in your devtools console:

 sessionStorage.setItem('$enableFeatureFlags', 'foo,bar');
sessionStorage.setItem('$disableFeatureFlags', 'baz');

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code