Featurevisor OpenFeature provider for Node.js
Use Featurevisor through the OpenFeature server SDK in Node.js.
Installation
npm install @featurevisor/openfeature-provider-node @openfeature/server-sdk
Usage
import { OpenFeature } from '@openfeature/server-sdk'
import { FeaturevisorOpenFeatureProvider } from '@featurevisor/openfeature-provider-node'
const provider = new FeaturevisorOpenFeatureProvider({ datafile })
await OpenFeature.setProviderAndWait(provider)
const client = OpenFeature.getClient()
const enabled = await client.getBooleanValue('checkout', false, {
targetingKey: 'user-123',
country: 'nl',
})
OpenFeature keys map to Featurevisor evaluations:
| Key | Evaluation |
|---|---|
checkout | flag |
checkout:variation | variation |
checkout:title | variable named title |
variable:supportEmail | global variable named supportEmail |
Use the resolver matching the Featurevisor value type. Boolean variables use the boolean resolver with a suffixed key. Arrays, objects, and JSON variables use the object resolver.
The provider maps OpenFeature targetingKey to Featurevisor userId by default. Configure targetingKeyField, keySeparator, variationKey, or globalVariablePrefix when your project needs different names. The global variable prefix defaults to variable.
The provider distinguishes an omitted targetingKey from an explicitly empty string. The OpenFeature server client passes both through, so { targetingKey: '', userId: 'old' } maps userId to the empty string. If no targeting key is supplied, an existing userId is preserved. Untyped JavaScript callers can also supply a finite numeric targeting key, including 0; NaN and infinity do not override the existing field.
Featurevisor instance
The provider can create and own a Featurevisor instance, or reuse one created by your application.
Let the provider create the instance
Pass normal Featurevisor options to the provider:
const provider = new FeaturevisorOpenFeatureProvider({
datafile,
modules,
onDiagnostic,
})
The provider exposes the created instance as provider.featurevisor. You can use it for datafile updates, events, diagnostics, and other Featurevisor APIs. When OpenFeature closes the provider, the provider closes this instance and releases its resources.
Reuse an existing instance
Pass an instance created with createFeaturevisor() using the featurevisor option:
import { OpenFeature } from '@openfeature/server-sdk'
import { createFeaturevisor } from '@featurevisor/sdk'
import { FeaturevisorOpenFeatureProvider } from '@featurevisor/openfeature-provider-node'
const featurevisor = createFeaturevisor({ datafile })
const provider = new FeaturevisorOpenFeatureProvider({ featurevisor })
await OpenFeature.setProviderAndWait(provider)
The same instance is available as provider.featurevisor. Evaluations, datafile updates, modules, diagnostics, and events continue to use that instance.
An invalid initial datafile rejects setProviderAndWait with PARSE_ERROR, regardless of diagnostic log level. The provider remains in an error state until a valid datafile is supplied. Malformed updates emit an error event and resolve to the OpenFeature default with PARSE_ERROR. A valid update restores readiness, including when it restores the previously accepted datafile.
An instance passed this way remains owned by your application. Closing the OpenFeature provider does not close it, so other consumers can continue using it. Call featurevisor.close() when every consumer is finished.
If featurevisor and Featurevisor construction options such as datafile or modules are supplied together, the existing instance takes precedence and the construction options are ignored. Provider options such as targetingKeyField, keySeparator, variationKey, globalVariablePrefix, and onTrack still apply.
See Featurevisor OpenFeature documentation for reasons, errors, metadata, tracking, lifecycle, and other language providers.
License
MIT © Fahad Heylaal