Slim Feature Toggle
slim-feature-toggle is a JavaScript library for toggling content based on features and version management.
slim-feature-toggle fits for both client and server applications and is agnostic to frameworks
The following example will guide you through it
const appFeatures = {
PUPPIES: {
enabled: true,
POODLES: {
enabled: true
},
LABRADORS: {
enabled: false
}
},
KITTEN: { enabled: true },
BABIES: { enabled: true }
};
To set up the configuration needed,
import the featureToggle module at Application Start and set the app's features:
const { featureToggle } = require('slim-feature-toggle');
featureToggle.setAppFeatures(appFeatures);
After setting the configuration we are good to go.
Simply import the featureToggle module into your code and control the application flow using one of the following methods:
const { featureToggle } = require('slim-feature-toggle');
const { isFeatureEnabled } = featureToggle;
if (isFeatureEnabled('PUPPIES')) {
console.log(`TODO - Render the PuppyList component here ... `)
} else {
console.log(`TODO - Engage a free monthly puppy subscription offer here ...`);
}
if (isFeatureEnabled(['KITTEN', 'BABIES'])) {
console.log(`Do stuff if both KITTEN and BABIES are enabled`);
}
if (isFeatureEnabled('PUPPIES.POODLES')) {
console.log(`Run if both PUPPIES feature and POODLES sub-feature are enabled`);
}
const { featureToggle } = require('slim-feature-toggle');
const { featureToggleRunCallback } = featureToggle;
featureToggleRunCallback('KITTEN', () => {
});
const { featureToggle } = require('slim-feature-toggle');
const { featureToggleRunPromise } = featureToggle;
featureToggleRunPromise('BABIES')
.then(() => console.log('resolves only if BABIES feature is enabled'))
.catch(() => console.log('rejects if BABIES feature is disabled'));