|
| 1 | +import { OpenFeature, ProviderEvents } from '@openfeature/server-sdk'; |
| 2 | + |
| 3 | +import { LaunchDarklyProvider } from '@launchdarkly/openfeature-node-server'; |
| 4 | + |
| 5 | +// The server-side SDK key is read from the LAUNCHDARKLY_SDK_KEY environment variable. |
| 6 | +const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY; |
| 7 | + |
| 8 | +// Set flagKey to the feature flag key you want to evaluate. |
| 9 | +const flagKey = process.env.LAUNCHDARKLY_FLAG_KEY || 'sample-feature'; |
| 10 | + |
| 11 | +if (!sdkKey) { |
| 12 | + console.error( |
| 13 | + '*** LaunchDarkly SDK key is required: set the LAUNCHDARKLY_SDK_KEY environment variable and try again.', |
| 14 | + ); |
| 15 | + process.exit(1); |
| 16 | +} |
| 17 | + |
| 18 | +if (!flagKey) { |
| 19 | + console.error( |
| 20 | + "*** LaunchDarkly flag key is required: set the 'flagKey' variable in src/index.ts, or the LAUNCHDARKLY_FLAG_KEY environment variable and try again.", |
| 21 | + ); |
| 22 | + process.exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +const BANNER = ` ██ |
| 26 | + ██ |
| 27 | + ████████ |
| 28 | + ███████ |
| 29 | +██ LAUNCHDARKLY █ |
| 30 | + ███████ |
| 31 | + ████████ |
| 32 | + ██ |
| 33 | + ██ |
| 34 | +`; |
| 35 | + |
| 36 | +// Set up the evaluation context. This context should appear on your LaunchDarkly contexts dashboard |
| 37 | +// soon after you run the demo. |
| 38 | +const context = { |
| 39 | + kind: 'user', |
| 40 | + targetingKey: 'example-user-key', |
| 41 | + name: 'Sandy', |
| 42 | +}; |
| 43 | + |
| 44 | +let lastFlagValue: boolean | null = null; |
| 45 | + |
| 46 | +function printFlagState(flagValue: boolean): void { |
| 47 | + if (lastFlagValue === flagValue) { |
| 48 | + return; |
| 49 | + } |
| 50 | + console.log(`*** The '${flagKey}' feature flag evaluates to ${flagValue}.\n`); |
| 51 | + if (flagValue) { |
| 52 | + console.log(BANNER); |
| 53 | + } |
| 54 | + lastFlagValue = flagValue; |
| 55 | +} |
| 56 | + |
| 57 | +async function main(): Promise<void> { |
| 58 | + const provider = new LaunchDarklyProvider(sdkKey!); |
| 59 | + |
| 60 | + try { |
| 61 | + await OpenFeature.setProviderAndWait(provider); |
| 62 | + console.log('*** SDK successfully initialized!\n'); |
| 63 | + } catch (error) { |
| 64 | + console.error( |
| 65 | + `*** SDK failed to initialize. Please check your internet connection and SDK credential for any typo.\n${error}`, |
| 66 | + ); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + const ofClient = OpenFeature.getClient(); |
| 71 | + const initialValue = await ofClient.getBooleanValue(flagKey, false, context); |
| 72 | + printFlagState(initialValue); |
| 73 | + |
| 74 | + // Subscribe to provider configuration changes so the demo reacts to LaunchDarkly flag updates |
| 75 | + // without polling. |
| 76 | + ofClient.addHandler(ProviderEvents.ConfigurationChanged, async (eventDetails) => { |
| 77 | + if (eventDetails?.flagsChanged?.includes(flagKey)) { |
| 78 | + const updatedValue = await ofClient.getBooleanValue(flagKey, false, context); |
| 79 | + printFlagState(updatedValue); |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + if (process.env.CI !== undefined) { |
| 84 | + process.exit(0); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +main().catch((error) => { |
| 89 | + console.error(`*** Unhandled error: ${error}`); |
| 90 | + process.exit(1); |
| 91 | +}); |
0 commit comments