|
| 1 | +# unleash-web Provider |
| 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 | + |
| 22 | +## Installation |
| 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 snippets: |
| 31 | + |
| 32 | +### Initialization - without context |
| 33 | + |
| 34 | +```ts |
| 35 | +import { UnleashWebProvider } from '@openfeature/unleash-web-provider'; |
| 36 | + |
| 37 | +const provider = new UnleashWebProvider({ |
| 38 | + url: 'http://your.upstream.unleash.instance', |
| 39 | + clientKey: 'theclientkey', |
| 40 | + appName: 'your app', |
| 41 | +}); |
| 42 | + |
| 43 | +await OpenFeature.setProviderAndWait(provider); |
| 44 | +``` |
| 45 | + |
| 46 | +### Initialization - with context |
| 47 | + |
| 48 | +The [Unleash context](https://docs.getunleash.io/reference/unleash-context) can be set during creation of the provider. |
| 49 | + |
| 50 | +```ts |
| 51 | +import { UnleashWebProvider } from '@openfeature/unleash-web-provider'; |
| 52 | + |
| 53 | +const context = { |
| 54 | + userId: '123', |
| 55 | + sessionId: '456', |
| 56 | + remoteAddress: 'address', |
| 57 | + properties: { |
| 58 | + property1: 'property1', |
| 59 | + property2: 'property2', |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +const provider = new UnleashWebProvider({ |
| 64 | + url: 'http://your.upstream.unleash.instance', |
| 65 | + clientKey: 'theclientkey', |
| 66 | + appName: 'your app', |
| 67 | + context: context, |
| 68 | +}); |
| 69 | + |
| 70 | +await OpenFeature.setProviderAndWait(provider); |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### Available Constructor Configuration Options |
| 75 | + |
| 76 | +Unleash has a variety of configuration options that can be provided to the `UnleashWebProvider` constructor. |
| 77 | + |
| 78 | +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). |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +### After initialization |
| 84 | + |
| 85 | +After the provider gets initialized, you can start evaluations of feature flags like so: |
| 86 | + |
| 87 | +```ts |
| 88 | + |
| 89 | +// Get the client |
| 90 | +const client = await OpenFeature.getClient(); |
| 91 | + |
| 92 | +// You can now use the client to evaluate your flags |
| 93 | +const details = client.getBooleanValue('my-feature', false); |
| 94 | +``` |
| 95 | + |
| 96 | +The static evaluation context can be changed if needed |
| 97 | + |
| 98 | +```ts |
| 99 | +const evaluationCtx: EvaluationContext = { |
| 100 | + usedId: 'theuser', |
| 101 | + currentTime: 'time', |
| 102 | + sessionId: 'theSessionId', |
| 103 | + remoteAddress: 'theRemoteAddress', |
| 104 | + environment: 'theEnvironment', |
| 105 | + appName: 'theAppName', |
| 106 | + aCustomProperty: 'itsValue', |
| 107 | + anotherCustomProperty: 'somethingForIt', |
| 108 | +}; |
| 109 | + |
| 110 | +// changes the static evaluation context for OpenFeature |
| 111 | +await OpenFeature.setContext(evaluationCtx); |
| 112 | + |
| 113 | +``` |
| 114 | + |
| 115 | +## Contribute |
| 116 | + |
| 117 | +### Building |
| 118 | + |
| 119 | +Run `nx package providers-unleash-web` to build the library. |
| 120 | + |
| 121 | +### Running unit tests |
| 122 | + |
| 123 | +Run `nx test providers-unleash-web` to execute the unit tests via [Jest](https://jestjs.io). |
0 commit comments