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

express-feature-flags

v0.0.8

Published

A tool for constructing complex feature-flags with Express

Downloads

12

Readme

express-feature-flags

A tool for constructing complex feature-flags with Express

npm Version

Example

// app/features-schema.js
const administrator = {
  type: 'contains',
  key: 'user.role',
  comparison: ['admin', 'root', 'sysadmin']
}

const features = {
  'administrator': administrator,
  'regular-user': Object.assign({ inverse: true }, administrator),
  'hidden-page': [{
    type: 'eq',
    key: 'user.authenticated',
    comparison: true
  }, {
    type: 'contains',
    key: 'locales',
    comparison({ user }) { return user.locale; }
  }, {
    type: 'gt',
    key: 'timestamp',
    comparison: 1449297410423
  }]
};

export default features;
// app/server.js
import express from 'express';
import featureFlags from 'express-feature-flags';
import featureSchema from './features-schema';

const app = express();
const feature = featureFlags.create(app, featureSchema);

// optional: add a custom predicate
// supported of the box: eq, neq, contains, gt, gte, lt, lte
feature.builder.registerPredicate('blank', (value/*, rule */) => {
  if (typeof value === 'undefined' || value === '') {
    return true;
  }

  return value.length && value.length === 0;
});

app.locals.locales = ['en-US', 'en-CA'];

app.use((req, res, next) => {
  Object.assign(res.locals, {
    timestamp: new Date().getTime(),
    user: {
      authenticated: req.user.isAuthenticated,
      role: req.user.role,
      locale: req.locale.code
    }
  });

  next();
});

/**
 * Responsible for constructing the enabled feature list on per-request basis.
 * Ordering the middleware after your request context is finish being
 * built BUT BEFORE any logic that checks if a feature is enabled
 * is very important!
 */
app.use(feature.register());

// example of a utility function used to guard routes/middleware
function is(key, middlewareFunc) {
  return function(req, res, next) {
    if (res.isEnabled(key)) {
      return middlewareFunc.call(this, req, res, next);
    }

    next();
  }
}

app.use('/hidden', is('hidden-page', (req, res, next) => {
  res.render('pages/hidden-page');
}));

app.use('/admin', is('administrator', (req, res, next) => {
  res.render('pages/hidden-page');
}));

app.use('/', (req, res) => {
  if (res.isEnabled('administrator')) {
    return res.render('pages/home/power-user');
  }

  res.render('pages/home/regular-user');
});

Templating Engine Integration

Handlebars

Template Helper

Handlebars.registerHelper('is-enabled', function(key, options) {
  return options.data.isEnabled(key);
});

Exposing Feature Flags

Handlebars.registerHelper('json', function(context) {
  return JSON.stringify(context);
});
window.FEATURES = {{{json @root.enabled}}}