OpenFeature Node.js Provider
Introduction
The OpenFeature Node.js Provider is intended to be used in combination with the OpenFeature Node.js SDK. The provider wraps the ConfigDirector Node.js SDK.
The minimum Node.js version supported is 18.0.
Installation
The provider can be installed from NPM: https://www.npmjs.com/package/@configdirector/openfeature-server-provider
npm install --save @openfeature/server-sdk @configdirector/openfeature-server-provider
yarn add @openfeature/server-sdk @openfeature/core @configdirector/openfeature-server-provider
pnpm add @openfeature/server-sdk @openfeature/core @configdirector/openfeature-server-provider
bun add @openfeature/server-sdk @openfeature/core @configdirector/openfeature-server-provider
Configure and initialize the client
- Create an instance of the provider using your server SDK key. You can retrieve a server SDK key under your project settings in the
Environments & SDK Keystab. - Set the OpenFeature provider.
- Get a client instance from OpenFeature.
import { OpenFeature } from "@openfeature/server-sdk";
import { ConfigDirectorProvider } from "@configdirector/openfeature-server-provider";
const provider = new ConfigDirectorProvider("YOUR-SERVER-SDK-KEY");
await OpenFeature.setProviderAndWait(provider);
const client = OpenFeature.getClient();
Additional configuration options
Additional configuration options can be passed into the provider in the optional second argument of the constructor.
For example, the metadata can be provided like this:
import { ConfigDirectorProvider } from "@configdirector/openfeature-server-provider";
const provider = new ConfigDirectorProvider("YOUR-SERVER-SDK-KEY", {
metadata: {
appName: "YOUR-APP-NAME",
appVersion: "1.0.2",
},
});
The provider accepts the same configuration options as the Node.js SDK client, refer to the additional configuration options section of the Node.js SDK for a full list.
Retrieve config values
To retrieve config values, use the OpenFeature client:
const booleanValue = client.getBooleanValue("my-config-key", false);
const stringValue = client.getStringValue("my-string-config-key", "Default");
For additional information regarding the OpenFeature client refer to the OpenFeature Node.js SDK documentation.
User context
The user context can be provided as the third argument to value getter functions of the OpenFeature client. The OpenFeature Node.js provider evaluates targeting rules locally without additional network calls for different contexts.
const booleanValue = client.getBooleanValue("my-config-key", false, {
targetingKey: "12345", // In OpenFeature, the `targetingKey` represents the context's user ID
name: "Example User",
traits: {
region: "North America", // Any arbitrary traits which can be referenced in targeting rules
},
});
For additional information regarding the OpenFeature client refer to the OpenFeature Node.js SDK documentation.