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

dipswitch

v4.0.2

Published

Multi-tenant feature toggles backed by Redis.

Downloads

41

Readme

dipswitch

Dipswitch is a multi-tenant feature toggle library.

NPM

Build Status

Installation (via npm)

$ npm install dipswitch --save

If you wish to use redis to store this data, also install dipswitch-redis. Otherwise, an in-memory store will be used.

Usage

The Basics

var Dipswitch = require('dipswitch'),
  dipswitch = new Dipswitch({
    redis: require('redis').createClient(),

    getTenantId: function (tenant) { // default is `return tenant.id;`
      return tenant.tenantId;
    },

    getUserId: function (user) { // default is `return user.id;`
      return user.userId;
    },

    getUserGroups: function (user) { // default is `return user.groups || [];`
      if (user.isAdmin) {
        return ['admins'];
      }
      return [];
    }
  });

// ADDING & REMOVING FEATURES --------------------------------------------------

// add & remove a feature
dipswitch.features.add('EXAMPLE_FEATURE').then(function () { /* ... */ });
dipswitch.features.remove('EXAMPLE_FEATURE').then(function () { /* ... */ });

// and another syntax for registering many features at once
dipswitch.features.add(['FEATURE_1', 'FEATURE_2']).then(function () { /* ... */ });

// and unregister many features at once as well
dipswitch.features.remove(['EXAMPLE_FEATURE1', 'EXAMPLE_FEATURE2']).then(function () { /* ... */ });

// retrieve all registered features
dipswitch.features.find().then(function (features) {
  /* ... */
});

// ENABLING FEATURES -----------------------------------------------------------

// enable a feature globally
dipswitch.features.enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// enable a feature for an entire tenant
dipswitch.tenant(tenant).enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// enable a feature for a group within a tenant
dipswitch.tenant(tenant).group('admins').enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// enable a feature a group across all tenants
dipswitch.group('admins').enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// enable a feature for a user within a tenant
dipswitch.tenant(tenant).user(user).enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// enable a feature for a user across all tenants
dipswitch.user(user).enable('EXAMPLE_FEATURE').then(function () { /* ... */ });

// SCHEDULING FEATURES ---------------------------------------------------------

// when enabling a feature you may optionally pass a `start` and/or `end` date
dipswitch.features.enable('LOL_OMG_CATFACTS', {
  start: new Date(2015, 3, 1), // April 1st
  end: new Date(2015, 3, 2) // April 2nd
}).then(function () { /* ... */ });

// CHECKING FEATURES -----------------------------------------------------------

// retrieve all features and whether they're enabled or not for a user
dipswitch.tenant(tenant).user(user).features().then(function (features) {
  console.log(features.EXAMPLE_FEATURE); // `true`
});

// check a single feature and whether it is enabled or not for a user
dipswitch.tenant(tenant).user(user).feature('EXAMPLE_FEATURE').then(function (isEnabled) {
  console.log(isEnabled); // `true`
});

// DISABLING FEATURES ----------------------------------------------------------

// anywhere that you can call #enable, you can also call #disable
dipswitch.tenant(tenant).user(user).disable('EXAMPLE_FEATURE').then(function () { /* ... */ });

Unregistering Out-of-Date Features

Rather than having to have an admin UI for unregistering features you no longer care about, you can use a bit of underscore/lodash magic to automate the process:

var _ = require('underscore'),
  Dipswitch = require('dipswitch'),
  dipswitch = new Dipswitch(options);

// note that FEATURE_2 was removed at some point
var desiredFeatures = ['FEATURE_1', 'FEATURE_3'];

dipswitch.register(desiredFeatures)
  .then(function () {
    return dipswitch.features(); // lets say this returns ['FEATURE_1', 'FEATURE_2', 'FEATURE_3']
  })
  .then(function (registeredFeatures) {
    var outOfDateFeatures = _.without(registeredFeatures, desiredFeatures);
    return dipswitch.unregister(outOfDateFeatures); // will remove ['FEATURE_2']
  });

Interacting With Unregistered Features

When enabling or disabling a feature that has never been registered, that feature is automatically registered. When testing to see the value of a feature that isn't registered, the result will be false.

License

MIT License

Author

Lanetix ([email protected])