Skip to content

Files

This branch is 1 commit behind open-feature/js-sdk-contrib:main.

unleash-web

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025
Jan 10, 2025

unleash-web Provider

About this provider

This provider is a community-developed implementation for Unleash which uses the official Unleash Proxy Client for the browser Client Side SDK.

This provider uses a static evaluation context suitable for client-side implementation.

Suitable for connecting to an Unleash instance

Gitlab Feature Flags can also be used with this provider - although note that Unleash Edge is not currently supported by Gitlab.

Concepts

  • Boolean evaluation gets feature enabled status.
  • String, Number, and Object evaluation gets feature variant value.
  • Object evaluation should be used for JSON/CSV payloads in variants.

Installation

$ npm install @openfeature/unleash-web-provider @openfeature/web-sdk

Usage

To initialize the OpenFeature client with Unleash, you can use the following code snippets:

Initialization - without context

import { UnleashWebProvider } from '@openfeature/unleash-web-provider';

const provider = new UnleashWebProvider({
    url: 'http://your.upstream.unleash.instance',
    clientKey: 'theclientkey',
    appName: 'your app',
});
  
await OpenFeature.setProviderAndWait(provider);

Initialization - with context

The Unleash context can be set during creation of the provider.

import { UnleashWebProvider } from '@openfeature/unleash-web-provider';

const context = {
    userId: '123',
    sessionId: '456',
    remoteAddress: 'address',
    properties: {
        property1: 'property1',
        property2: 'property2',
    },
};

const provider = new UnleashWebProvider({
    url: 'http://your.upstream.unleash.instance',
    clientKey: 'theclientkey',
    appName: 'your app',
    context: context,
});
  
await OpenFeature.setProviderAndWait(provider);

Available Constructor Configuration Options

Unleash has a variety of configuration options that can be provided to the UnleashWebProvider constructor.

Please refer to the options described in the official Unleash Proxy Client for the browser Client Side SDK.

After initialization

After the provider gets initialized, you can start evaluations of feature flags like so:

// Get the client 
const client = await OpenFeature.getClient();

// You can now use the client to evaluate your flags
const details = client.getBooleanValue('my-feature', false);

The static evaluation context can be changed if needed

const evaluationCtx: EvaluationContext = {
  usedId: 'theuser',
  currentTime: 'time',
  sessionId: 'theSessionId',
  remoteAddress: 'theRemoteAddress',
  environment: 'theEnvironment',
  appName: 'theAppName',
  aCustomProperty: 'itsValue',
  anotherCustomProperty: 'somethingForIt',
};

// changes the static evaluation context for OpenFeature
await OpenFeature.setContext(evaluationCtx);

Contribute

Building

Run nx package providers-unleash-web to build the library.

Running unit tests

Run nx test providers-unleash-web to execute the unit tests via Jest.