Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"browser": true,
"es2021": true
},
"ignorePatterns": ["**/dist/**/*"],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
- name: Publish to NPM
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
run: npm publish --access public
run: npm run publish-all

- name: Build Docs
run: npm run docs
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,6 @@ typedoc

# IDE
.idea

# license files copied from root
packages/**/LICENSE
4 changes: 3 additions & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
".": "1.1.0"
"packages/server": "1.1.0",
"packages/client": "0.0.1-experimental",
"packages/shared": "0.0.1"
}
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Install dependencies with `npm ci`. `npm install` will update the package-lock.j

We value having as few runtime dependencies as possible. The addition of any dependencies requires careful consideration and review.

### Modules

This repository uses [NPM workspaces](https://docs.npmjs.com/cli/v9/using-npm/workspaces) to establish a simple monorepo.
Within the root project, there is one common project (`packages/shared`) which features common interfaces and code, consumed by the published modules (`packages/server` and `packages/client`). The shared module is bundled transparently into the published modules - it is not published itself. Changes in `packages/shared` will result in releases of the dependant modules via Release Please.

### Testing

Run tests with `npm test`.
Expand Down
98 changes: 15 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
# OpenFeature SDK for JavaScript
# OpenFeature JavaScript SDKs

[![a](https://img.shields.io/badge/slack-%40cncf%2Fopenfeature-brightgreen?style=flat&logo=slack)](https://cloud-native.slack.com/archives/C0344AANLA1)
[![Known Vulnerabilities](https://snyk.io/test/github/open-feature/js-sdk/badge.svg)](https://snyk.io/test/github/open-feature/js-sdk)
[![codecov](https://codecov.io/gh/open-feature/js-sdk/branch/main/graph/badge.svg?token=3DC5XOEHMY)](https://codecov.io/gh/open-feature/js-sdk)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/6594/badge)](https://bestpractices.coreinfrastructure.org/projects/6594)

This repository contains the JavaScript implementations of [OpenFeature][openfeature-website], a vendor-agnostic abstraction library for evaluating feature flags.

We support multiple data types for flags (numbers, strings, booleans, objects) as well as hooks, which can alter the lifecycle of a flag evaluation.

## Server

---

[![npm version](https://badge.fury.io/js/@openfeature%2Fjs-sdk.svg)](https://badge.fury.io/js/@openfeature%2Fjs-sdk)
[![Known Vulnerabilities](https://snyk.io/test/github/open-feature/js-sdk/badge.svg)](https://snyk.io/test/github/open-feature/js-sdk)
[![v0.5.1](https://img.shields.io/static/v1?label=Specification&message=v0.5.1&color=yellow)](https://github.com/open-feature/spec/tree/v0.5.1)
[![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/6594/badge)](https://bestpractices.coreinfrastructure.org/projects/6594)

<p align="center">
<strong>
<!-- TODO: add direct link to server module when published -->
<a href="https://docs.openfeature.dev/docs/tutorials/getting-started/node">Getting Started<a/>
&nbsp;&nbsp;&bull;&nbsp;&nbsp;
<a href="https://open-feature.github.io/js-sdk">API Documentation<a/>
Expand All @@ -17,13 +27,7 @@

---

This is the JavaScript implementation of [OpenFeature][openfeature-website], a vendor-agnostic abstraction library for evaluating feature flags.

We support multiple data types for flags (numbers, strings, booleans, objects) as well as hooks, which can alter the lifecycle of a flag evaluation.

> This library is intended to be used in server-side contexts and has only **experimental support** for web usage. Client-side support can be tracked [here][client-side-github-issue].

## Installation
### Installation

```shell
npm install @openfeature/js-sdk
Expand All @@ -35,79 +39,7 @@ or
yarn add @openfeature/js-sdk
```

## Usage

To configure the SDK you'll need to add a provider to the `OpenFeature` global signleton. From there, you can generate a `client` which is usable by your code. While you'll likely want a provider for your specific backend, we've provided a `NoopProvider`, which simply returns the default value.

```typescript
import { OpenFeature } from '@openfeature/js-sdk';

// configure a provider
OpenFeature.setProvider(new YourProviderOfChoice());

// create a client
const client = OpenFeature.getClient('my-app');

// get a bool value
const boolValue = await client.getBooleanValue('boolFlag', false);

// get a string value
const stringValue = await client.getStringValue('stringFlag', 'default');

// get an numeric value
const numberValue = await client.getNumberValue('intFlag', 1);

// get an object value
const object = await client.getObjectValue<MyObject>('objectFlag', {});

// add a value to the invocation context
const context: EvaluationContext = {
myInvocationKey: 'myInvocationValue',
};
const contextAwareValue = await client.getBooleanValue('boolFlag', false, context);
```

A list of available providers can be found [here][server-side-artifacts].

For complete documentation, visit: https://docs.openfeature.dev/docs/category/concepts

## Hooks

Implement your own hook by conforming to the [Hook interface][hook-interface].

All of the hook stages (before, after, error, and finally) are optional.

```typescript
import { OpenFeature, Hook, HookContext } from '@openfeature/js-sdk';

// Example hook that logs if an error occurs during flag evaluation
export class GlobalDebugHook implements Hook {
after(hookContext: HookContext, err: Error) {
console.log('hook context', hookContext);
console.error(err);
}
}
```

Register the hook at global, client, or invocation level.

```typescript
import { OpenFeature } from '@openfeature/js-sdk';
// This hook used is used for example purposes
import { GlobalDebugHook, ClientDebugHook, InvocationDebugHook } from './debug-hook';

// A global hook will run on every flag evaluation
OpenFeature.addHooks(new GlobalDebugHook());

const client = OpenFeature.getClient('my-app');
// A client hook will run on every flag evaluation executed by this client
client.addHooks(new ClientDebugHook());

// An invocation hook will only run on the registred flag evaluation method
const boolValue = await client.getBooleanValue('boolFlag', false, {}, { hooks: [new InvocationDebugHook()] });
```

A list of available hooks can be found [here][server-side-artifacts].
See [README.md](./packages/server/README.md)

## Contributing

Expand Down
27 changes: 23 additions & 4 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,29 @@ export default {

// A preset that is used as a base for Jest's configuration
preset: 'ts-jest',

// Run tests from one or more projects
// projects: undefined,
projects: [
{
displayName: 'server',
testEnvironment: 'node',
preset: 'ts-jest',
testMatch: [
'<rootDir>/packages/server/test/**/*.spec.ts'
]
},
{
displayName: 'server-e2e',
testEnvironment: 'node',
preset: 'ts-jest',
testMatch: [
'<rootDir>/packages/server/integration/**/*.spec.ts'
],
modulePathIgnorePatterns: [
'.*/node-modules/'
],
setupFiles: ['<rootDir>/packages/server/integration/step-definitions/setup.ts'],
}
],

// Use this configuration option to add custom reporters to Jest
// reporters: undefined,
Expand All @@ -126,10 +146,9 @@ export default {
// restoreMocks: false,

// The root directory that Jest should scan for tests and modules within
// rootDir: undefined,
rootDir: './',

// A list of paths to directories that Jest should use to search for files in
roots: ['<rootDir>/test'],

// Allows you to use a custom runner instead of Jest's default test runner
// runner: "jest-runner",
Expand Down
Loading