Skip to content

Commit aeeed73

Browse files
committed
changed provider logging to debug. Tidied up README
Signed-off-by: jarebudev <[email protected]>
1 parent 9825c82 commit aeeed73

File tree

3 files changed

+78
-25
lines changed

3 files changed

+78
-25
lines changed

libs/providers/unleash-web/README.md

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,75 @@ $ npm install @openfeature/unleash-web-provider @openfeature/web-sdk
2727

2828
## Usage
2929

30-
To initialize the OpenFeature client with Unleash, you can use the following code snippet:
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.
3149

3250
```ts
3351
import { UnleashWebProvider } from '@openfeature/unleash-web-provider';
3452

53+
const context = {
54+
userId: '123',
55+
sessionId: '456',
56+
remoteAddress: 'address',
57+
properties: {
58+
property1: 'property1',
59+
property2: 'property2',
60+
},
61+
};
62+
3563
const provider = new UnleashWebProvider({
36-
url: 'http://your.upstream.unleash.instance',
37-
clientKey: 'theclientkey',
38-
appName: 'your app',
39-
});
64+
url: 'http://your.upstream.unleash.instance',
65+
clientKey: 'theclientkey',
66+
appName: 'your app',
67+
context: context,
68+
});
4069

4170
await OpenFeature.setProviderAndWait(provider);
4271
```
4372

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+
4485
After the provider gets initialized, you can start evaluations of feature flags like so:
4586

4687
```ts
47-
// Note - this can also be set within the contructor
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
4899
const evaluationCtx: EvaluationContext = {
49100
usedId: 'theuser',
50101
currentTime: 'time',
@@ -56,20 +107,10 @@ const evaluationCtx: EvaluationContext = {
56107
anotherCustomProperty: 'somethingForIt',
57108
};
58109

59-
// Set the static context for OpenFeature
110+
// changes the static evaluation context for OpenFeature
60111
await OpenFeature.setContext(evaluationCtx);
61112

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);
67113
```
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).
73114

74115
## Contribute
75116

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ describe('UnleashWebProvider', () => {
1717

1818
it('should be an instance of UnleashWebProvider', async () => {
1919
fetchMock.mockResponseOnce(JSON.stringify({ toggles: [] }));
20-
provider = new UnleashWebProvider({ url: endpoint, clientKey: 'clientsecret', appName: 'test' }, logger);
20+
const context = {
21+
userId: '123',
22+
sessionId: '456',
23+
remoteAddress: 'address',
24+
properties: {
25+
property1: 'property1',
26+
property2: 'property2',
27+
},
28+
};
29+
provider = new UnleashWebProvider(
30+
{ url: endpoint, clientKey: 'clientsecret', appName: 'test', context: context },
31+
logger,
32+
);
2133
await provider.initialize();
2234
expect(provider).toBeInstanceOf(UnleashWebProvider);
2335
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class UnleashWebProvider implements Provider {
4343

4444
async initialize(): Promise<void> {
4545
await this.initializeClient();
46-
this._logger?.info('UnleashWebProvider initialized');
46+
this._logger?.debug('UnleashWebProvider initialized');
4747
}
4848

4949
private async initializeClient() {
@@ -57,25 +57,25 @@ export class UnleashWebProvider implements Provider {
5757

5858
private registerEventListeners() {
5959
this._client?.on('ready', () => {
60-
this._logger?.info('Unleash ready event received');
60+
this._logger?.debug('Unleash ready event received');
6161
this.events.emit(ProviderEvents.Ready, {
6262
message: 'Ready',
6363
});
6464
});
6565
this._client?.on('update', () => {
66-
this._logger?.info('Unleash update event received');
66+
this._logger?.debug('Unleash update event received');
6767
this.events.emit(ProviderEvents.ConfigurationChanged, {
6868
message: 'Flags changed',
6969
});
7070
});
7171
this._client?.on('error', () => {
72-
this._logger?.info('Unleash error event received');
72+
this._logger?.debug('Unleash error event received');
7373
this.events.emit(ProviderEvents.Error, {
7474
message: 'Error',
7575
});
7676
});
7777
this._client?.on('recovered', () => {
78-
this._logger?.info('Unleash recovered event received');
78+
this._logger?.debug('Unleash recovered event received');
7979
this.events.emit(ProviderEvents.Ready, {
8080
message: 'Recovered',
8181
});
@@ -104,11 +104,11 @@ export class UnleashWebProvider implements Provider {
104104
unleashContext.set('properties', Object.fromEntries(properties));
105105
}
106106
await this._client?.updateContext(Object.fromEntries(unleashContext));
107-
this._logger?.info('Unleash context updated');
107+
this._logger?.debug('Unleash context updated');
108108
}
109109

110110
async onClose() {
111-
this._logger?.info('closing Unleash client');
111+
this._logger?.debug('closing Unleash client');
112112
this._client?.stop();
113113
}
114114

0 commit comments

Comments
 (0)