posthog-node 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/event-validation.js +16 -0
- package/feature-flags.js +3 -3
- package/index.d.ts +7 -3
- package/index.js +4 -2
- package/package.json +2 -2
package/event-validation.js
CHANGED
|
@@ -24,6 +24,8 @@ function eventValidation(event, type) {
|
|
|
24
24
|
return validateAliasEvent(event)
|
|
25
25
|
case 'groupIdentify':
|
|
26
26
|
return validateGroupIdentifyEvent(event)
|
|
27
|
+
case 'isFeatureEnabled':
|
|
28
|
+
return validateIsFeatureEnabled(event)
|
|
27
29
|
default:
|
|
28
30
|
assert(0, 'Invalid event type: "' + type + '"')
|
|
29
31
|
}
|
|
@@ -64,6 +66,20 @@ function validateAliasEvent(event) {
|
|
|
64
66
|
assert(event.groupKey, 'You must pass a "groupKey".')
|
|
65
67
|
}
|
|
66
68
|
|
|
69
|
+
/**
|
|
70
|
+
* Validate a "isFeatureEnabled" call
|
|
71
|
+
*/
|
|
72
|
+
|
|
73
|
+
function validateIsFeatureEnabled(event) {
|
|
74
|
+
assert(event.key, 'You must pass a "key".')
|
|
75
|
+
assert(event.distinctId, 'You must pass a "distinctId".')
|
|
76
|
+
assert(type(event.defaultResult) == 'boolean', '"defaultResult" must be a boolean.')
|
|
77
|
+
if (event.groups) {
|
|
78
|
+
assert(type(event.groups) == 'object', 'You must pass an object for "groups".')
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
|
|
67
83
|
/**
|
|
68
84
|
* Validation rules.
|
|
69
85
|
*/
|
package/feature-flags.js
CHANGED
|
@@ -32,7 +32,7 @@ class FeatureFlagsPoller {
|
|
|
32
32
|
void this.loadFeatureFlags()
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
async isFeatureEnabled(key, distinctId, defaultResult = false) {
|
|
35
|
+
async isFeatureEnabled(key, distinctId, defaultResult = false, groups = {}) {
|
|
36
36
|
await this.loadFeatureFlags()
|
|
37
37
|
|
|
38
38
|
if (!this.loadedSuccessfullyOnce) {
|
|
@@ -61,7 +61,7 @@ class FeatureFlagsPoller {
|
|
|
61
61
|
rolloutPercentage: featureFlag.rolloutPercentage,
|
|
62
62
|
})
|
|
63
63
|
} else {
|
|
64
|
-
const res = await this._request({ path: 'decide', method: 'POST', data: { distinct_id: distinctId } })
|
|
64
|
+
const res = await this._request({ path: 'decide', method: 'POST', data: { groups, distinct_id: distinctId } })
|
|
65
65
|
isFlagEnabledResponse = res.data.featureFlags.indexOf(key) >= 0
|
|
66
66
|
}
|
|
67
67
|
|
|
@@ -147,7 +147,7 @@ class FeatureFlagsPoller {
|
|
|
147
147
|
|
|
148
148
|
let res
|
|
149
149
|
try {
|
|
150
|
-
res = await axios(req)
|
|
150
|
+
res = await axios.request(req)
|
|
151
151
|
} catch (err) {
|
|
152
152
|
throw new Error(`Request to ${path} failed with error: ${err.message}`)
|
|
153
153
|
}
|
package/index.d.ts
CHANGED
|
@@ -20,9 +20,12 @@ declare module 'posthog-node' {
|
|
|
20
20
|
groups?: Record<string, string | number> // Mapping of group type to group id
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
+
type GroupType = string
|
|
24
|
+
type GroupKey = string
|
|
25
|
+
|
|
23
26
|
interface GroupIdentifyMessage {
|
|
24
|
-
groupType:
|
|
25
|
-
groupKey:
|
|
27
|
+
groupType: GroupType
|
|
28
|
+
groupKey: GroupKey // Unique identifier for the group
|
|
26
29
|
properties?: Record<string | number, any>
|
|
27
30
|
}
|
|
28
31
|
|
|
@@ -72,8 +75,9 @@ declare module 'posthog-node' {
|
|
|
72
75
|
* @param key the unique key of your feature flag
|
|
73
76
|
* @param distinctId the current unique id
|
|
74
77
|
* @param defaultResult optional - default value to be returned if the feature flag is not on for the user
|
|
78
|
+
* @param groups optional - what groups are currently active (group analytics)
|
|
75
79
|
*/
|
|
76
|
-
isFeatureEnabled(key: string, distinctId: string, defaultResult?: boolean): Promise<boolean>
|
|
80
|
+
isFeatureEnabled(key: string, distinctId: string, defaultResult?: boolean, groups?: Record<GroupType, GroupKey>): Promise<boolean>
|
|
77
81
|
|
|
78
82
|
|
|
79
83
|
/**
|
package/index.js
CHANGED
|
@@ -247,9 +247,11 @@ class PostHog {
|
|
|
247
247
|
}
|
|
248
248
|
}
|
|
249
249
|
|
|
250
|
-
async isFeatureEnabled(key, distinctId, defaultResult) {
|
|
250
|
+
async isFeatureEnabled(key, distinctId, defaultResult = false, groups = {}) {
|
|
251
|
+
this._validate({ key, distinctId, defaultResult, groups }, 'isFeatureEnabled')
|
|
251
252
|
assert(this.personalApiKey, 'You have to specify the option personalApiKey to use feature flags.')
|
|
252
|
-
|
|
253
|
+
|
|
254
|
+
return await this.featureFlagsPoller.isFeatureEnabled(key, distinctId, defaultResult, groups)
|
|
253
255
|
}
|
|
254
256
|
|
|
255
257
|
async reloadFeatureFlags() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "posthog-node",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "PostHog Node.js integration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "PostHog/posthog-node",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"uuid": "^8.3.2"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"ava": "^
|
|
55
|
+
"ava": "^3.15.0",
|
|
56
56
|
"basic-auth": "^2.0.1",
|
|
57
57
|
"body-parser": "^1.17.1",
|
|
58
58
|
"codecov": "^3.0.0",
|