JSPM

  • Created
  • Published
  • Downloads 396269
  • Score
    100M100P100Q237254F

Unleash Client for Node

Package Exports

  • unleash-client

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (unleash-client) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

unleash-client-node

Build Status Code Climate Coverage Status dependencies Status devDependencies Status

This is the node client for Unleash. Read more about the Unleash project

Getting started

1. Initialize unleash-client

You should as early as possible in your node (web) app initialize the unleash-client. The unleash-client will set-up a in-memory repository, and poll updates from the unleash-server at regular intervals.

const { initialize } = require('unleash-client');
const instance = initialize({
    url: 'http://unleash.herokuapp.com',
    appName: 'my-app-name',
    instanceId: 'my-unique-instance-id',
});

// optional events
instance.on('error', console.error);
instance.on('warn', console.warn);
instance.on('ready', console.log);

// metrics hooks
instance.on('registered', (clientData) => console.log('registered', clientData));
instance.on('sent', (payload) => console.log('metrics bucket/payload sent', payload));
instance.on('count', (name, enabled) => console.log(`isEnabled(${name}) returned ${enabled}`));

2. Use unleash

After you have initialized the unleash-client you can easily check if a feature toggle is enabled or not.

const { isEnabled } = require('unleash-client');
isEnabled('app.ToggleX');

3. Stop unleash

To shut down the client (turn off the polling) you can simply call the destroy-method. This is typically not required.

const { destroy } = require('unleash-client');
destroy();

Advanced usage

The initialize method takes the following arguments:

  • url - the url to fetch toggles from. (required)
  • appName - the application name / codebase name
  • instanceId - an unique identifier, should/could be somewhat unique
  • refreshIntervall - The poll-intervall to check for updates. Defaults to 15s.
  • strategies - Custom activation strategies to be used.
  • disableMetrics - disable metrics

Custom strategies

1. implement the custom strategy:

const { Strategy, initialize } = require('unleash-client');
class ActiveForUserWithEmailStrategy extends Strategy {
    constructor() {
        super('ActiveForUserWithEmail');
    }

    isEnabled (parameters, context) {
        return parameters.emails.indexOf(context.email) !== -1;
    }
}

2. register your custom strategy:

initialize({
    url: 'http://unleash.herokuapp.com',
    strategies: [new ActiveForUserWithEmailStrategy()]
});

Alternative usage

Its also possible to ship the unleash instance around yourself, instead of using on the default require.cache to have share one instance.

const { Unleash } = require('unleash-client');


const instance = new Unleash({
    appName: 'my-app-name',
    url: 'http://unleash.herokuapp.com'
});

instance.on('ready', console.log.bind(console, 'ready'));
// required error handling when using instance directly
instance.on('error', console.error);