Code Monkey home page Code Monkey logo

js-sdk's Introduction

OpenFeature SDK for JavaScript

a codecov npm version Known Vulnerabilities v0.5.1 CII Best Practices

Getting Started   •   API Documentation


This is the JavaScript implementation of OpenFeature, 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.

Installation

npm install @openfeature/js-sdk

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.

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.

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

Hooks

Implement your own hook by conforming to the Hook interface.

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

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.

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.

Contributing

See CONTRIBUTING.md for details on how to contribute to the OpenFeature project.

Our community meetings are held regularly and open to everyone. Check the OpenFeature community calendar for specific dates and the Zoom meeting links.

Thanks to everyone that has already contributed

Made with contrib.rocks.

License

Apache License 2.0

js-sdk's People

Contributors

anudeepreddy avatar beeme1mr avatar dependabot[bot] avatar github-actions[bot] avatar renovate[bot] avatar thomaspoignant avatar toddbaert avatar weyert avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.