|
1 | 1 | # unleash-web Provider |
2 | 2 |
|
| 3 | +## About this provider |
| 4 | + |
| 5 | +This provider is a community-developed implementation for Unleash which uses the official [Unleash Proxy Client for the browser Client Side SDK](https://docs.getunleash.io/reference/sdks/javascript-browser). |
| 6 | + |
| 7 | +This provider uses a **static evaluation context** suitable for client-side implementation. |
| 8 | + |
| 9 | +Suitable for connecting to an Unleash instance |
| 10 | + |
| 11 | +* Via the [Unleash front-end API](https://docs.getunleash.io/reference/front-end-api). |
| 12 | +* Via [Unleash Edge](https://docs.getunleash.io/reference/unleash-edge). |
| 13 | +* Via [Unleash Proxy](https://docs.getunleash.io/reference/unleash-proxy). |
| 14 | + |
| 15 | +[Gitlab Feature Flags](https://docs.gitlab.com/ee/operations/feature_flags.html) can also be used with this provider - although note that Unleash Edge is not currently supported by Gitlab. |
| 16 | + |
| 17 | +### Concepts |
| 18 | +* Boolean evaluation gets feature enabled status. |
| 19 | +* String, Number, and Object evaluation gets feature variant value. |
| 20 | +* Object evaluation should be used for JSON/CSV payloads in variants. |
| 21 | + |
3 | 22 | ## Installation |
4 | 23 |
|
| 24 | +```shell |
| 25 | +$ npm install @openfeature/unleash-web-provider @openfeature/web-sdk |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +To initialize the OpenFeature client with Unleash, you can use the following code snippet: |
| 31 | + |
| 32 | +```ts |
| 33 | +import { UnleashWebProvider } from '@openfeature/unleash-web-provider'; |
| 34 | + |
| 35 | +const provider = new UnleashWebProvider({ |
| 36 | + url: 'http://your.upstream.unleash.instance', |
| 37 | + clientKey: 'theclientkey', |
| 38 | + appName: 'your app', |
| 39 | + }); |
| 40 | + |
| 41 | +await OpenFeature.setProviderAndWait(provider); |
5 | 42 | ``` |
6 | | -$ npm install @openfeature/unleash-web-provider |
| 43 | + |
| 44 | +After the provider gets initialized, you can start evaluations of feature flags like so: |
| 45 | + |
| 46 | +```ts |
| 47 | +// Note - this can also be set within the contructor |
| 48 | +const evaluationCtx: EvaluationContext = { |
| 49 | + usedId: 'theuser', |
| 50 | + currentTime: 'time', |
| 51 | + sessionId: 'theSessionId', |
| 52 | + remoteAddress: 'theRemoteAddress', |
| 53 | + environment: 'theEnvironment', |
| 54 | + appName: 'theAppName', |
| 55 | + aCustomProperty: 'itsValue', |
| 56 | + anotherCustomProperty: 'somethingForIt', |
| 57 | +}; |
| 58 | + |
| 59 | +// Set the static context for OpenFeature |
| 60 | +await OpenFeature.setContext(evaluationCtx); |
| 61 | + |
| 62 | +// Get the client |
| 63 | +const client = await OpenFeature.getClient(); |
| 64 | + |
| 65 | +// You can now use the client to evaluate your flags |
| 66 | +const details = client.getBooleanValue('my-feature', false); |
7 | 67 | ``` |
| 68 | +### Available Options |
| 69 | + |
| 70 | +Unleash has a variety of configuration options that can be provided to the the `UnleashWebProvider` constructor. |
| 71 | + |
| 72 | +Please refer to the options described in the official [Unleash Proxy Client for the browser Client Side SDK](https://docs.getunleash.io/reference/sdks/javascript-browser#available-options). |
| 73 | + |
| 74 | +## Contribute |
8 | 75 |
|
9 | | -## Building |
| 76 | +### Building |
10 | 77 |
|
11 | 78 | Run `nx package providers-unleash-web` to build the library. |
12 | 79 |
|
13 | | -## Running unit tests |
| 80 | +### Running unit tests |
14 | 81 |
|
15 | 82 | Run `nx test providers-unleash-web` to execute the unit tests via [Jest](https://jestjs.io). |
0 commit comments