-
Notifications
You must be signed in to change notification settings - Fork 53
feat: Init web-sdk go-feature-flag #474
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
Merged
thomaspoignant
merged 29 commits into
open-feature:main
from
thomaspoignant:add-web-goff
Jul 31, 2023
Merged
Changes from 21 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
4382eb0
Init web-sdk go-feature-flag
thomaspoignant 4ff5f02
Fix PR CI Issue
thomaspoignant 3fd1d8b
Merge branch 'main' into add-web-goff
thomaspoignant 298745e
Fix CI failure
thomaspoignant b6a0035
Fix jest configuration
thomaspoignant f48f076
Remove unused errors
thomaspoignant 08e0826
Code style
thomaspoignant 584748b
Add readme
thomaspoignant 8fbeaee
Merge branch 'main' into add-web-goff
thomaspoignant 8ed2a9d
Send information in the emit function
thomaspoignant 26aca58
Merge remote-tracking branch 'origin/main' into add-web-goff
thomaspoignant d51ca77
Addin myself has a code owner of this new component
thomaspoignant 241260b
Fix doc mentioning flagd + change event type to match the example
thomaspoignant c7a9590
address review comment from Todd
thomaspoignant 632735e
add retry mechanism to reload the flags
thomaspoignant af6c468
Merge branch 'main' into add-web-goff
thomaspoignant 47d5188
Removing the experimental mention
thomaspoignant 2ee79cd
manage apiKey for the websocket
thomaspoignant 204bda2
Merge branch 'main' into add-web-goff
thomaspoignant 5f38c79
removing axios
thomaspoignant 9499f84
Emit events when context is changing
thomaspoignant bce42db
fix log
thomaspoignant cafc245
Merge branch 'main' into add-web-goff
thomaspoignant ef673e8
Add prefix for logs
thomaspoignant 91e3680
Fixing the version of SDK to 0.4.0
thomaspoignant ecc62ee
Merge branch 'main' into add-web-goff
beeme1mr 432d6c3
Merge branch 'main' into add-web-goff
beeme1mr b206192
Fix test suite
thomaspoignant 224a402
Merge branch 'main' into add-web-goff
thomaspoignant 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"extends": ["../../../.eslintrc.json"], | ||
"ignorePatterns": ["!**/*"], | ||
"overrides": [ | ||
{ | ||
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.ts", "*.tsx"], | ||
"rules": {} | ||
}, | ||
{ | ||
"files": ["*.js", "*.jsx"], | ||
"rules": {} | ||
} | ||
] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# go-feature-flag-web Provider for OpenFeature | ||
## About this provider | ||
[GO Feature Flag](https://gofeatureflag.org) provider allows you to connect to your GO Feature Flag instance with the `@openfeature/web-sdk`. | ||
|
||
The main difference between this provider and [`@openfeature/go-feature-flag-provider`](https://www.npmjs.com/package/@openfeature/go-feature-flag-provider) is that it uses a **static evaluation context**. | ||
This provider is more sustainable for client-side implementation. | ||
|
||
If you want to know more about this pattern, I encourage you to read this [blog post](https://openfeature.dev/blog/catering-to-the-client-side/). | ||
|
||
## What is GO Feature Flag? | ||
GO Feature Flag is a simple, complete and lightweight self-hosted feature flag solution 100% Open Source. | ||
Our focus is to avoid any complex infrastructure work to use GO Feature Flag. | ||
|
||
This is a complete feature flagging solution with the possibility to target only a group of users, use any types of flags, store your configuration in various location and advanced rollout functionality. You can also collect usage data of your flags and be notified of configuration changes. | ||
|
||
## Install the provider | ||
|
||
```shell | ||
npm install @openfeature/go-feature-flag-web-provider @openfeature/web-sdk | ||
``` | ||
|
||
## How to use the provider? | ||
```typescript | ||
const evaluationCtx: EvaluationContext = { | ||
targetingKey: 'user-key', | ||
email: '[email protected]', | ||
name: 'John Doe', | ||
}; | ||
|
||
const goFeatureFlagWebProvider = new GoFeatureFlagWebProvider({ | ||
endpoint: endpoint, | ||
// ... | ||
}, logger); | ||
|
||
await OpenFeature.setContext(evaluationCtx); // Set the static context for OpenFeature | ||
OpenFeature.setProvider(goFeatureFlagWebProvider); // Attach the provider to OpenFeature | ||
const client = await OpenFeature.getClient(); | ||
|
||
// You can now use the client to use your flags | ||
if(client.getBooleanValue('my-new-feature', false)){ | ||
//... | ||
} | ||
|
||
// You can add handlers to know what happen in the provider | ||
client.addHandler(ProviderEvents.Ready, () => { ... }); | ||
client.addHandler(ProviderEvents.Error, () => { //... }); | ||
client.addHandler(ProviderEvents.Stale, () => { //... }); | ||
client.addHandler(ProviderEvents.ConfigurationChanged, () => { //... }); | ||
``` | ||
|
||
### Available options | ||
| Option name | Type | Default | Description | | ||
|-------------------------------|--------|----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | ||
| endpoint | string | | endpoint is the URL where your GO Feature Flag server is located. | | ||
| apiTimeout | number | 0 = no timeout | (optional) timeout is the time in millisecond we wait for an answer from the server. | | ||
| apiKey | string | | (optional) If GO Feature Flag is configured to authenticate the requests, you should provide an API Key to the provider. Please ask the administrator of the relay proxy to provide an API Key. | | ||
| websocketRetryInitialDelay | number | 100 | (optional) initial delay in millisecond to wait before retrying to connect the websocket | | ||
| websocketRetryDelayMultiplier | number | 2 | (optional) multiplier of websocketRetryInitialDelay after each failure _(example: 1st connection retry will be after 100ms, second after 200ms, third after 400ms ...)_ | | ||
| websocketMaxRetries | number | 10 | (optional) maximum number of retries before considering the websocket unreachable | | ||
|
||
### Reconnection | ||
If the connection to the GO Feature Flag instance fails, the provider will attempt to reconnect with an exponential back-off. | ||
The `websocketMaxRetries` can be specified to customize reconnect behavior. | ||
|
||
### Event streaming | ||
The `GoFeatureFlagWebProvider` receives events from GO Feature Flag with changes. | ||
Combined with the event API in the web SDK, this allows for subscription to flag value changes in clients. | ||
|
||
```typescript | ||
client.addHandler(ProviderEvents.ConfigurationChanged, (ctx: EventDetails) => { | ||
// do something when the configuration has changed. | ||
// ctx.flagsChanged contains the list of changed flags. | ||
}); | ||
``` | ||
|
||
## Contribute | ||
|
||
### Building | ||
Run `nx package providers-go-feature-flag-web` to build the library. | ||
|
||
### Running unit tests | ||
Run `nx test providers-go-feature-flag-web` 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [["minify", { "builtIns": false }]] | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'providers-go-feature-flag-web', | ||
preset: '../../../jest.preset.js', | ||
globals: { | ||
'ts-jest': { | ||
tsconfig: '<rootDir>/tsconfig.spec.json', | ||
}, | ||
}, | ||
transform: { | ||
'^.+\\.[tj]s$': 'ts-jest', | ||
}, | ||
testEnvironment: 'jsdom', | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/providers/go-feature-flag-web', | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@openfeature/go-feature-flag-web-provider", | ||
"version": "0.0.1", | ||
"type": "commonjs", | ||
"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": "*" | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
{ | ||
"name": "providers-go-feature-flag-web", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/providers/go-feature-flag-web/src", | ||
"projectType": "library", | ||
"targets": { | ||
"publish": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"command": "npm run publish-if-not-exists", | ||
"cwd": "dist/libs/providers/go-feature-flag-web" | ||
}, | ||
"dependsOn": [ | ||
{ | ||
"projects": "self", | ||
"target": "package" | ||
} | ||
] | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/providers/go-feature-flag-web/**/*.ts"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/providers/go-feature-flag-web/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"package": { | ||
"executor": "@nx/rollup:rollup", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"project": "libs/providers/go-feature-flag-web/package.json", | ||
"outputPath": "dist/libs/providers/go-feature-flag-web", | ||
"entryFile": "libs/providers/go-feature-flag-web/src/index.ts", | ||
"tsConfig": "libs/providers/go-feature-flag-web/tsconfig.lib.json", | ||
"buildableProjectDepsInPackageJsonType": "dependencies", | ||
"compiler": "tsc", | ||
"generateExportsField": true, | ||
"umdName": "go-feature-flag-web", | ||
"external": "all", | ||
"format": ["cjs", "esm"], | ||
"assets": [ | ||
{ | ||
"glob": "package.json", | ||
"input": "./assets", | ||
"output": "./src/" | ||
}, | ||
{ | ||
"glob": "LICENSE", | ||
"input": "./", | ||
"output": "./" | ||
}, | ||
{ | ||
"glob": "README.md", | ||
"input": "./libs/providers/go-feature-flag-web", | ||
"output": "./" | ||
} | ||
] | ||
} | ||
} | ||
}, | ||
"tags": [] | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './lib/go-feature-flag-web-provider'; |
63 changes: 63 additions & 0 deletions
63
libs/providers/go-feature-flag-web/src/lib/context-transfomer.spec.ts
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import {EvaluationContext} from '@openfeature/js-sdk'; | ||
import {GoFeatureFlagEvaluationContext} from './model'; | ||
import {transformContext} from './context-transformer'; | ||
import {TargetingKeyMissingError} from "@openfeature/web-sdk"; | ||
|
||
describe('contextTransformer', () => { | ||
it('should use the targetingKey as user key', () => { | ||
const got = transformContext({ | ||
targetingKey: 'user-key', | ||
} as EvaluationContext); | ||
const want: GoFeatureFlagEvaluationContext = { | ||
key: 'user-key', | ||
custom: {}, | ||
}; | ||
expect(got).toEqual(want); | ||
}); | ||
|
||
it('should specify the anonymous field base on attributes', () => { | ||
const got = transformContext({ | ||
targetingKey: 'user-key', | ||
anonymous: true, | ||
} as EvaluationContext); | ||
const want: GoFeatureFlagEvaluationContext = { | ||
key: 'user-key', | ||
custom: { | ||
anonymous: true, | ||
}, | ||
}; | ||
expect(got).toEqual(want); | ||
}); | ||
|
||
it('should hash the context as key if no targetingKey provided', () => { | ||
expect(() => { | ||
transformContext({ | ||
anonymous: true, | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
email: '[email protected]', | ||
} as EvaluationContext); | ||
}).toThrow(TargetingKeyMissingError); | ||
}); | ||
|
||
it('should fill custom fields if extra field are present', () => { | ||
const got = transformContext({ | ||
targetingKey: 'user-key', | ||
anonymous: true, | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
email: '[email protected]', | ||
} as EvaluationContext); | ||
|
||
const want: GoFeatureFlagEvaluationContext = { | ||
key: 'user-key', | ||
custom: { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
email: '[email protected]', | ||
anonymous: true, | ||
}, | ||
}; | ||
expect(got).toEqual(want); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
libs/providers/go-feature-flag-web/src/lib/context-transformer.ts
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {EvaluationContext} from '@openfeature/js-sdk'; | ||
import {GoFeatureFlagEvaluationContext} from './model'; | ||
import {TargetingKeyMissingError} from "@openfeature/web-sdk"; | ||
|
||
/** | ||
* transformContext takes the raw OpenFeature context returns a GoFeatureFlagEvaluationContext. | ||
* @param context - the context used for flag evaluation. | ||
* @returns {GoFeatureFlagEvaluationContext} the user against who we will evaluate the flag. | ||
*/ | ||
export function transformContext( | ||
context: EvaluationContext | ||
): GoFeatureFlagEvaluationContext { | ||
const {targetingKey, ...attributes} = context; | ||
if (targetingKey === undefined || targetingKey === null || targetingKey === '') { | ||
throw new TargetingKeyMissingError(); | ||
} | ||
return { | ||
key: targetingKey, | ||
custom: attributes, | ||
}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* FetchError is a wrapper around the HTTP error returned by | ||
* the method fetch. | ||
* It allows to throw an error with the status code. | ||
*/ | ||
export class FetchError extends Error{ | ||
status: number; | ||
constructor(status:number) { | ||
super(`Request failed with status code ${status}`); | ||
this.status = status; | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.