-
Notifications
You must be signed in to change notification settings - Fork 59
fix(flagsmith): Fetch identity flags when context changes #1351
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rolodato
wants to merge
5
commits into
open-feature:main
Choose a base branch
from
rolodato:fix/flagsmith-update-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9f72fe3
Update README
rolodato dd18406
fix(flagsmith): Fetch identity flags when context changes (WIP)
rolodato 8ec4423
Map OpenFeature context to Flagsmith traits
rolodato d65ff3e
Merge branch 'main' into fix/flagsmith-update-context
rolodato 453c70c
comment
rolodato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,114 @@ | ||
# Flagsmith Provider | ||
# Flagsmith OpenFeature provider for client-side JavaScript | ||
|
||
This provider is an implementation for the [JavaScript SDK](https://docs.flagsmith.com/clients/javascript/) of [Flagsmith](https://flagsmith.com). | ||
[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. | ||
|
||
## Installation | ||
|
||
``` | ||
npm install @openfeature/flagsmith-client-provider | ||
``` | ||
|
||
## Initialising the provider | ||
## Initializing the provider | ||
|
||
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. | ||
The Flagsmith OpenFeature provider can be created with the same [initialization options as the Flagsmith SDK](https://docs.flagsmith.com/clients/javascript/#initialisation-options). | ||
|
||
```javascript | ||
import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider'; | ||
import { OpenFeature } from '@openfeature/web-sdk'; | ||
|
||
const flagsmithClientProvider = new FlagsmithClientProvider({ | ||
environmentID: '<ENVIRONMENT_ID>' | ||
environmentID: 'your_client_side_environment_key', | ||
cacheFlags: true, | ||
cacheOptions: { | ||
skipAPI: true | ||
} | ||
}); | ||
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature | ||
OpenFeature.setProvider(flagsmithClientProvider); | ||
``` | ||
|
||
## Usage with React Native, SSR or custom instances | ||
## Examples | ||
|
||
See our [examples repository](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature) for usage with various frameworks. | ||
|
||
## Usage with React Native | ||
|
||
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). | ||
To use the React Native implementation of OpenFeature, install `react-native-flagsmith`: | ||
|
||
Note: In order to use the React Native implementation of OpenFeature you will need to install both Flagsmith and react-native-flagsmith. | ||
``` | ||
npm install flagsmith react-native-flagsmith | ||
``` | ||
|
||
Then, pass the `flagsmith` instance from `react-native-flagsmith` when initializing the provider: | ||
|
||
```javascript | ||
import flagsmith from 'react-native-flagsmith' // Could also be flagsmith/isomorphic, flagsmith-es or createFlagsmithInstance() | ||
import flagsmith from 'react-native-flagsmith'; | ||
import { FlagsmithClientProvider } from '@openfeature/flagsmith-client-provider'; | ||
import { OpenFeature } from '@openfeature/web-sdk'; | ||
|
||
const flagsmithClientProvider = new FlagsmithClientProvider({ | ||
environmentID: '<ENVIRONMENT_ID>', | ||
environmentID: 'your_client_side_environment_key', | ||
flagsmithInstance: flagsmith, | ||
state: serverState, | ||
}); | ||
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature | ||
OpenFeature.setProvider(flagsmithClientProvider); | ||
``` | ||
|
||
## Identifying and setting Traits | ||
See the [React Native example application](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature/reactnative) for more details. | ||
|
||
## Flag targeting and dynamic evaluation | ||
|
||
In Flagsmith, users are [identified](https://docs.flagsmith.com/clients/javascript/#identifying-users) in order to allow for segmentation and percentage rollouts. | ||
In Flagsmith, users can be [identified](https://docs.flagsmith.com/clients/javascript/#identifying-users) to perform targeted flag rollouts. | ||
Traits are key-value pairs that can be used for [segment-based](https://docs.flagsmith.com/basic-features/segments) targeting. | ||
|
||
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``. | ||
Flagsmith identifiers and traits make up the [OpenFeature evaluation context](https://openfeature.dev/specification/glossary/#evaluation-context). | ||
They correspond to OpenFeature [targeting keys](https://openfeature.dev/docs/reference/concepts/evaluation-context/#targeting-key) and context attributes respectively: | ||
|
||
```javascript | ||
const flagsmithClientProvider = new FlagsmithClientProvider({ | ||
environmentID: '<ENVIRONMENT_ID>', | ||
}); | ||
await OpenFeature.setContext({ | ||
targetingKey: 'my-identity-id', | ||
traits: { | ||
myTraitKey: 'my-trait-value', | ||
}, | ||
}); | ||
OpenFeature.setProvider(flagsmithClientProvider); // Attach the provider to OpenFeature | ||
``` | ||
|
||
To reset the identity you can simply reset the context. This will do the equivalent of ``flagsmith.logout()`` | ||
To reset the identity, set the context to an empty object: | ||
|
||
```javascript | ||
await OpenFeature.setContext({ }); | ||
await OpenFeature.setContext({}); | ||
``` | ||
|
||
## Resolution reasoning | ||
## Resolution reasons | ||
|
||
In Flagsmith, features are evaluated based on the following [Resolution reasons](https://openfeature.dev/specification/types/#resolution-details): | ||
This provider supports the following [resolution reasons](https://openfeature.dev/specification/types/#resolution-reason): | ||
|
||
```typescript | ||
StandardResolutionReasons.CACHED | StandardResolutionReasons.STATIC | StandardResolutionReasons.DEFAULT | StandardResolutionReasons.ERROR | ||
``` | ||
|
||
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. | ||
import { StandardResolutionReasons } from '@openfeature/web-sdk'; | ||
|
||
type FlagsmithResolutionReasons = | ||
| typeof StandardResolutionReasons.STATIC | ||
| typeof StandardResolutionReasons.CACHED | ||
| typeof StandardResolutionReasons.DEFAULT | ||
| typeof StandardResolutionReasons.ERROR; | ||
``` | ||
|
||
## Events | ||
|
||
The Flagsmith provider emits the | ||
following [OpenFeature events](https://openfeature.dev/specification/types#provider-events): | ||
This provider emits the following [events](https://openfeature.dev/specification/types#provider-events): | ||
|
||
- PROVIDER_READY | ||
- PROVIDER_ERROR | ||
- PROVIDER_CONFIGURATION_CHANGED | ||
```typescript | ||
import { ProviderEvents } from '@openfeature/web-sdk'; | ||
|
||
type FlagsmithProviderEvents = | ||
| typeof ProviderEvents.Ready | ||
| typeof ProviderEvents.Stale | ||
| typeof ProviderEvents.ConfigurationChanged | ||
| typeof ProviderEvents.Error; | ||
``` | ||
|
||
## Building | ||
|
||
Run `nx package providers-flagsmith` to build the library. | ||
Run `nx package providers-flagsmith-client` to build the library. | ||
|
||
## Running unit tests | ||
|
||
Run `nx test providers-flagsmith` to execute the unit tests via [Jest](https://jestjs.io). | ||
|
||
## Examples | ||
|
||
You can find examples using this provider in several frameworks [Here](https://github.com/Flagsmith/flagsmith-js-examples/tree/main/open-feature). | ||
Run `nx test providers-flagsmith-client` to execute the unit tests via [Jest](https://jestjs.io). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see this isn't new behavior but I'm curious why this is necessary. Do you know why the logout would need to be called during initialization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It confused me as well for sure. I speculate that this was a shortcut, since this provider implements
onContextChange
by directly callinginitialize
: https://github.com/rolodato/js-sdk-contrib/blob/453c70cf829cfee02720e73d78736ddc89d82ce3/libs/providers/flagsmith-client/src/lib/flagsmith-client-provider.ts#L106A better solution could have been to keep the post-initialization logic in
onContextChange
, and only have initialization logic ininitialize
.After this fix, we'd still need to upgrade the Flagsmith SDK dependency to v9, since this provider uses a quite out of date version. I'd prefer leaving this PR as-is and make the code nicer when we're making that upgrade.