|
1 | 1 | import * as LaunchDarkly from 'launchdarkly-node-server-sdk'; |
2 | 2 |
|
3 | | -// TODO : Enter your LaunchDarkly SDK key here |
4 | | -const client = LaunchDarkly.init("YOUR_SDK_KEY"); |
| 3 | +// Set sdkKey to your LaunchDarkly SDK key. |
| 4 | +const sdkKey = ""; |
5 | 5 |
|
| 6 | +// Set featureFlagKey to the feature flag key you want to evaluate. |
| 7 | +const featureFlagKey = "my-boolean-flag"; |
| 8 | + |
| 9 | +// Set up the user properties. This use should appear on your LaunchDarkly |
| 10 | +// users dashboard soon after you run the demo. |
6 | 11 | const user = { |
7 | | - "firstName":"Bob", |
8 | | - "lastName":"Loblaw", |
9 | | - "key":"louise@example.com", |
10 | | - "custom":{ |
11 | | - "groups":"beta_testers" |
12 | | - } |
| 12 | + "name": "Sandy", |
| 13 | + "key": "example-user-key" |
13 | 14 | }; |
14 | 15 |
|
| 16 | +function showMessage(s: string) { |
| 17 | + console.log("*** " + s); |
| 18 | + console.log(""); |
| 19 | +} |
| 20 | + |
| 21 | +const client = LaunchDarkly.init(sdkKey); |
| 22 | + |
15 | 23 | client.once('ready', function() { |
16 | | - // TODO : Enter the key for your feature flag here |
17 | | - client.variation("enable-pinning", user, false, function(err, showFeature) { |
| 24 | + showMessage("SDK successfully initialized!"); |
| 25 | + client.variation(featureFlagKey, user, false, function(err, showFeature) { |
18 | 26 | client.track("event-called", user); |
19 | 27 | if (showFeature) { |
20 | 28 | // application code to show the feature |
21 | | - console.log("Showing your feature to " + user.key ); |
| 29 | + showMessage("Feature flag '" + featureFlagKey + "' is true for this user"); |
22 | 30 | } else { |
23 | | - // the code to run if the feature is off |
24 | | - console.log("Not showing your feature to " + user.key); |
| 31 | + // the code to run if the feature is off |
| 32 | + showMessage("Feature flag '" + featureFlagKey + "' is false for this user"); |
25 | 33 | } |
| 34 | + |
| 35 | + // Here we ensure that the SDK shuts down cleanly and has a chance to deliver analytics |
| 36 | + // events to LaunchDarkly before the program exits. If analytics events are not delivered, |
| 37 | + // the user properties and flag usage statistics will not appear on your dashboard. In a |
| 38 | + // normal long-running application, the SDK would continue running and events would be |
| 39 | + // delivered automatically in the background. |
26 | 40 | client.flush(function() { |
27 | 41 | client.close(); |
28 | 42 | }); |
|
0 commit comments