|
| 1 | +const Experiment = require('@amplitude/experiment-node-server'); |
| 2 | +const _ = require('lodash'); |
| 3 | + |
| 4 | +var experiment; |
| 5 | +var debug = process.env.LOCAL_EVALUATION_CONFIG_DEBUG || false; |
| 6 | +var serverUrl = process.env.LOCAL_EVALUATION_CONFIG_SERVER_URL || "https://api.lab.amplitude.com"; |
| 7 | +var flagConfigPollingIntervalMillis = process.env.LOCAL_EVALUATION_CONFIG_POLL_INTERVAL || 10; |
| 8 | +var deploymentKey = process.env.LOCAL_EVALUATION_DEPLOYMENT_KEY || ""; |
| 9 | + |
| 10 | +function validateuser(user) { |
| 11 | + let userProperties = {}; |
| 12 | + if (user && user.org_id && typeof user.org_id === "string") { |
| 13 | + userProperties.org_id = user.org_id; |
| 14 | + } |
| 15 | + if (user && user.org_name && typeof user.org_name === "string") { |
| 16 | + userProperties.org_name = user.org_name; |
| 17 | + } |
| 18 | + if (user && user.username && typeof user.username === "string") { |
| 19 | + userProperties.username = user.username; |
| 20 | + } |
| 21 | + if (user && user.email && typeof user.email === "string") { |
| 22 | + userProperties.email = user.email; |
| 23 | + } |
| 24 | + if (user && user.plan && typeof user.plan === "string") { |
| 25 | + userProperties.plan = user.plan; |
| 26 | + } |
| 27 | + if (user && user.hub_region && typeof user.hub_region === "string") { |
| 28 | + userProperties.hub_region = user.hub_region; |
| 29 | + } |
| 30 | + if (user && user.org_status && typeof user.org_status === "string") { |
| 31 | + userProperties.org_status = user.org_status; |
| 32 | + } |
| 33 | + if (user && user.subscription_type && typeof user.subscription_type === "string") { |
| 34 | + userProperties.subscription_type = user.subscription_type; |
| 35 | + } |
| 36 | + if (user && user.subscription_status && typeof user.subscription_status === "string") { |
| 37 | + userProperties.subscription_status = user.subscription_status; |
| 38 | + } |
| 39 | + return userProperties; |
| 40 | +} |
| 41 | + |
| 42 | +async function Initialize() { |
| 43 | + try { |
| 44 | + let config = {}; |
| 45 | + config.debug = debug; |
| 46 | + config.serverUrl = serverUrl; |
| 47 | + config.flagConfigPollingIntervalMillis = flagConfigPollingIntervalMillis * 1000; |
| 48 | + experiment = Experiment.Experiment.initializeLocal(deploymentKey, config); |
| 49 | + await experiment.start(); |
| 50 | + } catch (e) { |
| 51 | + throw new Error(`unable to create local evaluation client with error ${e.message}`) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function fetch(flagName, user) { |
| 56 | + try { |
| 57 | + const userProp = validateuser(user); |
| 58 | + const expUser = {user_properties: userProp}; |
| 59 | + return await experiment.evaluate(expUser, [flagName]); |
| 60 | + } catch (e) { |
| 61 | + return new Error(`error in evaluating flag: ${flagName} error: ${e}`) |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +async function GetFeatureFlagString(flagName, user) { |
| 66 | + const data = await fetch(flagName, user); |
| 67 | + if (data.error || !data[flagName]) { |
| 68 | + return ""; |
| 69 | + } |
| 70 | + return data[flagName].value.toLowerCase(); |
| 71 | +} |
| 72 | + |
| 73 | +async function GetFeatureFlagBool(flagName, user) { |
| 74 | + const data = await fetch(flagName, user); |
| 75 | + if (data.error || !data[flagName]) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + return data[flagName].value.toLowerCase() === 'true'; |
| 79 | +} |
| 80 | + |
| 81 | +async function GetFeatureFlagPayload(flagName, user) { |
| 82 | + const data = await fetch(flagName, user); |
| 83 | + if (data.error || !data[flagName]) { |
| 84 | + return {}; |
| 85 | + } |
| 86 | + return data[flagName]; |
| 87 | +} |
| 88 | + |
| 89 | +module.exports = {GetFeatureFlagPayload, GetFeatureFlagBool, GetFeatureFlagString, Initialize} |
0 commit comments