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

fastify-feature-flags

v1.2.9

Published

Fastify feature flags plugin

Downloads

74

Readme

fastify-feature-flags

NPM Version Downloads Count Vunerabilities Count Build Status License

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, including v2.x.x versions.

Installation

npm i fastify-feature-flags --save

Back to top

Features and requirements


  • Requires fastify >=1.0.0.
  • Node.js >=8.9.0.

Back to top

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.

Back to top

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);
})();

Back to top

Docs

See docs.

Back to top

Changelog

See changelog.

Back to top

See also

Back to top

License

Licensed under MIT.

Back to top