|
1 |
| -# Flagsmith Provider |
| 1 | +# Flagsmith OpenFeature provider for client-side JavaScript |
2 | 2 |
|
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. |
4 | 4 |
|
5 | 5 | ## Installation
|
6 | 6 |
|
7 | 7 | ```
|
8 | 8 | npm install @openfeature/flagsmith-client-provider
|
9 | 9 | ```
|
10 | 10 |
|
11 |
| -## Initialising the provider |
| 11 | +Make sure that the SDK version is compatible with the `peerDependencies` one. |
12 | 12 |
|
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). |
14 | 16 |
|
15 | 17 | ```javascript
|
16 | 18 | import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider';
|
17 | 19 | import { OpenFeature } from '@openfeature/web-sdk';
|
18 | 20 |
|
19 | 21 | const flagsmithClientProvider = new FlagsmithClientProvider({
|
20 |
| - environmentID: '<ENVIRONMENT_ID>' |
| 22 | + environmentID: 'your_client_side_environment_key', |
| 23 | + cacheFlags: true, |
| 24 | + cacheOptions: { |
| 25 | + skipAPI: true, |
| 26 | + }, |
21 | 27 | });
|
22 |
| -OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature |
| 28 | +OpenFeature.setProvider(flagsmithClientProvider); |
23 | 29 | ```
|
24 | 30 |
|
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 |
26 | 36 |
|
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`: |
28 | 38 |
|
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: |
30 | 44 |
|
31 | 45 | ```javascript
|
32 |
| -import flagsmith from 'react-native-flagsmith' // Could also be flagsmith/isomorphic, flagsmith-es or createFlagsmithInstance() |
| 46 | +import flagsmith from 'react-native-flagsmith'; |
33 | 47 | import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider';
|
34 | 48 | import { OpenFeature } from '@openfeature/web-sdk';
|
35 | 49 |
|
36 | 50 | const flagsmithClientProvider = new FlagsmithClientProvider({
|
37 |
| - environmentID: '<ENVIRONMENT_ID>', |
38 |
| - flagsmithInstance: flagsmith, |
39 |
| - state: serverState, |
| 51 | + environmentID: 'your_client_side_environment_key', |
| 52 | + flagsmithInstance: flagsmith, |
40 | 53 | });
|
41 |
| -OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature |
| 54 | +OpenFeature.setProvider(flagsmithClientProvider); |
42 | 55 | ```
|
43 | 56 |
|
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. |
45 | 58 |
|
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 |
47 | 60 |
|
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: |
49 | 66 |
|
50 | 67 | ```javascript
|
51 |
| -const flagsmithClientProvider = new FlagsmithClientProvider({ |
52 |
| - environmentID: '<ENVIRONMENT_ID>', |
53 |
| -}); |
54 | 68 | await OpenFeature.setContext({
|
55 | 69 | targetingKey: 'my-identity-id',
|
56 | 70 | traits: {
|
57 | 71 | myTraitKey: 'my-trait-value',
|
58 | 72 | },
|
59 | 73 | });
|
60 |
| -OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature |
61 | 74 | ```
|
62 | 75 |
|
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: |
64 | 77 |
|
65 | 78 | ```javascript
|
66 |
| -await OpenFeature.setContext({ }); |
| 79 | +await OpenFeature.setContext({}); |
67 | 80 | ```
|
68 | 81 |
|
69 |
| -## Resolution reasoning |
| 82 | +## Resolution reasons |
70 | 83 |
|
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): |
72 | 85 |
|
73 | 86 | ```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'; |
78 | 88 |
|
| 89 | +type FlagsmithResolutionReasons = |
| 90 | + | typeof StandardResolutionReasons.STATIC |
| 91 | + | typeof StandardResolutionReasons.CACHED |
| 92 | + | typeof StandardResolutionReasons.DEFAULT |
| 93 | + | typeof StandardResolutionReasons.ERROR; |
| 94 | +``` |
79 | 95 |
|
80 | 96 | ## Events
|
81 | 97 |
|
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'; |
84 | 102 |
|
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 | +``` |
88 | 109 |
|
89 | 110 | ## Building
|
90 | 111 |
|
91 |
| -Run `nx package providers-flagsmith` to build the library. |
| 112 | +Run `nx package providers-flagsmith-client` to build the library. |
92 | 113 |
|
93 | 114 | ## Running unit tests
|
94 | 115 |
|
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). |
0 commit comments