-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
71 lines (63 loc) · 1.97 KB
/
index.js
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 { ON_FLAG, OFF_FLAG, BASE_URL, TRUE_FLAG, FALSE_FLAG } from "./base-constants";
class Features {
constructor(flags, user, apiKey) {
this.user = user;
this.apiKey = apiKey;
this.flags = flags;
this.enabledMap = {};
if (Array.isArray(flags)) {
this.enabledMap = flags.reduce((acc, nxt) => {
const isBoolFlag = [ON_FLAG, OFF_FLAG, TRUE_FLAG, FALSE_FLAG].includes(nxt.variation.name);
if (isBoolFlag) {
const evalStatus =
(nxt.flag.enabled && [ON_FLAG, TRUE_FLAG].includes(nxt.variation.name)) || false;
acc[nxt.flag.key] = evalStatus;
}
return acc;
}, {});
console.log({ thisEnabledMap: this.enabledMap });
}
}
isEnabled(key) {
const result = this.enabledMap[key];
this.flagEvaluated(key);
return Boolean(result); // in case bad key is sent
}
getFlagVariation(key) {
const flag = this.flags.find(({ flag }) => flag.key === key);
if (flag) {
return flag.variation.name;
}
return null;
}
flagEvaluated(flagKey) {
const flageval = this.flags.find(({ flag }) => flag.key === flagKey);
const recordUrl = `${BASE_URL}record/${this.apiKey}`;
const body = JSON.stringify(flageval);
fetch(recordUrl, { body, method: "POST" })
.then(() => {})
.catch((err) => console.error(err));
}
}
async function initializeClient(apiKey, user) {
const flagsUrl = `${BASE_URL}flags/${apiKey}`;
const body = JSON.stringify({ user: user });
let flags;
try {
const res = await fetch(flagsUrl, { body, method: "POST" });
if (res.status === 400) {
console.error("invalid feature flag api key, setting all flags to off");
flags = null;
} else {
flags = await res.json();
}
} catch (err) {
console.error("error fetching feature flags", err);
console.error("features will all evaluate to default");
flags = null;
}
return new Features(flags, user, apiKey);
}
export default {
initializeClient,
};