-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathanalytics.featureFlags.service.js
More file actions
60 lines (55 loc) · 2.39 KB
/
Copy pathanalytics.featureFlags.service.js
File metadata and controls
60 lines (55 loc) · 2.39 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
/**
* Module dependencies
*/
import AnalyticsService from './analytics.service.js';
/**
* Check whether a feature flag is enabled for a given user.
* Builds the PostHog options object from the provided context so callers
* don't need to know the PostHog SDK shape.
*
* Returns `false` when analytics is not configured (safe default) so that
* downstream projects without PostHog are never blocked.
*
* @param {string} flag - Feature flag key
* @param {string} distinctId - User identifier
* @param {Object} [options] - Evaluation context
* @param {Object} [options.personProperties] - Properties for local evaluation
* @param {Object} [options.groups] - Group identifiers (e.g. { company: orgId })
* @param {Object} [options.groupProperties] - Properties per group type
* @returns {Promise<boolean>} true when the flag is enabled, false otherwise
*/
const isEnabled = async (flag, distinctId, options = {}) => {
const { personProperties, groups, groupProperties } = options;
const phOptions = {};
if (personProperties) phOptions.personProperties = personProperties;
if (groups) phOptions.groups = groups;
if (groupProperties) phOptions.groupProperties = groupProperties;
const result = await AnalyticsService.isFeatureEnabled(flag, distinctId, phOptions);
// Normalise undefined (not configured) to false for a safe default
return result === true;
};
/**
* Get the variant value of a feature flag for a given user.
* Returns the variant key string, a boolean, or `undefined` when analytics
* is not configured.
*
* @param {string} flag - Feature flag key
* @param {string} distinctId - User identifier
* @param {Object} [options] - Evaluation context
* @param {Object} [options.personProperties] - Properties for local evaluation
* @param {Object} [options.groups] - Group identifiers (e.g. { company: orgId })
* @param {Object} [options.groupProperties] - Properties per group type
* @returns {Promise<string|boolean|undefined>} Variant value
*/
const getVariant = async (flag, distinctId, options = {}) => {
const { personProperties, groups, groupProperties } = options;
const phOptions = {};
if (personProperties) phOptions.personProperties = personProperties;
if (groups) phOptions.groups = groups;
if (groupProperties) phOptions.groupProperties = groupProperties;
return AnalyticsService.getFeatureFlag(flag, distinctId, phOptions);
};
export default {
isEnabled,
getVariant,
};