-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add unleash web provider #1105
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
toddbaert
merged 20 commits into
open-feature:main
from
jarebudev:feat/add_unleash_provider
Jan 10, 2025
Merged
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
21c7f11
initial unleash provider - wip
jarebudev 1ad0bea
merge in main and resolve conflict
jarebudev ca52bf1
added evaluation tests
jarebudev 2678eec
added event tests
jarebudev 7faed09
added documentation, extended rather than duplicate unleash config op…
jarebudev 711a41a
Merge branch 'main' into feat/add_unleash_provider
jarebudev 7c8a41b
linting fixes applied
jarebudev 10f70d1
added context change tests, tsconfig options
jarebudev 2cee133
removed unnecessary cast. Added component owner
jarebudev a95aa2b
added checks to ensure that returned variant matches requested flag t…
jarebudev c0203e7
Merge branch 'main' into feat/add_unleash_provider
jarebudev 1e27396
Applied suggested package.json changes to resolve CI building. Correc…
jarebudev a0229d9
applied linting fixes
jarebudev 807a2ec
Merge branch 'main' into feat/add_unleash_provider
lukas-reining 9825c82
resolved merge conflicts
jarebudev aeeed73
changed provider logging to debug. Tidied up README
jarebudev 443f3df
merge in main
jarebudev afd45c5
merge in main, resolve merge conflicts with growthbook
jarebudev 6c108e0
fixup: submodules
toddbaert 510c77e
Merge branch 'main' into feat/add_unleash_provider
toddbaert 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
Submodule schemas
updated
from b81a56 to 76d611
Submodule schemas
updated
from b81a56 to 2aa89b
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,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" | ||
} | ||
} | ||
] | ||
} |
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 @@ | ||
# unleash-web Provider | ||
|
||
## About this provider | ||
|
||
This provider is a community-developed implementation for Unleash which uses the official [Unleash Proxy Client for the browser Client Side SDK](https://docs.getunleash.io/reference/sdks/javascript-browser). | ||
|
||
This provider uses a **static evaluation context** suitable for client-side implementation. | ||
|
||
Suitable for connecting to an Unleash instance | ||
|
||
* Via the [Unleash front-end API](https://docs.getunleash.io/reference/front-end-api). | ||
* Via [Unleash Edge](https://docs.getunleash.io/reference/unleash-edge). | ||
* Via [Unleash Proxy](https://docs.getunleash.io/reference/unleash-proxy). | ||
|
||
[Gitlab Feature Flags](https://docs.gitlab.com/ee/operations/feature_flags.html) can also be used with this provider - although note that Unleash Edge is not currently supported by Gitlab. | ||
|
||
### Concepts | ||
* Boolean evaluation gets feature enabled status. | ||
* String, Number, and Object evaluation gets feature variant value. | ||
* Object evaluation should be used for JSON/CSV payloads in variants. | ||
|
||
## Installation | ||
|
||
```shell | ||
$ npm install @openfeature/unleash-web-provider @openfeature/web-sdk | ||
``` | ||
|
||
## Usage | ||
|
||
To initialize the OpenFeature client with Unleash, you can use the following code snippet: | ||
|
||
```ts | ||
import { UnleashWebProvider } from '@openfeature/unleash-web-provider'; | ||
|
||
const provider = new UnleashWebProvider({ | ||
url: 'http://your.upstream.unleash.instance', | ||
clientKey: 'theclientkey', | ||
appName: 'your app', | ||
}); | ||
|
||
await OpenFeature.setProviderAndWait(provider); | ||
``` | ||
|
||
After the provider gets initialized, you can start evaluations of feature flags like so: | ||
|
||
```ts | ||
// Note - this can also be set within the contructor | ||
toddbaert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const evaluationCtx: EvaluationContext = { | ||
usedId: 'theuser', | ||
currentTime: 'time', | ||
sessionId: 'theSessionId', | ||
remoteAddress: 'theRemoteAddress', | ||
environment: 'theEnvironment', | ||
appName: 'theAppName', | ||
aCustomProperty: 'itsValue', | ||
anotherCustomProperty: 'somethingForIt', | ||
}; | ||
|
||
// Set the static context for OpenFeature | ||
await OpenFeature.setContext(evaluationCtx); | ||
|
||
// Get the client | ||
const client = await OpenFeature.getClient(); | ||
|
||
// You can now use the client to evaluate your flags | ||
const details = client.getBooleanValue('my-feature', false); | ||
``` | ||
### Available Options | ||
|
||
Unleash has a variety of configuration options that can be provided to the the `UnleashWebProvider` constructor. | ||
|
||
Please refer to the options described in the official [Unleash Proxy Client for the browser Client Side SDK](https://docs.getunleash.io/reference/sdks/javascript-browser#available-options). | ||
|
||
## Contribute | ||
|
||
### Building | ||
|
||
Run `nx package providers-unleash-web` to build the library. | ||
|
||
### Running unit tests | ||
|
||
Run `nx test providers-unleash-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,10 @@ | ||
/* eslint-disable */ | ||
export default { | ||
displayName: 'providers-unleash-web', | ||
preset: '../../../jest.preset.js', | ||
transform: { | ||
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }], | ||
}, | ||
moduleFileExtensions: ['ts', 'js', 'html'], | ||
coverageDirectory: '../../../coverage/libs/providers/unleash-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,15 @@ | ||
{ | ||
"name": "@openfeature/unleash-web-provider", | ||
"version": "0.1.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", | ||
"tslib": "^2.3.0", | ||
"unleash-proxy-client": "^3.6.0" | ||
} | ||
} |
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,77 @@ | ||
{ | ||
"name": "providers-unleash-web", | ||
"$schema": "../../../node_modules/nx/schemas/project-schema.json", | ||
"sourceRoot": "libs/providers/unleash-web/src", | ||
"projectType": "library", | ||
"targets": { | ||
"publish": { | ||
"executor": "nx:run-commands", | ||
"options": { | ||
"command": "npm run publish-if-not-exists", | ||
"cwd": "dist/libs/providers/unleash-web" | ||
}, | ||
"dependsOn": [ | ||
{ | ||
"projects": "self", | ||
"target": "package" | ||
} | ||
] | ||
}, | ||
"lint": { | ||
"executor": "@nx/linter:eslint", | ||
"outputs": ["{options.outputFile}"], | ||
"options": { | ||
"lintFilePatterns": ["libs/providers/unleash-web/**/*.ts", "libs/providers/unleash-web/package.json"] | ||
} | ||
}, | ||
"test": { | ||
"executor": "@nx/jest:jest", | ||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"], | ||
"options": { | ||
"jestConfig": "libs/providers/unleash-web/jest.config.ts", | ||
"passWithNoTests": true | ||
}, | ||
"configurations": { | ||
"ci": { | ||
"ci": true, | ||
"codeCoverage": true | ||
} | ||
} | ||
}, | ||
"package": { | ||
"executor": "@nx/rollup:rollup", | ||
"outputs": ["{options.outputPath}"], | ||
"options": { | ||
"project": "libs/providers/unleash-web/package.json", | ||
"outputPath": "dist/libs/providers/unleash-web", | ||
"entryFile": "libs/providers/unleash-web/src/index.ts", | ||
"tsConfig": "libs/providers/unleash-web/tsconfig.lib.json", | ||
"buildableProjectDepsInPackageJsonType": "dependencies", | ||
"updateBuildableProjectDepsInPackageJson": true, | ||
"compiler": "tsc", | ||
"generateExportsField": true, | ||
"umdName": "unleash-web", | ||
"external": "all", | ||
"format": ["cjs", "esm"], | ||
"assets": [ | ||
{ | ||
"glob": "package.json", | ||
"input": "./assets", | ||
"output": "./src/" | ||
}, | ||
{ | ||
"glob": "LICENSE", | ||
"input": "./", | ||
"output": "./" | ||
}, | ||
{ | ||
"glob": "README.md", | ||
"input": "./libs/providers/unleash-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/unleash-web-provider'; |
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,39 @@ | ||
/** | ||
* TestLogger is a logger build for testing purposes. | ||
* This is not ready to be production ready, so please avoid using it. | ||
*/ | ||
export default class TestLogger { | ||
public inMemoryLogger: Record<string, string[]> = { | ||
error: [], | ||
warn: [], | ||
info: [], | ||
debug: [], | ||
}; | ||
|
||
error(...args: unknown[]): void { | ||
this.inMemoryLogger['error'].push(args.join(' ')); | ||
} | ||
|
||
warn(...args: unknown[]): void { | ||
this.inMemoryLogger['warn'].push(args.join(' ')); | ||
} | ||
|
||
info(...args: unknown[]): void { | ||
console.log(args); | ||
this.inMemoryLogger['info'].push(args.join(' ')); | ||
} | ||
|
||
debug(...args: unknown[]): void { | ||
console.log(args); | ||
this.inMemoryLogger['debug'].push(args.join(' ')); | ||
} | ||
|
||
reset() { | ||
this.inMemoryLogger = { | ||
error: [], | ||
warn: [], | ||
info: [], | ||
debug: [], | ||
}; | ||
} | ||
} |
Oops, something went wrong.
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.