Skip to content

Commit 7faed09

Browse files
committed
added documentation, extended rather than duplicate unleash config options
Signed-off-by: jarebudev <[email protected]>
1 parent 2678eec commit 7faed09

File tree

4 files changed

+82
-39
lines changed

4 files changed

+82
-39
lines changed
Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,82 @@
11
# unleash-web Provider
22

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+
322
## Installation
423

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);
542
```
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);
767
```
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
875

9-
## Building
76+
### Building
1077

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

13-
## Running unit tests
80+
### Running unit tests
1481

1582
Run `nx test providers-unleash-web` to execute the unit tests via [Jest](https://jestjs.io).

libs/providers/unleash-web/src/lib/options.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import {
2+
IConfig
3+
} from 'unleash-proxy-client';
4+
5+
export interface UnleashConfig extends IConfig {
6+
}

libs/providers/unleash-web/src/lib/unleash-web-provider.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import {
1717
IMutableContext
1818
} from 'unleash-proxy-client';
1919
import {
20-
UnleashOptions,
21-
UnleashContextOptions
22-
} from './options';
20+
UnleashConfig
21+
} from './unleash-web-provider-config';
2322

2423
export class UnleashWebProvider implements Provider {
2524
metadata = {
@@ -31,24 +30,17 @@ export class UnleashWebProvider implements Provider {
3130
// logger is the OpenFeature logger to use
3231
private _logger?: Logger;
3332

34-
// options is the Unleash options provided to the provider
35-
private _options?: UnleashOptions;
33+
// config is the Unleash config provided to the provider
34+
private _config?: UnleashConfig;
3635

3736
// client is the Unleash client reference
3837
private _client?: UnleashClient;
3938

4039
readonly runsOn = 'client';
4140

42-
constructor(options: UnleashOptions, logger?: Logger) {
43-
this._options = options;
41+
constructor(config: UnleashConfig, logger?: Logger) {
42+
this._config = config;
4443
this._logger = logger;
45-
// TODO map all available options to unleash config - done minimum for now
46-
let config : IConfig = {
47-
url: options.url,
48-
clientKey: options.clientKey,
49-
appName: options.appName,
50-
refreshInterval: options.refreshInterval,
51-
};
5244
this._client = new UnleashClient(config);
5345
}
5446

@@ -149,7 +141,6 @@ export class UnleashWebProvider implements Provider {
149141
const evaluatedVariant = this._client?.getVariant(flagKey);
150142
let value;
151143
let variant
152-
this._logger?.debug("evaluatedVariant = " + JSON.stringify(evaluatedVariant));
153144
if (typeof evaluatedVariant === 'undefined') {
154145
throw new FlagNotFoundError();
155146
}

0 commit comments

Comments
 (0)