Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Server Dreamcode

Eric Elliott edited this page Nov 10, 2013 · 1 revision

On the server:

Create some features

Creating a feature will cause the feature object to emit the 'new-feature' event. Changing an existing feature will trigger the 'change' event.

var feature = require('feature-toggle');

// Returns the feature object
feature.set({
  // global setting
  'my feature': true,

  // 30 percent of users will see this feature
  'feature by percent': 30,

  // whitelist users
  'another feature': {
    users: ['adam', 'mike', 'testuser']
  },

  // user groups:
  'new feature': {
    groups: ['qa']
  },

  // Combine rules
  'complex feature': {
    // off by default
    active: false,

    // unless you're the product tester...
    users: ['product-tester']
  }
});

// Or set one at a time. Returns the feature object.
feature.set('one off', true);

// Set descriptions...
feature.set('t21', {
  name: 't21',
  description: 'Enable the fancy address input form ' +
  'in template 21.'
  active: true
});


// Later...

// Set userID
feature.userID = 'some-user-id';

// Is the feature enabled for the current user?
feature.active('my feature');

Of course, you can get your feature settings, too:

// Examine feature settings
feature.get('complex feature'); // returns rules object

feature.get('feature by percent'); // returns {percent: 30}

// Get the settings for several features. Returns a collection:
feature.get(featureArray);

feature.get(); // Get all features, returns a collection

Convenience methods

var myFeature = feature.get('complex feature');

// Whitelist a group...
myFeature.addGroup('product');

// Dewhitelist a group...
myFeature.removeGroup('product');

// Whitelist a user...
myFeature.addUser('greg');

// Dewhitelist a user...
myFeature.removeUser('greg');

// Change percent
myFeature.percent(50);

// You can do this with new features, too:
anotherFeature = feature.create({})

Feature sets

It's often convenient to group a number of features together, for example, in order to plan production change sets, and timed releases in a continuous delivery system. Creating a set is easy:

feature.featureSet('Blackbird', [feature1, feature2, feature3]);

// Now you can treat the 'Blackbird' release just like a feature:
feature.set('Blackbird', {
  groups: ['qa', 'product']
});

// Add another feature to the set:
feature.addToSet('Blackbird', feature4); // Also takes an array.

// Get a feature set:
var blackbird = feature.getFeatureSet('Blackbird');

// Activate / deactivate

Clone this wiki locally