JavaScript implementation of the Confidence SDK and the Confidence OpenFeature provider, to be used in conjunction wth the OpenFeature SDK.
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:
To add the packages to your dependencies run:
yarn add @spotify-confidence/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,
});
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.
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', {});
We'd love to get patches from you! See Contributing for details.
You can install all dependencies by running
yarn
Code is formatted using prettier, you can format all files by running
yarn format
To run the linter, run:
yarn lint
Tests are based on jest and can be run with
yarn test
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
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.
This project adheres to the Open Source Code of Conduct. By participating, you are expected to honor this code.
Copyright 2023 Spotify AB.
Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0