Skip to content

Commit 35d4653

Browse files
feat(flagsmith): upgrade-provider-to-latest-flagsmith-client-version (#1362)
Signed-off-by: wadii <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
1 parent da9748a commit 35d4653

File tree

8 files changed

+415
-1388
lines changed

8 files changed

+415
-1388
lines changed
Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,116 @@
1-
# Flagsmith Provider
1+
# Flagsmith OpenFeature provider for client-side JavaScript
22

3-
This provider is an implementation for the [JavaScript SDK](https://docs.flagsmith.com/clients/javascript/) of [Flagsmith](https://flagsmith.com).
3+
[Flagsmith](https://flagsmith.com) is an open-source feature flagging and remote configuration service. This provider implements the [Flagsmith JavaScript SDK](https://flagsmith.com/docs/clients/javascript/) for client-side applications.
44

55
## Installation
66

77
```
88
npm install @openfeature/flagsmith-client-provider
99
```
1010

11-
## Initialising the provider
11+
Make sure that the SDK version is compatible with the `peerDependencies` one.
1212

13-
The Flagsmith Provider can be created with the standard [initialization options](https://docs.flagsmith.com/clients/javascript/#example-initialising-the-sdk) and an optional Flagsmith instance to use.
13+
## Initializing the provider
14+
15+
The Flagsmith OpenFeature provider can be created with the same [initialization options as the Flagsmith SDK](https://docs.flagsmith.com/clients/javascript/#initialisation-options).
1416

1517
```javascript
1618
import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider';
1719
import { OpenFeature } from '@openfeature/web-sdk';
1820

1921
const flagsmithClientProvider = new FlagsmithClientProvider({
20-
environmentID: '<ENVIRONMENT_ID>'
22+
environmentID: 'your_client_side_environment_key',
23+
cacheFlags: true,
24+
cacheOptions: {
25+
skipAPI: true,
26+
},
2127
});
22-
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature
28+
OpenFeature.setProvider(flagsmithClientProvider);
2329
```
2430

25-
## Usage with React Native, SSR or custom instances
31+
## Examples
32+
33+
See our [examples repository](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature) for usage with various frameworks.
34+
35+
## Usage with React Native
2636

27-
The Flagsmith Provider can be constructed with a custom Flagsmith instance and optional server-side generated state, [initialization options](https://docs.flagsmith.com/clients/javascript/#example-initialising-the-sdk).
37+
To use the React Native implementation of OpenFeature, install `react-native-flagsmith`:
2838

29-
Note: In order to use the React Native implementation of OpenFeature you will need to install both Flagsmith and react-native-flagsmith.
39+
```
40+
npm install flagsmith react-native-flagsmith
41+
```
42+
43+
Then, pass the `flagsmith` instance from `react-native-flagsmith` when initializing the provider:
3044

3145
```javascript
32-
import flagsmith from 'react-native-flagsmith' // Could also be flagsmith/isomorphic, flagsmith-es or createFlagsmithInstance()
46+
import flagsmith from 'react-native-flagsmith';
3347
import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider';
3448
import { OpenFeature } from '@openfeature/web-sdk';
3549

3650
const flagsmithClientProvider = new FlagsmithClientProvider({
37-
environmentID: '<ENVIRONMENT_ID>',
38-
flagsmithInstance: flagsmith,
39-
state: serverState,
51+
environmentID: 'your_client_side_environment_key',
52+
flagsmithInstance: flagsmith,
4053
});
41-
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature
54+
OpenFeature.setProvider(flagsmithClientProvider);
4255
```
4356

44-
## Identifying and setting Traits
57+
See the [React Native example application](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature/reactnative) for more details.
4558

46-
In Flagsmith, users are [identified](https://docs.flagsmith.com/clients/javascript/#identifying-users) in order to allow for segmentation and percentage rollouts.
59+
## Flag targeting and dynamic evaluation
4760

48-
To identify and set traits you can specify a targetingKey(identity) and optionally a set of traits. This will do the equivalent of ``flagsmith.identify(id, traits)`` or pass these to ``flagsmith.init`` if you are calling this before ``OpenFeature.setProvider``.
61+
In Flagsmith, users can be [identified](https://docs.flagsmith.com/clients/javascript/#identifying-users) to perform targeted flag rollouts.
62+
Traits are key-value pairs that can be used for [segment-based](https://docs.flagsmith.com/basic-features/segments) targeting.
63+
64+
Flagsmith identifiers and traits make up the [OpenFeature evaluation context](https://openfeature.dev/specification/glossary/#evaluation-context).
65+
They correspond to OpenFeature [targeting keys](https://openfeature.dev/docs/reference/concepts/evaluation-context/#targeting-key) and context attributes respectively:
4966

5067
```javascript
51-
const flagsmithClientProvider = new FlagsmithClientProvider({
52-
environmentID: '<ENVIRONMENT_ID>',
53-
});
5468
await OpenFeature.setContext({
5569
targetingKey: 'my-identity-id',
5670
traits: {
5771
myTraitKey: 'my-trait-value',
5872
},
5973
});
60-
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature
6174
```
6275

63-
To reset the identity you can simply reset the context. This will do the equivalent of ``flagsmith.logout()``
76+
To reset the identity, set the context to an empty object:
6477

6578
```javascript
66-
await OpenFeature.setContext({ });
79+
await OpenFeature.setContext({});
6780
```
6881

69-
## Resolution reasoning
82+
## Resolution reasons
7083

71-
In Flagsmith, features are evaluated based on the following [Resolution reasons](https://openfeature.dev/specification/types/#resolution-details):
84+
This provider supports the following [resolution reasons](https://openfeature.dev/specification/types/#resolution-reason):
7285

7386
```typescript
74-
StandardResolutionReasons.CACHED | StandardResolutionReasons.STATIC | StandardResolutionReasons.DEFAULT | StandardResolutionReasons.ERROR
75-
```
76-
77-
Note that resolutions of type SPLIT may be the result of targetted matching or percentage split however Flagsmith does not expose this information to client-side SDKs.
87+
import { StandardResolutionReasons } from '@openfeature/web-sdk';
7888

89+
type FlagsmithResolutionReasons =
90+
| typeof StandardResolutionReasons.STATIC
91+
| typeof StandardResolutionReasons.CACHED
92+
| typeof StandardResolutionReasons.DEFAULT
93+
| typeof StandardResolutionReasons.ERROR;
94+
```
7995

8096
## Events
8197

82-
The Flagsmith provider emits the
83-
following [OpenFeature events](https://openfeature.dev/specification/types#provider-events):
98+
This provider emits the following [events](https://openfeature.dev/specification/types#provider-events):
99+
100+
```typescript
101+
import { ProviderEvents } from '@openfeature/web-sdk';
84102

85-
- PROVIDER_READY
86-
- PROVIDER_ERROR
87-
- PROVIDER_CONFIGURATION_CHANGED
103+
type FlagsmithProviderEvents =
104+
| typeof ProviderEvents.Ready
105+
| typeof ProviderEvents.Stale
106+
| typeof ProviderEvents.ConfigurationChanged
107+
| typeof ProviderEvents.Error;
108+
```
88109

89110
## Building
90111

91-
Run `nx package providers-flagsmith` to build the library.
112+
Run `nx package providers-flagsmith-client` to build the library.
92113

93114
## Running unit tests
94115

95-
Run `nx test providers-flagsmith` to execute the unit tests via [Jest](https://jestjs.io).
96-
97-
## Examples
98-
99-
You can find examples using this provider in several frameworks [Here](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature).
116+
Run `nx test providers-flagsmith-client` to execute the unit tests via [Jest](https://jestjs.io).

libs/providers/flagsmith-client/package-lock.json

Lines changed: 5 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/providers/flagsmith-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"peerDependencies": {
1212
"@openfeature/web-sdk": "^1.0.0",
13-
"flagsmith": "^4.1.4"
13+
"flagsmith": "^9.3.1"
1414
},
1515
"dependencies": {}
1616
}

0 commit comments

Comments
 (0)