feature-toggle-service
v8.0.0
Published
The simplest solution for feature toggle in Javascript. Simple how it should be.
Maintainers
Readme
Feature Toggle Service
The simplest solution for feature toggles in Javascript. Simple how it should be.
Why Feature toggle?
This is a common concept, but why use this directive instead solve it via server-side rendering?
The idea of this directive is make this process transparent and easier. So the main point is integrate this directive with other tooling process, such as:
- Server-side rendering;
- Progressive rendering;
- Any other that yoy like :)
You can integrate with WebSockets or handling this in a EventSourcing architecture. It's totally transparent for you and you can integrate easier in your application.
Contributing
Please check our contributing.md to know more about setup and how to contribute.
Install
npm install feature-toggle-serviceimport { set, isOn } from 'feature-toggle-service';
set({ enableNewHeader: true, enableBetaArea: false });
isOn('enableNewHeader'); // true
isOn('enableBetaArea'); // falseUsing it via <script> tag (UMD)
The UMD build exposes the FeatureToggleService global:
<script src="https://unpkg.com/feature-toggle-service/dist/umd/feature-toggle-service.min.js"></script>
<script>
FeatureToggleService.set({ enableNewHeader: true });
FeatureToggleService.isOn('enableNewHeader'); // true
</script>Demo
Try out our demo on Stackblitz!
Development
Use the Node.js version from .nvmrc (e.g. nvm use), then:
npm install
npm test # unit tests (Vitest)
npm run test:smoke # installs the packed package and checks ESM, CJS and UMD builds
npm run lint # ESLint
npm run format # Prettier
npm run lint:types # TypeScript type check
npm run build # builds ESM, CJS and UMD into `dist/`
npm run size # bundle size check (size-limit)FeatureToggleService
This service exposes a few different methods with which you can interact with feature toggle service.
featureToggleService.set(obj)
Adds the feature toggle configuration in your application. This information will be private and checked via featureToggleService.isOn(key) method.
featureToggleService.isOn(key, debug)
Checks if the feature toggle configuration has the string key value with true value. For bundle reasons if you want to check if the feature toggle is turned off, please use !featureToggleService.isOn(key). When debug param is passed as true, it prints configuration, version and settings for debug purposes
createFeatureToggleService(initialConfig)
Creates an isolated service with its own configuration. Use it when the global configuration shared by set()/isOn() is not enough, e.g. one service per server request (so toggles don't leak between users) or per React tree.
import { createFeatureToggleService } from 'feature-toggle-service';
const featureToggles = createFeatureToggleService({ enableNewHeader: true });
featureToggles.isOn('enableNewHeader'); // true
featureToggles.set({ enableBetaArea: true }); // merged with the current configuration
featureToggles.getAll(); // { enableNewHeader: true, enableBetaArea: true }
// Notified every time the configuration changes
const unsubscribe = featureToggles.subscribe(() => {
console.log(featureToggles.getAll());
});getAll() returns a frozen object that keeps the same reference until the configuration changes, so subscribe and getAll can be used with React's useSyncExternalStore.
Publish
this project is using np package to publish, which makes things straightforward. EX: np <patch|minor|major>
For more details, please check np package on npmjs.com
Author
Wilson Mendes (willmendesneto)


