Skip to content

Commit 19accf8

Browse files
feat: add unleash web provider (open-feature#1105)
Signed-off-by: jarebudev <[email protected]> Co-authored-by: Lukas Reining <[email protected]>
1 parent b1c6d23 commit 19accf8

22 files changed

+1032
-4
lines changed

.github/component_owners.yml

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ components:
3030
- markphelps
3131
libs/providers/flipt-web:
3232
- markphelps
33+
libs/providers/unleash-web:
34+
- jarebudev
3335

3436
ignored-authors:
3537
- renovate-bot

.release-please-manifest.json

+1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"libs/providers/growthbook-client": "0.1.2",
2020
"libs/providers/config-cat-web": "0.1.3",
2121
"libs/shared/config-cat-core": "0.1.0",
22+
"libs/providers/unleash-web": "0.1.0",
2223
"libs/providers/growthbook": "0.1.1"
2324
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"extends": ["../../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
},
17+
{
18+
"files": ["*.json"],
19+
"parser": "jsonc-eslint-parser",
20+
"rules": {
21+
"@nx/dependency-checks": "error"
22+
}
23+
}
24+
]
25+
}

libs/providers/unleash-web/README.md

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# unleash-web Provider
2+
3+
## About this provider
4+
5+
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).
6+
7+
This provider uses a **static evaluation context** suitable for client-side implementation.
8+
9+
Suitable for connecting to an Unleash instance
10+
11+
* Via the [Unleash front-end API](https://docs.getunleash.io/reference/front-end-api).
12+
* Via [Unleash Edge](https://docs.getunleash.io/reference/unleash-edge).
13+
* Via [Unleash Proxy](https://docs.getunleash.io/reference/unleash-proxy).
14+
15+
[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.
16+
17+
### Concepts
18+
* Boolean evaluation gets feature enabled status.
19+
* String, Number, and Object evaluation gets feature variant value.
20+
* Object evaluation should be used for JSON/CSV payloads in variants.
21+
22+
## Installation
23+
24+
```shell
25+
$ npm install @openfeature/unleash-web-provider @openfeature/web-sdk
26+
```
27+
28+
## Usage
29+
30+
To initialize the OpenFeature client with Unleash, you can use the following code snippets:
31+
32+
### Initialization - without context
33+
34+
```ts
35+
import { UnleashWebProvider } from '@openfeature/unleash-web-provider';
36+
37+
const provider = new UnleashWebProvider({
38+
url: 'http://your.upstream.unleash.instance',
39+
clientKey: 'theclientkey',
40+
appName: 'your app',
41+
});
42+
43+
await OpenFeature.setProviderAndWait(provider);
44+
```
45+
46+
### Initialization - with context
47+
48+
The [Unleash context](https://docs.getunleash.io/reference/unleash-context) can be set during creation of the provider.
49+
50+
```ts
51+
import { UnleashWebProvider } from '@openfeature/unleash-web-provider';
52+
53+
const context = {
54+
userId: '123',
55+
sessionId: '456',
56+
remoteAddress: 'address',
57+
properties: {
58+
property1: 'property1',
59+
property2: 'property2',
60+
},
61+
};
62+
63+
const provider = new UnleashWebProvider({
64+
url: 'http://your.upstream.unleash.instance',
65+
clientKey: 'theclientkey',
66+
appName: 'your app',
67+
context: context,
68+
});
69+
70+
await OpenFeature.setProviderAndWait(provider);
71+
```
72+
73+
74+
### Available Constructor Configuration Options
75+
76+
Unleash has a variety of configuration options that can be provided to the `UnleashWebProvider` constructor.
77+
78+
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).
79+
80+
81+
82+
83+
### After initialization
84+
85+
After the provider gets initialized, you can start evaluations of feature flags like so:
86+
87+
```ts
88+
89+
// Get the client
90+
const client = await OpenFeature.getClient();
91+
92+
// You can now use the client to evaluate your flags
93+
const details = client.getBooleanValue('my-feature', false);
94+
```
95+
96+
The static evaluation context can be changed if needed
97+
98+
```ts
99+
const evaluationCtx: EvaluationContext = {
100+
usedId: 'theuser',
101+
currentTime: 'time',
102+
sessionId: 'theSessionId',
103+
remoteAddress: 'theRemoteAddress',
104+
environment: 'theEnvironment',
105+
appName: 'theAppName',
106+
aCustomProperty: 'itsValue',
107+
anotherCustomProperty: 'somethingForIt',
108+
};
109+
110+
// changes the static evaluation context for OpenFeature
111+
await OpenFeature.setContext(evaluationCtx);
112+
113+
```
114+
115+
## Contribute
116+
117+
### Building
118+
119+
Run `nx package providers-unleash-web` to build the library.
120+
121+
### Running unit tests
122+
123+
Run `nx test providers-unleash-web` to execute the unit tests via [Jest](https://jestjs.io).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": [["minify", { "builtIns": false }]]
3+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'providers-unleash-web',
4+
preset: '../../../jest.preset.js',
5+
transform: {
6+
'^.+\\.[tj]s$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
7+
},
8+
moduleFileExtensions: ['ts', 'js', 'html'],
9+
coverageDirectory: '../../../coverage/libs/providers/unleash-web',
10+
};

libs/providers/unleash-web/package-lock.json

+104
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@openfeature/unleash-web-provider",
3+
"version": "0.1.0",
4+
"main": "./src/index.js",
5+
"typings": "./src/index.d.ts",
6+
"scripts": {
7+
"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",
8+
"current-version": "echo $npm_package_version"
9+
},
10+
"peerDependencies": {
11+
"@openfeature/web-sdk": "^1.0.0",
12+
"tslib": "^2.3.0",
13+
"unleash-proxy-client": "^3.6.0"
14+
}
15+
}
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"name": "providers-unleash-web",
3+
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "libs/providers/unleash-web/src",
5+
"projectType": "library",
6+
"targets": {
7+
"publish": {
8+
"executor": "nx:run-commands",
9+
"options": {
10+
"command": "npm run publish-if-not-exists",
11+
"cwd": "dist/libs/providers/unleash-web"
12+
},
13+
"dependsOn": [
14+
{
15+
"projects": "self",
16+
"target": "package"
17+
}
18+
]
19+
},
20+
"lint": {
21+
"executor": "@nx/linter:eslint",
22+
"outputs": ["{options.outputFile}"],
23+
"options": {
24+
"lintFilePatterns": ["libs/providers/unleash-web/**/*.ts", "libs/providers/unleash-web/package.json"]
25+
}
26+
},
27+
"test": {
28+
"executor": "@nx/jest:jest",
29+
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
30+
"options": {
31+
"jestConfig": "libs/providers/unleash-web/jest.config.ts",
32+
"passWithNoTests": true
33+
},
34+
"configurations": {
35+
"ci": {
36+
"ci": true,
37+
"codeCoverage": true
38+
}
39+
}
40+
},
41+
"package": {
42+
"executor": "@nx/rollup:rollup",
43+
"outputs": ["{options.outputPath}"],
44+
"options": {
45+
"project": "libs/providers/unleash-web/package.json",
46+
"outputPath": "dist/libs/providers/unleash-web",
47+
"entryFile": "libs/providers/unleash-web/src/index.ts",
48+
"tsConfig": "libs/providers/unleash-web/tsconfig.lib.json",
49+
"buildableProjectDepsInPackageJsonType": "dependencies",
50+
"updateBuildableProjectDepsInPackageJson": true,
51+
"compiler": "tsc",
52+
"generateExportsField": true,
53+
"umdName": "unleash-web",
54+
"external": "all",
55+
"format": ["cjs", "esm"],
56+
"assets": [
57+
{
58+
"glob": "package.json",
59+
"input": "./assets",
60+
"output": "./src/"
61+
},
62+
{
63+
"glob": "LICENSE",
64+
"input": "./",
65+
"output": "./"
66+
},
67+
{
68+
"glob": "README.md",
69+
"input": "./libs/providers/unleash-web",
70+
"output": "./"
71+
}
72+
]
73+
}
74+
}
75+
},
76+
"tags": []
77+
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './lib/unleash-web-provider';

0 commit comments

Comments
 (0)