-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
71 lines (55 loc) · 1.84 KB
/
index.ts
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
import * as LaunchDarkly from '@launchdarkly/node-server-sdk';
// Set sdkKey to your LaunchDarkly SDK key.
const sdkKey = process.env.LAUNCHDARKLY_SDK_KEY ?? 'your-sdk-key';
// Set featureFlagKey to the feature flag key you want to evaluate.
const featureFlagKey = process.env.LAUNCHDARKLY_FLAG_KEY ?? 'sample-feature';
function showBanner() {
console.log(
` ██
██
████████
███████
██ LAUNCHDARKLY █
███████
████████
██
██
`,
);
}
function printValueAndBanner(flagValue: boolean) {
console.log(`*** The '${featureFlagKey}' feature flag evaluates to ${flagValue}.`);
if (flagValue) showBanner();
}
if (sdkKey === 'your-sdk-key' || !sdkKey) {
console.log('*** Please specify an SDK key by editing index.ts or setting the LAUNCHDARKLY_SDK_KEY environment variable.');
process.exit(1);
}
const ldClient = LaunchDarkly.init(sdkKey);
// Set up the context properties. This context should appear on your LaunchDarkly contexts dashboard
// soon after you run the demo.
const context = {
kind: 'user',
key: 'example-user-key',
name: 'Sandy',
};
async function main() {
try {
await ldClient.waitForInitialization({timeout: 10});
console.log('*** SDK successfully initialized!');
const eventKey = `update:${featureFlagKey}`;
ldClient.on(eventKey, async () => {
const flagValue = await ldClient.variation(featureFlagKey, context, false);
printValueAndBanner(flagValue);
});
const flagValue = await ldClient.variation(featureFlagKey, context, false);
printValueAndBanner(flagValue);
if (typeof process.env.CI !== "undefined") {
process.exit(0);
}
} catch (error) {
console.log(`*** SDK failed to initialize: ${error}`);
process.exit(1);
}
}
main();