OpenFeature Provider for Node.js
Getting started
1. Install the provider
npm i @openfeature/config-cat-provider
2. Initialize the provider
The ConfigCatProvider.create()
function takes the SDK key and an optional options
argument containing additional configuration options for the ConfigCat Node.js SDK:
import { OpenFeature } from "@openfeature/server-sdk";
import { ConfigCatProvider } from '@openfeature/config-cat-provider';
import { createConsoleLogger, LogLevel, PollingMode } from "configcat-node";
// Build options for the ConfigCat SDK.
const options = {
logger: createConsoleLogger(LogLevel.Info),
setupHooks: (hooks) => hooks.on('clientReady', () => console.log('Client is ready!')),
// ...
}
// Configure the provider.
await OpenFeature.setProviderAndWait(ConfigCatProvider.create('#YOUR-SDK-KEY#', PollingMode.AutoPoll, options));
// Create a client.
const client = OpenFeature.getClient();
For more information about all the configuration options, see the Node.js SDK documentation.
3. Evaluate your feature flag
const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false);
if (isAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}
4. Cleaning up
On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client.
await OpenFeature.clearProviders();
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
targetingKey | identifier | ☑ |
email | email | |
country | country | |
Any other | custom |
To evaluate feature flags for a context, use the OpenFeature Evaluation API:
const context = {
targetingKey: '#SOME-USER-ID#',
email: '[email protected]',
country: 'CountryID',
};
const isAwesomeFeatureEnabled = await client.getBooleanValue('isAwesomeFeatureEnabled', false, context);