fastify-feature-flags
v1.2.9
Published
Fastify feature flags plugin
Downloads
21
Maintainers
Readme
fastify-feature-flags
Fastify feature flags plugin. By default it has built-in provider for config
module. However it could be extended by various plugins that implement simple interface.
This plugin is currently in beta, so some bugs can appear. Feel free to create an issue and I'll try to fix them asap.
ToC
Fastify support
- v1.x.x - supports
>= fastify-1.0.0
, includingv2.x.x
versions.
Installation
npm i fastify-feature-flags --save
Features and requirements
- Requires fastify
>=1.0.0
. - Node.js
>=8.9.0
.
Usage
Add it to your project like regular fastify plugin. Use register
method and pass options to it.
const fastify = require('fastify');
const app = fastify();
const ffPlugin = require('fastify-feature-flags');
const ConfigProvider = require('fastify-feature-flags/dist/providers/config');
app.register(ffPlugin, {providers: [new ConfigProvider()]});
Plugin adds an object with built-in providers and generic provider interface that you can extend. For checking features availability it adds two methods: fastify.featureFlags.isEnabled
which returns true
or false
and fastify.featureFlags.checkEnabled
which throws an error if feature is disabled. The list of built-in providers is available below.
Providers
Generic provider
Generic provider is an abstract class that you may extend to add new providers. It should have isEnabled
method that consumes feature name and context (optionally) and returns true
or false
.
Config provider
Reads feature flags from specified config section. Depends on config
module. You should install it manually. It's constuctor consumes options object that contains prefix
for config section where features are defined.
Example:
default.js
(in config directory):
module.exports = {
features: {
a: true,
b: false,
}
}
Configuring provider:
const ConfigProvider = require('fastify-feature-flags/dist/providers/config');
const provider = new ConfigProvider({
prefix: 'features',
})
Valid config values for feature to be enabled are: true
, "true"
or "1"
. Last two may be useful if you're using config module with env overrides.
Env provider
Reads feature flags from env variables. It's constuctor consumes options object that may contain prefix
for filtering env variables containing features.
Example:
default.js
(in config directory):
FEATURE_A = true
FEATURE_B = false
Configuring provider:
const EnvProvider = require('fastify-feature-flags/dist/providers/env');
const provider = new EnvProvider({
prefix: 'FEATURE_',
})
Valid config values for feature to be enabled are: "true"
or "1"
.
Unleash provider
This provider relies on feature flags service Unleash. You should install the module manually.
Example:
Configuring provider:
const UnleashProvider = require('fastify-feature-flags/dist/providers/unleash');
const provider = new UnleashProvider({
appName: 'my-fastify-app',
url: 'https://unleash.example.com/api',
})
For more options please refer to unleash docs
Using plugin
After configuring providers and registering the plugin in your fastify app you can use isEnabled
or checkEnabled
methods.
You may also specify multiple providers, then the feature will be enabled only when it will be enabled in all providers.
Example:
const fastify = require('fastify')();
const ffPlugin = require('fastify-feature-flags');
const EnvProvider = require('fastify-feature-flags/dist/providers/env');
fastify.register(ffPlugin, {
providers: [new EnvProvider({prefix: 'FEATURE_'})]
});
fastify.get('/a', async (request, reply) => {
await fastify.featureFlags.checkEnabled('A');
reply.type('application/json').code(200);
return { a: 'enabled' };
});
fastify.get('/b', async (request, reply) => {
const isEnabled = await fastify.featureFlags.isEnabled('B');
reply.type('application/json').code(200);
return { b: isEnabled };
});
(async () => {
await fastify.ready();
await fastify.listen(3000);
})();
Docs
See docs.
Changelog
See changelog.
See also
- unleash - feature toogle service.
License
Licensed under MIT.