OpenFeature Provider for Web
The official ConfigBee OpenFeature Provider for Web allows you to seamlessly integrate ConfigBee with the OpenFeature standard in your web applications. This provider brings ConfigBee’s powerful feature flagging and dynamic configuration capabilities to any OpenFeature-compatible JavaScript/TypeScript application.
Introduction to OpenFeature
OpenFeature is an open standard for feature flag management that provides a unified API across different programming languages and environments. By implementing the OpenFeature specification, ConfigBee enables you to:
- Use ConfigBee as your feature management backend while maintaining compatibility with OpenFeature tooling
- Switch between feature flag providers without changing your application code
- Benefit from the broader OpenFeature ecosystem and community
Installation
Install the ConfigBee OpenFeature Provider for Web via npm or yarn:
npm install configbee-openfeature-provider-web
yarn add configbee-openfeature-provider-web
Prerequisites:
- A ConfigBee account (sign up at platform.configbee.com)
- OpenFeature Web SDK installed in your project
Install OpenFeature Web SDK
npm install @openfeature/js-sdk
yarn add @openfeature/js-sdk
Getting Started
Basic Setup
Here’s how to set up the ConfigBee OpenFeature Provider in your web application:
import { OpenFeature } from '@openfeature/js-sdk';import { ConfigbeeWebProvider } from 'configbee-openfeature-provider-web';
// Initialize the ConfigBee provider with your account credentialsconst configBeeProvider = new ConfigbeeWebProvider({ accountId: 'your-account-id', projectId: 'your-project-id', environmentId: 'your-environment-id'});
// Set the ConfigBee provider as the OpenFeature providerOpenFeature.setProvider(configBeeProvider);
// Get a client instance to interact with feature flagsconst client = OpenFeature.getClient();
Using Feature Flags
Once the provider is set up, you can use OpenFeature’s client to check feature flags:
// Get a boolean flag with a default value of falseconst isNewFeatureEnabled = await client.getBooleanValue('new_feature', false);
// Conditionally execute code based on flag valueif (isNewFeatureEnabled) { // Execute code for the new feature console.log('New feature is enabled!');} else { // Execute fallback code console.log('New feature is disabled.');}
Working with Different Value Types
OpenFeature supports multiple value types for flags:
// Boolean flagconst isEnabled = await client.getBooleanValue('feature_enabled', false);
// String valueconst welcomeMessage = await client.getStringValue('welcome_message', 'Hello, World!');
// Number valueconst maxItems = await client.getNumberValue('max_items', 10);
// JSON objectconst config = await client.getObjectValue('app_config', { theme: 'light', timeout: 30 });
Contextual Targeting
ConfigBee supports delivering personalized features based on context (such as user attributes). You can use OpenFeature’s context API to provide targeting information:
Setting Context
// Set context that applies to all flag evaluationsawait OpenFeature.setContext({ userId: 'user-123', userRole: 'admin', region: 'us-west'});
Updating Context Based on User State
// Update context when user logs infunction onUserLogin(user) { OpenFeature.setContext({ userId: user.id, userName: user.name, userRole: user.role, subscription: user.subscription });}
// Clear context when user logs outfunction onUserLogout() { OpenFeature.setContext({});}
Framework Integration
React Integration
The ConfigBee OpenFeature Provider works with the OpenFeature React SDK:
npm install @openfeature/react-sdk
Example usage:
import { useFlagValue } from '@openfeature/react-sdk';
function FeatureComponent() { const isNewUIEnabled = useFlagValue('new_ui', false);
return ( <div> {isNewUIEnabled ? ( <NewUIComponent /> ) : ( <LegacyUIComponent /> )} </div> );}
Angular Integration
For Angular applications, use the OpenFeature Angular SDK:
npm install @openfeature/angular-sdk
Example usage:
import { Component } from '@angular/core';import { OpenFeatureClient } from '@openfeature/angular-sdk';
@Component({ selector: 'app-feature', template: ` <div *ngIf="isNewFeatureEnabled$ | async"> <new-feature-component></new-feature-component> </div> <div *ngIf="!(isNewFeatureEnabled$ | async)"> <legacy-feature-component></legacy-feature-component> </div> `})export class FeatureComponent { isNewFeatureEnabled$ = this.client.getBooleanDetails('new_feature', false).valueChanges;
constructor(private client: OpenFeatureClient) {}}
Best Practices
-
Default Values: Always provide sensible default values when requesting flag values.
// Good practiceconst maxRetries = await client.getNumberValue('max_retries', 3);// Avoid omitting defaultsconst maxRetries = await client.getNumberValue('max_retries'); // Bad practice -
Error Handling: Wrap flag evaluations in try-catch blocks for production code.
try {const isFeatureEnabled = await client.getBooleanValue('feature_flag', false);// Use the flag value} catch (error) {console.error('Error evaluating feature flag:', error);// Fall back to default behavior} -
Context Management: Keep user context updated throughout the application lifecycle.
-
Flag Naming Conventions: Use consistent naming conventions for your flags in ConfigBee.
-
Graceful Degradation: Design your application to work properly even if flag evaluations fail.
Troubleshooting
Provider Not Ready
If flags are not being evaluated correctly:
- Check that the provider initialization is complete
- Verify your ConfigBee credentials
- Check network connectivity
- Look for errors in the browser console
Context Not Applied
If targeting doesn’t work as expected:
- Verify that context is set before evaluating flags
- Check that context property names match those configured in ConfigBee
- Ensure context values are of the expected type
Flag Values Not Updating
If flag changes are not reflected:
- Verify that the flag has been updated in the ConfigBee dashboard
- Ensure you’re checking the correct environment