Javascript SDK for FeatureGuards (Node + Browser)
The official FeatureGuards NodeJS Javascript client library.
NodeJS
The FeatureGuards Node library provides convenient access to the FeatureGuards API from applications written in server-side JavaScript.
For using FeatureGuards in the browser, use [featureguards-web][featureguards-web].
Requirements
Node 8, 10 or higher.
Installation
Install the package with:
npm install featureguards-node --save
# or
yarn add featureguards-node
Usage
The package needs to be configured with your API key, which is available in the Featureguards Dashboard. Require it with the key's value:
async
/await
:
Usage with modules and import featureguards from 'featureguards-node';
const featureGuards = await featureguards({
apiKey: 'MY API KEY'
})(async () => {
const isOn = await featureguards.isOn('MY_FEATURE_GUARD');
console.log(isOn);
})();
Usage with Promises
Every method returns a chainable promise which can be used instead of a regular callback:
// Check a feature flag is on or not:
featureGuards.isOn('MY_FEATURE')
.then((isOn) => {
// Deal with whether the feature is on or off.
})
.catch((err) => {
// Deal with an error
});
});
Usage with TypeScript
Import featureguards as a default import passing it the API key and use it.
import featureguards from 'featureguards-node';
const featureGuards = await featureguards({
apiKey: 'MY API KEY'
});
const isOn = await featureguards.isOn('MY_FEATURE_GUARD');
console.log(isOn);
You can find a full TS server example in featureguards-js.
Configuring default values
FeatureGuards does no I/O for isOn
because it keeps a fresh copy for all features defined in the
dashboard. In the event the initial fetch doesn't succeed, the library can use default values passed
by the caller. This is useful for features that have graduated and no longer need to be guarded by a
feature flag, but may want to keep an emergency toggle to turn off the feature in case something bad
happens, hence not removing them completely from FeatureGuards. This concept is referred to as 'kill
switch'. Here is an example of how to do so.
Javascript
import featureguards from 'featureguards-web';
const featureGuards = await featureguards({
apiKey: 'MY API KEY',
defaults: {
MY_FEATURE_GUARD: true
}
})(async () => {
const isOn = await featureguards.isOn('MY_FEATURE_GUARD');
console.log(isOn);
})();
Typescript
import featureguards from 'featureguards-web';
const featureGuards = await featureguards({
apiKey: 'MY API KEY',
defaults: {
MY_FEATURE_GUARD: true
}
})(async () => {
const isOn = await featureguards.isOn('MY_FEATURE_GUARD');
console.log(isOn);
})();
Development
Run all tests:
$ yarn install
$ yarn test
If you do not have yarn
installed, you can get it with npm install --global yarn
.
Run prettier:
Add an editor integration or:
$ yarn fix