1.0.2 • Published 4 years ago

bizfly-unleash-proxy-client v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Unleash Proxy Client for the browser (js)

This is a tiny Unleash Client SDK you can use together with the Unleash Hosted Proxy. This makes it super simple to use Unleash-hosted from any single page app.

This client expect fetch to be available. If you need to support older browsers you should probably use the fetch polyfill.

How to use the client as a module.

Step 1: Install

npm install bizfly-unleash-proxy-client --save

Step 2: Initialize the SDK You need to have a Unleash-hosted instance, and the proxy need to be enabled. In addition you will need a proxy-specific clientKey in order to connect to the Unleash-hosted Proxy.

import { UnleashClient } from 'bizfly-unleash-proxy-client';

const unleash = new UnleashClient({
    url: 'https://hostname/hosted/api',
    clientKey: 'your-proxy-key',
    appName: 'my-webapp'
});

// Used to set the context fields, shared with the Unleash Proxy
unleash.updateContext({userId: '1233'});

// Start the background polling
unleash.start();

Step 3: Check if feature toggle is enabled

unleash.isEnabled('proxy.demo');

Step 4: Get toggle variant

const variant = unleash.getVariant('proxy.demo');
if(variant.name === 'blue') {
 // somehting with variant blue...
}

Listen for updates via the EventEmitter The client is also an event emitter. This means that your code can subscribe to updates from the client. This is a neat way to update a single page app when toggle state updates.

unleash.on('update', () => {
    const myToggle = unleash.isEnabled('proxy.demo');
    //do something useful
});

How to use the client via CDN.

<html>
<head>
    <script src="https://unpkg.com/bizfly-unleash-proxy-client@latest/build/main.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var config = {url: 'https://hostname/hosted/api', clientKey: 'some-proxy-key', appName: 'web'};
        var client = new unleash.UnleashClient(config);
        clinet.updateContext({userId: '1233'})

        client.on('update', () => {
            console.log(client.isEnabled('demo.toggle'));
        });
        client.start();
    </script>
</head>
</html>
1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago