npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nuskin/ns-feature-flags

v1.5.0

Published

This library is used to load the feature flags and check if they are enabled

Downloads

3,388

Readme

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');