-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathgrowthbook.js
More file actions
64 lines (57 loc) · 1.5 KB
/
Copy pathgrowthbook.js
File metadata and controls
64 lines (57 loc) · 1.5 KB
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
import { GrowthBook } from '@growthbook/growthbook';
import { GROWTH_BOOK_API_HOST, GROWTH_BOOK_CLIENT_KEY } from '@env';
let growthBookClient;
/**
* creates an instance of the growthBook client
* @returns growthBook client
*/
export const createGrowthBookClient = userEmail => {
const growthBook = new GrowthBook({
apiHost: GROWTH_BOOK_API_HOST,
clientKey: GROWTH_BOOK_CLIENT_KEY,
enableDevMode: true,
subscribeToChanges: true
});
growthBook.setAttributes({
email: userEmail
});
return growthBook;
};
/**
* Function to get the GrowthBook client instance
* @returns {GrowthBook} growthBook instance
*/
export function getGrowthBookClient(email) {
if (growthBookClient) {
return growthBookClient;
}
return createGrowthBookClient(email);
}
/**
* Function to get growthBook feature
* @param {String} email
* @returns {Applicant} growthBook feature value
*/
export async function getGrowthBookFeaturesData(name, email) {
try {
const growthBook = getGrowthBookClient(email);
await growthBook.loadFeatures();
return growthBook.getFeatureValue(name);
} catch (error) {
return new Error(error);
}
}
/**
* Function to get growthBook feature
* @param {String} name
* @returns {Boolean} growthBook feature status
*/
export async function getGrowthBookFeatureFlag(name, email) {
try {
const growthBook = getGrowthBookClient(email);
await growthBook.loadFeatures();
return growthBook.isOn(name);
} catch (error) {
return new Error(error);
}
}