Skip to content
A browser client that can be used together with the unleash-proxy.
TypeScript JavaScript
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
tests
.gitignore
README.md
jest.config.js
package-lock.json
package.json
rollup.config.js
setupJest.js
tsconfig.json
tslint.json

README.md

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 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.

importUnleashClient } from 'unleash-proxy-client';

const unleash = new UnleashClient({
    url: 'https://eu.unleash-hosted.com/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/unleash-proxy-client@latest/build/main.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var config = {url: 'https://eu.unleash-hosted.com/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>
You can’t perform that action at this time.