Skip to content

Latest commit

 

History

History
140 lines (92 loc) · 4.06 KB

README.md

File metadata and controls

140 lines (92 loc) · 4.06 KB

JavaScript Confidence SDK Monorepo

JavaScript implementation of the Confidence SDK and the Confidence OpenFeature provider, to be used in conjunction wth the OpenFeature SDK.

Usage

We recommend to try out Confidence using the vanilla sdk. The setup guide below will help you get started.

This monorepo exports multiple packages, with their own docs:

SDK setup

Adding the dependencies

To add the packages to your dependencies run:

yarn add @spotify-confidence/sdk

Initializing the SDK

Run the Confidence.create function to obtain a root instance of Confidence.

The SDK initialization requires an API key (clientSecret) to work. This key obtained through the Confidence console.

import { Confidence } from '@spotify-confidence/sdk';

const confidence = Confidence.create({
  clientSecret: 'mysecret',
  region: 'eu', // or 'us'
  environment: 'client', // or 'server'
  timeout: 1000,
});

Setting the context

You can set the context manually by using setContext({}) or obtain a "child instance" of Confidence with a modified context by using withContext({}).

confidence.setContext({ 'pants-color': 'yellow' });
const childInstance = confidence.withContext({ 'pants-color': 'blue', 'pants-fit': 'slim' });

Important

When using the SDK in a server environment, you should call withContext rather than setContext. This will give you a new instance scoped to the request and prevent context from leaking between requests.

Accessing flags

The flag value API returns the Confidence assigned flag value or the passed in default value if no value was returned. The API supports dot notation, meaning that if the Confidence flag has a property enabled on the flag, you can access it directly.

const flag = await confidence.getFlag('tutorial-feature', {});
if (flag.enabled) {
  // ship it!
}
// or
const enabled = await confidence.getFlag('tutorial-feature.enabled', false);
if (enabled) {
  // ship it!
}

Tip

If you are troubleshooting flag values, the flag evaluation API can be really useful since it returns a FlagEvaluation type that contain information about variant, reason and possible error details.

const flagEvaluation = await confidence.evaluateFlag('tutorial-feature', {});

Contributions and Development

We'd love to get patches from you! See Contributing for details.

Setup

You can install all dependencies by running

yarn

Formatting

Code is formatted using prettier, you can format all files by running

yarn format

Linting

To run the linter, run:

yarn lint

Testing

Tests are based on jest and can be run with

yarn test

Bundling and API reports

Before release the sources (and types) are bundled. This process also includes generating an API report to keep track of changes to the public API. If you intend to change the public API you need to run the bundle command locally and commit the changed API report files, otherwise the commit will fail in CI. To update the API report run:

yarn bundle --local

Example apps

This repo contains a few example apps (under examples/) to display and test the functionality. These apps depend on the bundled output of the main packages, so you will need to run yarn bundle before starting any of the apps.

Code of Conduct

This project adheres to the Open Source Code of Conduct. By participating, you are expected to honor this code.

License

Copyright 2023 Spotify AB.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0