









OpenFeature is an open specification that provides a vendor-agnostic, community-driven API for feature flagging that works with your favorite feature flag management tool or in-house solution. The Flags SDK OpenFeature adapter allows you to use the Flags SDK with any OpenFeature provider.
If there is a native Flags SDK adapter for your provider, we recommend using that instead. Native Flags SDK adapters finely tune the SDK for your flag provider, optionally integrate with Edge Config and Flags Explorer, and are configured for Edge Runtime compatibility where possible. See available adapters
The @flags-sdk/openfeature package provides an adapter for loading flags from OpenFeature.
Learn more about Adapters
Clone the OpenFeature template
The OpenFeature provider is available in the @flags-sdk/openfeature module. Install it with
npm i @flags-sdk/openfeature @openfeature/server-sdkThe command also installs the @openfeature/server-sdk peer dependency, as the OpenFeature adapter depends on the OpenFeature Node.js SDK.
Import the createOpenFeatureAdapter function from @flags-sdk/openfeature and create an adapter instance with your OpenFeature client.
For usage with regular providers, pass the client directly:
import { createOpenFeatureAdapter } from "@flags-sdk/openfeature"
OpenFeature.setProvider(new YourProviderOfChoice())
const openFeatureAdapter = createOpenFeatureAdapter(OpenFeature.getClient());For usage with async providers, pass an init function, and return the client:
import { createOpenFeatureAdapter } from "@flags-sdk/openfeature"
// pass an init function, and return the client
const openFeatureAdapter = createOpenFeatureAdapter(async () => {
const provider = new YourProviderOfChoice()
await OpenFeature.setProviderAndWait(provider);
return OpenFeature.getClient();
});| Option key | Type | Description |
|---|---|---|
client | Client | (() => Promise<Client>) | OpenFeature client |
OpenFeature relies on an Evaluation Context object to evaluate the flags for a given request.
Use the identify function to determine the Evaluation Context.
import type { Identify } from "flags";
import { dedupe, flag } from "flags/next";
import type { EvaluationContext } from "@openfeature/server-sdk";
const identify = dedupe((async ({ headers, cookies }) => {
// Your own logic to identify the user
// Identifying the user should rely on reading cookies and headers only, and
// not make any network requests, as it's important to keep latency low here.
const user = await getUser(headers, cookies);
const context: EvaluationContext = {
targetingKey: user.id,
// .. other properties ..
};
return context;
}));
const openFeatureAdapter = createOpenFeatureAdapter(async () => {
const provider = new YourProviderOfChoice()
await OpenFeature.setProviderAndWait(provider);
return OpenFeature.getClient();
});
export const exampleFlag = flag<boolean, EvaluationContext>({
key: "example-flag",
identify,
defaultValue: false,
adapter: openFeatureAdapter.booleanValue(),
});Learn more about identify
The OpenFeature adapter provides methods for evaluating flags.
booleanValueexport const exampleFlag = flag<boolean, EvaluationContext>({
key: 'example-flag',
identify,
defaultValue: false,
adapter: openFeatureAdapter.booleanValue(),
});identify must return the Evaluation Context.defaultValue must be provided when used with the OpenFeature adapter.stringValueexport const exampleFlag = flag<string, EvaluationContext>({
key: 'example-flag',
identify,
defaultValue: "",
adapter: openFeatureAdapter.stringValue(),
});identify must return the Evaluation Context.defaultValue must be provided when used with the OpenFeature adapter.numberValueexport const exampleFlag = flag<number, EvaluationContext>({
key: 'example-flag',
identify,
defaultValue: -1,
adapter: openFeatureAdapter.numberValue(),
});identify must return the Evaluation Context.defaultValue must be provided when used with the OpenFeature adapter.objectValueexport const exampleFlag = flag<YourCustomObjectType, EvaluationContext>({
key: 'example-flag',
identify,
defaultValue: {},
adapter: openFeatureAdapter.objectValue(),
});identify must return the Evaluation Context.defaultValue must be provided when used with the OpenFeature adapter.clientWhen the openFeatureAdapter was created synchronously, this contains the OpenFeature client.
openFeatureAdapter.client;When the openFeatureAdapter was created asynchronously, this is an async function which returns the OpenFeature client returned by the init function.
await openFeatureAdapter.client();The OpenFeature adapter does not integrate with Edge Config directly.
We recommend using a native Flags SDK adapter for your provider instead of using the OpenFeature adapter. Native Flags SDK adapters finely tune the SDK for your flag provider, optionally integrate with Edge Config and Flags Explorer, and are configured for Edge Runtime compatibility where possible. See available adapters.
If no native Flags SDK adapter is available, your OpenFeature provider might still allow you to configure Edge Config integration.
The Flags Explorer is a tool for viewing and overriding flags.
All feature flags using the Flags SDK can be overwritten by the Flags Explorer.
To make the Flags Explorer aware of your OpenFeature flags, you need to provide a route which the Flags Explorer will load your flags metadata from.
import { getProviderData, createFlagsDiscoveryEndpoint } from 'flags/next';
import * as flags from '../../../../flags';
export const GET = createFlagsDiscoveryEndpoint(async (request) => {
return getProviderData(flags);
});The OpenFeature provider of your choice may not be compatible with Edge Runtime, which can prevent usage in Routing Middleware and Edge Functions. This depends on the provider you use OpenFeature with.
Due to limitations in the OpenFeature specification, the @flags-sdk/openfeature adapter is not capable of providing additional metadata like descriptions and available options to the Flags Explorer. Flags Explorer can still display the descriptions and options you define when declaring feature flags in code.
Read more about OpenFeature, Flags SDK, and the OpenFeature Node.js SDK.
Learn more about Adapters
Clone the OpenFeature template
Learn more about the Flags Explorer
Learn more about the OpenFeature Node.js SDK
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。