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:

  1. Import the required modules.

  2. Configure the provider with your publicKey , application name and environment.
    You can specify the environment in one of two formats:

    • Alternate ID (e.g., "production", "staging") — the environment in which your application is running.
    • Project Environment ID (e.g., pevr_abc123) — useful for internal references.
  3. 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', // or project environment ID
};

await OpenFeature.setProviderAndWait(new HyphenProvider(publicKey, options));
  1. Configure the context needed for feature targeting evaluations, incorporating 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',  
    },  
  },  
};

Usage

Evaluation Context Example

Evaluate a feature flag using the OpenFeature client and context information:

const client = OpenFeature.getClient();
const flagDetailsWithContext = await client.getBooleanDetails('feature-flag-key', false, context);

console.log(flagDetailsWithContext.value); // true or false

Configuration

Options

OptionTypeRequiredDescription
applicationstringYesThe application id or alternate ID.
environmentstringYesThe environment identifier for the Hyphen project (project environment ID or alternateId).
horizonUrlsstring[]NoHyphen Horizon URLs for fetching flags.
enableToggleUsagebooleanNoEnable/disable telemetry (default: true).
cacheobjectNoConfiguration for caching feature flag evaluations.

Cache Configuration

The cache option accepts the following properties:

PropertyTypeDefaultDescription
ttlSecondsnumber300Time-to-live in seconds for cached flag evaluations.
generateCacheKeyFnFunction-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.

FieldTypeRequiredDescription
targetingKeystringYesThe key used for caching the evaluation response.
ipAddressstringNoThe IP address of the user making the request.
customAttributesRecord<string, any>NoCustom attributes for additional contextual information.
userobjectNoAn object containing user-specific information for the evaluation.
user.idstringNoThe unique identifier of the user.
user.emailstringNoThe email address of the user.
user.namestringNoThe name of the user.
user.customAttributesRecord<string, any>NoCustom 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.');  
}