OpenFeature server SDK
Provides one flag evaluation API that works with many flag providers.
- Downloads / mo
- 26.4M
- Stars
- 289
- Last release
- 1.23.0
- License
- Apache-2.0
Strengths
- Changes providers behind one API. Keep evaluation calls while replacing the feature flag backend.
- Propagates request context. Attach targeting attributes once for a transaction's downstream evaluations.
Limitations
- Needs a flag provider. An unconfigured client returns fallback values until a provider is registered.
- Provider events vary. Check provider support before relying on configuration-change notifications.
Quickstart
import { OpenFeature, InMemoryProvider } from "@openfeature/server-sdk"; await OpenFeature.setProviderAndWait(new InMemoryProvider({ "new-ui": { disabled: false, defaultVariant: "on", variants: { on: true, off: false } }, })); const client = OpenFeature.getClient("example"); console.log(await client.getBooleanValue("new-ui", false));
OpenFeature server SDK examples
export function createOpenFeatureAdapter( init: Client | (() => Promise<Client>), ): AdapterResponse<Client | (() => Promise<Client>)> { let client: Client | null = typeof init === 'function' ? null : init; let clientPromise: Promise<Client> | null = null; function initialize(): Client | Promise<Client> { if (client) return client; if (clientPromise) return clientPromise; const attempt = ( typeof init === 'function' ? init() : Promise.resolve(init) ).then( (resolvedClient) => { client = resolvedClient; return resolvedClient; }, (error: unknown) => { // Initialization failed. Unless the provider declared the failure // fatal, forget the rejected promise so the next evaluation retries // instead of replaying this error for the rest of the process' life. if (clientPromise === attempt && !isFatalError(error)) { clientPromise = null; } throw error; }, ); clientPromise = attempt; return attempt; } function booleanValue( options?: FlagEvaluationOptions, ): Adapter<boolean, EvaluationContext> { return { async decide({ key, entities, defaultValue }): Promise<boolean> { const client = await initialize(); if (!client) return defaultValue as boolean; return client.getBooleanValue( key, defaultValue as boolean, entities, options, ); }, }; } function stringValue( options?: FlagEvaluationOptions, ): Adapter<string, EvaluationContext> { return { async decide({ key, entities, defaultValue }): Promise<string> { const client = await initialize(); if (!client) return defaultValue as string; return client.getStringValue( key, defaultValue as string, entities, options, ); }, }; } function numberValue( options?: FlagEvaluationOptions, ): Adapter<number, EvaluationContext> { return { async decide({ key, entities, defaultValue }): Promise<number> { const client = await initialize(); if (!client) return defaultValue as number; return client.getNumberValue( key, defaultValue as number, entities, options, ); }, }; } function objectValue<ValueType>( options?: FlagEvaluationOptions, ): Adapter<ValueType, EvaluationContext> { return { async decide({ key, entities, defaultValue }): Promise<ValueType> { const client = await initialize(); if (!client) return defaultValue as ValueType; return client.getObjectValue( key, defaultValue as JsonValue, entities, options, ) as ValueType; }, }; } return { booleanValue, stringValue, numberValue, objectValue, client: typeof init === 'function' ? async () => { const client = await initialize(); if (!client) throw new Error( '@flags-sdk/openfeature: OpenFeature client failed to initialize', ); return client; } : init, }; }