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