Skip to content

Latest commit

 

History

History
186 lines (138 loc) · 6.66 KB

README.md

File metadata and controls

186 lines (138 loc) · 6.66 KB

Reflag Node.js OpenFeature Provider

The official OpenFeature Node.js provider for Reflag feature management service.

Installation

npm install @reflag/openfeature-node-provider

Required peer dependencies

The OpenFeature SDK is required as peer dependency. The minimum required version of @openfeature/server-sdk currently is 1.13.5. The minimum required version of @reflag/node-sdk currently is 2.0.0.

npm install @openfeature/server-sdk @reflag/node-sdk

Migrating from Bucket OpenFeature SDK

If you have been using the Bucket SDKs, the following list will help you migrate to Reflag SDK:

  • Bucket* classes, and types have been renamed to Reflag* (e.g. BucketClient is now ReflagClient)
  • All environment variables that were prefixed with BUCKET_ are now prefixed with REFLAG_
  • The BUCKET_HOST environment variable and host option have been removed from ReflagClient constructor, use REFLAG_API_BASE_URL instead
  • The BUCKET_FEATURES_ENABLED and BUCKET_FEATURES_DISABLED have been renamed to REFLAG_FLAGS_ENABLED and REFLAG_FLAGS_DISABLED
  • The default configuration file has been renamed from bucketConfig.json to reflag.config.json
  • The fallbackFeatures property in client constructor and configuration files has been renamed to fallbackFlags
  • featureKey has been renamed to flagKey in all methods that accepts that argument
  • The SDKs will not emit evaluate and evaluate-config events anymore

Usage

The provider uses the Reflag Node.js SDK. The available options can be found in the Reflag Node.js SDK.

Example using the default configuration

import { ReflagNodeProvider } from "@reflag/openfeature-node-provider";
import { OpenFeature } from "@openfeature/server-sdk";

const provider = new ReflagNodeProvider({ secretKey });

await OpenFeature.setProviderAndWait(provider);

// set a value to the global context
OpenFeature.setContext({ region: "us-east-1" });

// set a value to the invocation context
// this is merged with the global context
const requestContext = {
  targetingKey: req.user.id,
  email: req.user.email,
  companyPlan: req.locals.plan,
};

const client = OpenFeature.getClient();

const enterpriseFlagEnabled = await client.getBooleanValue(
  "enterpriseFlag",
  false,
  requestContext,
);

Flag resolution methods

The provider reads flags with Reflag SDK v2. Boolean flags resolve with getBooleanValue(). Multivariate flags resolve with the method that matches their value's JSON type.

client.getBooleanValue("huddles", false); // a Boolean flag
client.getStringValue("model", "quick"); // a multivariate flag with string values
client.getNumberValue("max-participants", 10); // ... with number values
client.getObjectValue("huddle-settings", { maxParticipants: 10 }); // ... with object or array values

All methods:

  • return the flag's value with TARGETING_MATCH;
  • return your default with DEFAULT if the flag has no value;
  • return your default with FLAG_NOT_FOUND if the flag doesn't exist;
  • return your default with TYPE_MISMATCH if the value has a different JSON type from your default. Pass null to getObjectValue() to accept any JSON value;
  • return your default with PROVIDER_NOT_READY before the provider is initialized.

Each evaluation records one check in Reflag.

Migrating from provider 1.x

Version 2 of the provider uses Reflag SDK v2 instead of v1.

  • getStringValue() and getObjectValue() read a multivariate flag's value instead of a Boolean flag's Remote config key and payload. Move Remote config to a multivariate flag first. See Migrating from Remote config.
  • getNumberValue() now works for multivariate flags with number values.
  • A Boolean read of a multivariate flag returns your default with TYPE_MISMATCH.
  • The fallbackFlags option is removed. See the SDK v2 migration guide.

Translating Evaluation Context

Reflag uses a context object of the following shape:

/**
 * Describes the current user context, company context, and other context.
 * This is used to determine if flag targeting matches and to track events.
 **/
export type ReflagContext = {
  /**
   * The user context. If the user is set, the user ID is required.
   */
  user?: {
    id: string;
    name?: string;
    email?: string;
    avatar?: string;
    [k: string]: any;
  };

  /**
   * The company context. If the company is set, the company ID is required.
   */
  company?: { id: string; name?: string; avatar?: string; [k: string]: any };

  /**
   * The other context. This is used for any additional context that is not related to user or company.
   */
  other?: Record<string, any>;
};

To use the Reflag Node.js OpenFeature provider, you must convert your OpenFeature contexts to Reflag contexts. You can achieve this by supplying a context translation function which takes the OpenFeature context and returns a corresponding Reflag Context:

import { ReflagNodeProvider } from "@openfeature/reflag-node-provider";

const contextTranslator = (context: EvaluationContext): ReflagContext => {
  return {
    user: {
      id: context.targetingKey ?? context["userId"]?.toString(),
      name: context["name"]?.toString(),
      email: context["email"]?.toString(),
      avatar: context["avatar"]?.toString(),
      country: context["country"]?.toString(),
    },
    company: {
      id: context["companyId"]?.toString(),
      name: context["companyName"]?.toString(),
      avatar: context["companyAvatar"]?.toString(),
      plan: context["companyPlan"]?.toString(),
    },
  };
};

const provider = new ReflagNodeProvider({ secretKey, contextTranslator });

OpenFeature.setProvider(provider);

Tracking feature adoption

The Reflag OpenFeature provider supports the OpenFeature Tracking API. It's straight forward to start sending tracking events through OpenFeature.

Simply call the "track" method on the OpenFeature client:

import { ReflagNodeProvider } from "@reflag/openfeature-node-provider";
import { OpenFeature } from "@openfeature/server-sdk";

const provider = new ReflagNodeProvider({ secretKey });

await OpenFeature.setProviderAndWait(provider);

const client = OpenFeature.getClient();

// `evaluationContext` is whatever you use to evaluate features based off
const enterpriseFlagEnabled = await client.track("huddles", evaluationContext);

License

MIT License Copyright (c) 2025 Bucket ApS