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 unleash web provider #1105

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
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 @@ -18,5 +18,6 @@
"libs/providers/multi-provider-web": "0.0.2",
"libs/providers/growthbook-client": "0.1.2",
"libs/providers/config-cat-web": "0.1.3",
"libs/shared/config-cat-core": "0.1.0"
"libs/shared/config-cat-core": "0.1.0",
"libs/providers/unleash-web": "0.1.0"
lukas-reining marked this conversation as resolved.
Show resolved Hide resolved
}
25 changes: 25 additions & 0 deletions libs/providers/unleash-web/.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"
}
}
]
}
82 changes: 82 additions & 0 deletions libs/providers/unleash-web/README.md
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
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).
3 changes: 3 additions & 0 deletions libs/providers/unleash-web/babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [["minify", { "builtIns": false }]]
}
10 changes: 10 additions & 0 deletions libs/providers/unleash-web/jest.config.ts
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',
};
104 changes: 104 additions & 0 deletions libs/providers/unleash-web/package-lock.json

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

17 changes: 17 additions & 0 deletions libs/providers/unleash-web/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@openfeature/unleash-web-provider",
"version": "0.0.1",
"dependencies": {
"tslib": "^2.3.0",
"unleash-proxy-client": "^3.6.1"
lukas-reining marked this conversation as resolved.
Show resolved Hide resolved
},
"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"
}
}
77 changes: 77 additions & 0 deletions libs/providers/unleash-web/project.json
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": []
}
1 change: 1 addition & 0 deletions libs/providers/unleash-web/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/unleash-web-provider';
39 changes: 39 additions & 0 deletions libs/providers/unleash-web/src/lib/test-logger.ts
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: [],
};
}
}
Loading
Loading