Last active
February 2, 2022 19:59
-
-
Save remotesynth/1659e36dc64c7df0139860b5d77adbe1 to your computer and use it in GitHub Desktop.
LaunchDarkly in Node.js with TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as LaunchDarkly from "launchdarkly-node-server-sdk"; | |
import * as dotenv from "dotenv"; | |
dotenv.config({ path: __dirname + "/.env" }); | |
let ldClient: LaunchDarkly.LDClient; | |
async function getClient(): Promise<LaunchDarkly.LDClient> { | |
const client = LaunchDarkly.init(process.env.LAUNCHDARKLY_SDK_KEY); | |
await client.waitForInitialization(); | |
return client; | |
} | |
async function getFlagValue( | |
key: string, | |
user: LaunchDarkly.LDUser | null, | |
defaultValue: any = false | |
): Promise<LaunchDarkly.LDFlagValue> { | |
let flagValue: LaunchDarkly.LDFlagValue; | |
if (!ldClient) ldClient = await getClient(); | |
if (!user) { | |
user = { | |
key: "anonymous", | |
}; | |
} | |
flagValue = await ldClient.variation(key, user, defaultValue); | |
return flagValue; | |
} | |
async function getUsername(): Promise<any> { | |
const username: string = await getFlagValue("featured-username", null); | |
console.log(username); | |
} | |
getUsername(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment