import{DatadogProvider}from'@datadog/openfeature-browser';import{OpenFeature}from'@openfeature/web-sdk';constprovider=newDatadogProvider({// Required
// applicationId is a unique identifier to distinguish multiple frontend applications.
// This should match the app ID you provide to your RUM SDK.
applicationId:'<APPLICATION_ID>',// Required
clientToken:'<CLIENT_TOKEN>',site:'',env:'<ENV_NAME>',});
constdetails=client.getBooleanDetails('checkout_new',false);console.log(details.value);// Evaluated value (true or false)
console.log(details.variant);// Variant name, if applicable
console.log(details.reason);// Why this value was chosen
console.log(details.errorCode);// Error code, if evaluation failed
import{DatadogProvider}from'@datadog/openfeature-browser';import{OpenFeature}from'@openfeature/web-sdk';// Initialize the Datadog provider
constprovider=newDatadogProvider({applicationId:'<APPLICATION_ID>',clientToken:'<CLIENT_TOKEN>',site:'',env:'<ENV_NAME>',});// Set the evaluation context
constevaluationContext={targetingKey:'user-123',user_id:'123',user_role:'admin',};awaitOpenFeature.setProviderAndWait(provider,evaluationContext);// Get the client and evaluate flags
constclient=OpenFeature.getClient();constshowNewFeature=client.getBooleanValue('new_feature',false);if(showNewFeature){console.log('New feature is enabled!');}
import{beforeEach,afterAll,expect,test}from'vitest';import{OpenFeature,TypedInMemoryProvider}from'@openfeature/web-sdk';constflags={new_checkout_button:{variants:{on:true,off:false},defaultVariant:'on',disabled:false,},ui_theme:{variants:{dark:'dark',light:'light'},defaultVariant:'light',disabled:false,},};beforeEach(async()=>{awaitOpenFeature.setProviderAndWait(newTypedInMemoryProvider(flags));});afterAll(async()=>{awaitOpenFeature.close();});test('new checkout button is enabled by default',()=>{constclient=OpenFeature.getClient();expect(client.getBooleanValue('new_checkout_button',false)).toBe(true);});test('missing flag returns default',()=>{constclient=OpenFeature.getClient();expect(client.getBooleanValue('does-not-exist',false)).toBe(false);});
Web SDK のフラグ形状には、variants、defaultVariant、および disabled が必要です。これらを省略すると TypeScript のコンパイルが失敗します。実行時に不明なフラグキーを評価すると、指定されたデフォルト値が返されます。タイプチェックされたフラグ構成には、非推奨の InMemoryProvider よりも TypedInMemoryProvider を使用してください。同じテストパターンが Jest + jsdom でも機能します。vitest のインポートを @jest/globals に置き換え、プロジェクトに jest-environment-jsdom を追加してください。