JavaScript (Node)
The Hyphen Toggle Provider for Node.js is an OpenFeature provider implementation that enables seamless feature flag evaluation. This section covers setup, usage, and configuration specific to the JavaScript implementation.
Installation
Install the provider and the OpenFeature server SDK:
npm install @openfeature/server-sdk @hyphen/openfeature-server-provider
Setup and Initialization
To begin using the Hyphen Provider, follow these steps:
- Import the required modules.
- Configure the provider with your
publicKey
and application options. - Register the provider with OpenFeature.
import { OpenFeature } from '@openfeature/server-sdk';
import { HyphenProvider, type HyphenProviderOptions } from '@hyphen/openfeature-server-provider';
const publicKey = "your-public-key-here";
const options: HyphenProviderOptions = {
application: 'your-application-name',
environment: 'production',
};
await OpenFeature.setProviderAndWait(new HyphenProvider(publicKey, options));
Feature Evaluation
Basic Feature Evaluation
Evaluate a feature flag using default values:
const client = OpenFeature.getClient();
const flagDetails = await client.getBooleanDetails('feature-flag-key', false);
console.log(flagDetails.value); // true or false
Contextual Feature Evaluation
Enhance evaluations by providing user or application context:
const context: HyphenEvaluationContext = {
targetingKey: 'user-123',
ipAddress: '203.0.113.42',
customAttributes: {
subscriptionLevel: 'premium',
region: 'us-east',
},
user: {
id: 'user-123',
email: '[email protected]',
name: 'John Doe',
customAttributes: {
role: 'admin',
},
},
};
const flagDetailsWithContext = await client.getBooleanDetails('feature-flag-key', false, context);
console.log(flagDetailsWithContext.value); // true or false
Configuration
Options
Option | Type | Description |
---|---|---|
application | string | The application id or alternate id. |
environment | string | The environment in which your application is running (e.g., production , staging ). |
enableToggleUsage | boolean | Enable or disable the logging of toggle usage (telemetry). |
cache | object | Configuration for caching feature flag evaluations. |
Cache Configuration
The cache
option accepts the following properties:
Property | Type | Default | Description |
---|---|---|---|
ttlSeconds | number | 300 | Time-to-live in seconds for cached flag evaluations. |
generateCacheKeyFn | Function | - | Custom function to generate cache keys from evaluation context. |
Example with cache configuration:
const options: HyphenProviderOptions = {
application: 'your-application-name',
environment: 'production',
cache: {
ttlSeconds: 600, // 10 minutes
generateCacheKeyFn: (context: HyphenEvaluationContext) => {
return `${context.targetingKey}-${context.user?.id}`;
},
},
};
Context
Provide an EvaluationContext
to pass contextual data for feature evaluation.
Context Fields
Field | Type | Description |
---|---|---|
targetingKey | string | The key used for caching the evaluation response. |
ipAddress | string | The IP address of the user making the request. |
customAttributes | Record<string, any> | Custom attributes for additional contextual information. |
user | object | An object containing user-specific information for the evaluation. |
user.id | string | The unique identifier of the user. |
user.email | string | The email address of the user. |
user.name | string | The name of the user. |
user.customAttributes | Record<string, any> | Custom attributes specific to the user. |
Example
const context: HyphenEvaluationContext = {
targetingKey: 'user-456',
customAttributes: {
subscriptionLevel: 'premium',
},
};
const flagDetails = await client.getBooleanDetails('enable-discounts', false, context);
if (flagDetails.value) {
console.log('Discounts enabled for premium users.');
} else {
console.log('No discounts available.');
}
Updated 8 days ago