Skip to content
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

feat: Add Flagsmith Provider #836

Merged
merged 16 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"libs/providers/go-feature-flag-web": "0.2.0",
"libs/shared/flagd-core": "0.1.11",
"libs/shared/ofrep-core": "0.0.4-experimental",
"libs/providers/flipt": "0.1.0"
"libs/providers/flipt": "0.1.0",
"libs/providers/flagsmith": "0.1.0"
}
25 changes: 25 additions & 0 deletions libs/providers/flagsmith/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
91 changes: 91 additions & 0 deletions libs/providers/flagsmith/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Flagsmith Provider

This provider is an implementation for the [JavaScript SDK](https://docs.flagsmith.com/clients/javascript/) of [Flagsmith](https://flagsmith.com).
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved

## Installation

```
npm install @openfeature/flagsmith-provider @openfeature/web-sdk
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved
```

## Initialising 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.

```javascript
import { FlagsmithProvider } from '@openfeature/flagsmith-provider';
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved

const flagsmithFeatureFlagWebProvider = new FlagsmithProvider({
environmentID: '<ENVIRONMENT_ID>'
});
OpenFeature.setProvider(flagsmithFeatureFlagWebProvider); // Attach the provider to OpenFeature
```

## Usage with React Native, SSR or custom instances

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).

Note: In order to use the React Native implementation of OpenFeature you will need to install both Flagsmith and react-native-flagsmith.

```javascript
import flagsmith from 'react-native-flagsmith' // Could also be flagsmith/isomorphic, flagsmith-es or createFlagsmithInstance()
import { FlagsmithProvider } from '@openfeature/flagsmith-provider';
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved

const flagsmithFeatureFlagWebProvider = new FlagsmithProvider({
environmentID: '<ENVIRONMENT_ID>',
flagsmithInstance: flagsmith,
state:serverState
});
OpenFeature.setProvider(flagsmithFeatureFlagWebProvider); // Attach the provider to OpenFeature
```

## Identifying and setting Traits

In Flagsmith, users are [identified](https://docs.flagsmith.com/clients/javascript/#identifying-users) in order to allow for segmentation and percentage rollouts.

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``.
```javascript
const flagsmithFeatureFlagWebProvider = new FlagsmithProvider({
environmentID: '<ENVIRONMENT_ID>',
});
await OpenFeature.setContext({ targetingKey, traits });
OpenFeature.setProvider(flagsmithFeatureFlagWebProvider); // Attach the provider to OpenFeature
```

To reset the identity you can simply reset the context. This will do the equivalent of ``flagsmith.logout()``

```javascript
await OpenFeature.setContext({ });
```

## Resolution reasoning

In Flagsmith, features are evaluated based on the following [Resolution reasons](https://openfeature.dev/specification/types/#resolution-details):

```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.


## Events

The Flagsmith provider emits the
following [OpenFeature events](https://openfeature.dev/specification/types#provider-events):

- PROVIDER_READY
- PROVIDER_ERROR
- PROVIDER_CONFIGURATION_CHANGED

## Building

Run `nx package providers-flagsmith` 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).
3 changes: 3 additions & 0 deletions libs/providers/flagsmith/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["minify", { "builtIns": false }]]
}
9 changes: 9 additions & 0 deletions libs/providers/flagsmith/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable */
export default {
displayName: 'providers-flagsmith-client',
preset: '../../../jest.preset.js',
transform: {
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'js', 'html']
};
38 changes: 38 additions & 0 deletions libs/providers/flagsmith/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions libs/providers/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@openfeature/flagsmith",
kyle-ssg marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0"
},
"main": "./src/index.js",
"typings": "./src/index.d.ts",
"scripts": {
"publish-if-not-exists": "cp $NPM_CONFIG_USERCONFIG .npmrc && if [ \"$(npm show $npm_package_name@$npm_package_version version)\" = \"$(npm run current-version -s)\" ]; then echo 'already published, skipping'; else npm publish --access public; fi",
"current-version": "echo $npm_package_version"
},
"peerDependencies": {
"@openfeature/web-sdk": "^1.0.0"
}
}
76 changes: 76 additions & 0 deletions libs/providers/flagsmith/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "providers-flagsmith",
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "libs/providers/flagsmith/src",
"projectType": "library",
"targets": {
"publish": {
"executor": "nx:run-commands",
"options": {
"command": "npm run publish-if-not-exists",
"cwd": "dist/libs/providers/flagsmith"
},
"dependsOn": [
{
"projects": "self",
"target": "package"
}
]
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["libs/providers/flagsmith/**/*.ts", "libs/providers/flagsmith/package.json"]
}
},
"test": {
"executor": "@nx/jest:jest",
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
"options": {
"jestConfig": "libs/providers/flagsmith/jest.config.ts",
"passWithNoTests": true
},
"configurations": {
"ci": {
"ci": true,
"codeCoverage": true
}
}
},
"package": {
"executor": "@nx/rollup:rollup",
"outputs": ["{options.outputPath}"],
"options": {
"project": "libs/providers/flagsmith/package.json",
"outputPath": "dist/libs/providers/flagsmith",
"entryFile": "libs/providers/flagsmith/src/index.ts",
"tsConfig": "libs/providers/flagsmith/tsconfig.lib.json",
"buildableProjectDepsInPackageJsonType": "dependencies",
"compiler": "tsc",
"generateExportsField": true,
"umdName": "flagsmith",
"external": "all",
"format": ["cjs", "esm"],
"assets": [
{
"glob": "package.json",
"input": "./assets",
"output": "./src/"
},
{
"glob": "LICENSE",
"input": "./",
"output": "./"
},
{
"glob": "README.md",
"input": "./libs/providers/flagsmith",
"output": "./"
}
]
}
}
},
"tags": []
}
1 change: 1 addition & 0 deletions libs/providers/flagsmith/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/flagsmith-provider';
Loading
Loading