posthog-node 2.2.2 → 2.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/CHANGELOG.md +8 -0
- package/lib/index.cjs.js +296 -30
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +60 -1
- package/lib/index.esm.js +296 -30
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/index.d.ts +9 -1
- package/lib/posthog-core/src/types.d.ts +13 -0
- package/lib/posthog-node/src/feature-flags.d.ts +5 -2
- package/lib/posthog-node/src/posthog-node.d.ts +14 -1
- package/lib/posthog-node/src/types.d.ts +19 -0
- package/package.json +1 -1
- package/src/feature-flags.ts +53 -8
- package/src/posthog-node.ts +99 -8
- package/src/types.ts +26 -0
- package/test/feature-flags.spec.ts +382 -3
- package/test/posthog-node.spec.ts +46 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
+
import { JsonType as JsonType$1 } from 'posthog-core/src';
|
|
3
|
+
|
|
2
4
|
declare type PosthogCoreOptions = {
|
|
3
5
|
host?: string;
|
|
4
6
|
flushAt?: number;
|
|
@@ -10,6 +12,7 @@ declare type PosthogCoreOptions = {
|
|
|
10
12
|
distinctId?: string;
|
|
11
13
|
isIdentifiedId?: boolean;
|
|
12
14
|
featureFlags?: Record<string, boolean | string>;
|
|
15
|
+
featureFlagPayloads?: Record<string, JsonType>;
|
|
13
16
|
};
|
|
14
17
|
fetchRetryCount?: number;
|
|
15
18
|
fetchRetryDelay?: number;
|
|
@@ -31,7 +34,33 @@ declare type PostHogFetchResponse = {
|
|
|
31
34
|
status: number;
|
|
32
35
|
text: () => Promise<string>;
|
|
33
36
|
json: () => Promise<any>;
|
|
34
|
-
};
|
|
37
|
+
};
|
|
38
|
+
declare type PostHogDecideResponse = {
|
|
39
|
+
config: {
|
|
40
|
+
enable_collect_everything: boolean;
|
|
41
|
+
};
|
|
42
|
+
editorParams: {
|
|
43
|
+
toolbarVersion: string;
|
|
44
|
+
jsURL: string;
|
|
45
|
+
};
|
|
46
|
+
isAuthenticated: true;
|
|
47
|
+
supportedCompression: string[];
|
|
48
|
+
featureFlags: {
|
|
49
|
+
[key: string]: string | boolean;
|
|
50
|
+
};
|
|
51
|
+
featureFlagPayloads: {
|
|
52
|
+
[key: string]: JsonType;
|
|
53
|
+
};
|
|
54
|
+
errorsWhileComputingFlags: boolean;
|
|
55
|
+
sessionRecording: boolean;
|
|
56
|
+
};
|
|
57
|
+
declare type PosthogFlagsAndPayloadsResponse = {
|
|
58
|
+
featureFlags: PostHogDecideResponse['featureFlags'];
|
|
59
|
+
featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];
|
|
60
|
+
};
|
|
61
|
+
declare type JsonType = string | number | boolean | null | {
|
|
62
|
+
[key: string]: JsonType;
|
|
63
|
+
} | Array<JsonType>;
|
|
35
64
|
|
|
36
65
|
interface IdentifyMessageV1 {
|
|
37
66
|
distinctId: string;
|
|
@@ -129,6 +158,23 @@ declare type PostHogNodeV1 = {
|
|
|
129
158
|
onlyEvaluateLocally?: boolean;
|
|
130
159
|
sendFeatureFlagEvents?: boolean;
|
|
131
160
|
}): Promise<string | boolean | undefined>;
|
|
161
|
+
/**
|
|
162
|
+
* @description Retrieves payload associated with the specified flag and matched value that is passed in.
|
|
163
|
+
* (Expected to be used in conjuction with getFeatureFlag but allows for manual lookup).
|
|
164
|
+
* If matchValue isn't passed, getFeatureFlag is called implicitly.
|
|
165
|
+
* Will try to evaluate for payload locally first otherwise default to network call if allowed
|
|
166
|
+
*
|
|
167
|
+
* @param key the unique key of your feature flag
|
|
168
|
+
* @param distinctId the current unique id
|
|
169
|
+
* @param matchValue optional- the matched flag string or boolean
|
|
170
|
+
* @param options: dict with optional parameters below
|
|
171
|
+
* @param onlyEvaluateLocally optional - whether to only evaluate the flag locally. Defaults to false.
|
|
172
|
+
*
|
|
173
|
+
* @returns payload of a json type object
|
|
174
|
+
*/
|
|
175
|
+
getFeatureFlagPayload(key: string, distinctId: string, matchValue?: string | boolean, options?: {
|
|
176
|
+
onlyEvaluateLocally?: boolean;
|
|
177
|
+
}): Promise<JsonType$1 | undefined>;
|
|
132
178
|
/**
|
|
133
179
|
* @description Sets a groups properties, which allows asking questions like "Who are the most active companies"
|
|
134
180
|
* using my product in PostHog.
|
|
@@ -180,6 +226,13 @@ declare class PostHog implements PostHogNodeV1 {
|
|
|
180
226
|
onlyEvaluateLocally?: boolean;
|
|
181
227
|
sendFeatureFlagEvents?: boolean;
|
|
182
228
|
}): Promise<string | boolean | undefined>;
|
|
229
|
+
getFeatureFlagPayload(key: string, distinctId: string, matchValue?: string | boolean, options?: {
|
|
230
|
+
groups?: Record<string, string>;
|
|
231
|
+
personProperties?: Record<string, string>;
|
|
232
|
+
groupProperties?: Record<string, Record<string, string>>;
|
|
233
|
+
onlyEvaluateLocally?: boolean;
|
|
234
|
+
sendFeatureFlagEvents?: boolean;
|
|
235
|
+
}): Promise<JsonType | undefined>;
|
|
183
236
|
isFeatureEnabled(key: string, distinctId: string, options?: {
|
|
184
237
|
groups?: Record<string, string>;
|
|
185
238
|
personProperties?: Record<string, string>;
|
|
@@ -193,6 +246,12 @@ declare class PostHog implements PostHogNodeV1 {
|
|
|
193
246
|
groupProperties?: Record<string, Record<string, string>>;
|
|
194
247
|
onlyEvaluateLocally?: boolean;
|
|
195
248
|
}): Promise<Record<string, string | boolean>>;
|
|
249
|
+
getAllFlagsAndPayloads(distinctId: string, options?: {
|
|
250
|
+
groups?: Record<string, string>;
|
|
251
|
+
personProperties?: Record<string, string>;
|
|
252
|
+
groupProperties?: Record<string, Record<string, string>>;
|
|
253
|
+
onlyEvaluateLocally?: boolean;
|
|
254
|
+
}): Promise<PosthogFlagsAndPayloadsResponse>;
|
|
196
255
|
groupIdentify({ groupType, groupKey, properties }: GroupIdentifyMessage): void;
|
|
197
256
|
reloadFeatureFlags(): Promise<void>;
|
|
198
257
|
flush(): void;
|
package/lib/index.esm.js
CHANGED
|
@@ -155,7 +155,7 @@ function __spreadArray(to, from, pack) {
|
|
|
155
155
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
156
156
|
}
|
|
157
157
|
|
|
158
|
-
var version = "2.
|
|
158
|
+
var version = "2.3.0";
|
|
159
159
|
|
|
160
160
|
var PostHogPersistedProperty;
|
|
161
161
|
(function (PostHogPersistedProperty) {
|
|
@@ -163,6 +163,7 @@ var PostHogPersistedProperty;
|
|
|
163
163
|
PostHogPersistedProperty["DistinctId"] = "distinct_id";
|
|
164
164
|
PostHogPersistedProperty["Props"] = "props";
|
|
165
165
|
PostHogPersistedProperty["FeatureFlags"] = "feature_flags";
|
|
166
|
+
PostHogPersistedProperty["FeatureFlagPayloads"] = "feature_flag_payloads";
|
|
166
167
|
PostHogPersistedProperty["OverrideFeatureFlags"] = "override_feature_flags";
|
|
167
168
|
PostHogPersistedProperty["Queue"] = "queue";
|
|
168
169
|
PostHogPersistedProperty["OptedOut"] = "opted_out";
|
|
@@ -771,6 +772,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
771
772
|
return ((res[key] = ((_b = (_a = options.bootstrap) === null || _a === void 0 ? void 0 : _a.featureFlags) === null || _b === void 0 ? void 0 : _b[key]) || false), res);
|
|
772
773
|
}, {});
|
|
773
774
|
this.setKnownFeatureFlags(activeFlags);
|
|
775
|
+
(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads) && this.setKnownFeatureFlagPayloads(options === null || options === void 0 ? void 0 : options.bootstrap.featureFlagPayloads);
|
|
774
776
|
}
|
|
775
777
|
};
|
|
776
778
|
Object.defineProperty(PostHogCore.prototype, "props", {
|
|
@@ -950,11 +952,10 @@ var PostHogCore = /** @class */ (function () {
|
|
|
950
952
|
return this;
|
|
951
953
|
};
|
|
952
954
|
PostHogCore.prototype.groupIdentify = function (groupType, groupKey, groupProperties) {
|
|
953
|
-
var payload = {
|
|
955
|
+
var payload = this.buildPayload({
|
|
954
956
|
event: '$groupidentify',
|
|
955
|
-
distinctId: "$".concat(groupType, "_").concat(groupKey),
|
|
956
957
|
properties: __assign({ $group_type: groupType, $group_key: groupKey, $group_set: groupProperties || {} }, this.getCommonEventProperties()),
|
|
957
|
-
};
|
|
958
|
+
});
|
|
958
959
|
this.enqueue('capture', payload);
|
|
959
960
|
return this;
|
|
960
961
|
};
|
|
@@ -995,7 +996,7 @@ var PostHogCore = /** @class */ (function () {
|
|
|
995
996
|
var url, distinctId, groups, personProperties, groupProperties, fetchOptions;
|
|
996
997
|
var _this = this;
|
|
997
998
|
return __generator(this, function (_a) {
|
|
998
|
-
url = "".concat(this.host, "/decide/?v=
|
|
999
|
+
url = "".concat(this.host, "/decide/?v=3");
|
|
999
1000
|
distinctId = this.getDistinctId();
|
|
1000
1001
|
groups = this.props.$groups || {};
|
|
1001
1002
|
personProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
|
|
@@ -1016,7 +1017,17 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1016
1017
|
.then(function (r) { return r.json(); })
|
|
1017
1018
|
.then(function (res) {
|
|
1018
1019
|
if (res.featureFlags) {
|
|
1019
|
-
|
|
1020
|
+
var newFeatureFlags = res.featureFlags;
|
|
1021
|
+
var newFeatureFlagPayloads = res.featureFlagPayloads;
|
|
1022
|
+
if (res.errorsWhileComputingFlags) {
|
|
1023
|
+
// if not all flags were computed, we upsert flags instead of replacing them
|
|
1024
|
+
var currentFlags = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
1025
|
+
var currentFlagPayloads = _this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
|
|
1026
|
+
newFeatureFlags = __assign(__assign({}, currentFlags), res.featureFlags);
|
|
1027
|
+
newFeatureFlagPayloads = __assign(__assign({}, currentFlagPayloads), res.featureFlagPayloads);
|
|
1028
|
+
}
|
|
1029
|
+
_this.setKnownFeatureFlags(newFeatureFlags);
|
|
1030
|
+
_this.setKnownFeatureFlagPayloads(newFeatureFlagPayloads);
|
|
1020
1031
|
}
|
|
1021
1032
|
return res;
|
|
1022
1033
|
})
|
|
@@ -1031,6 +1042,9 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1031
1042
|
this.setPersistedProperty(PostHogPersistedProperty.FeatureFlags, featureFlags);
|
|
1032
1043
|
this._events.emit('featureflags', featureFlags);
|
|
1033
1044
|
};
|
|
1045
|
+
PostHogCore.prototype.setKnownFeatureFlagPayloads = function (featureFlagPayloads) {
|
|
1046
|
+
this.setPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads, featureFlagPayloads);
|
|
1047
|
+
};
|
|
1034
1048
|
PostHogCore.prototype.getFeatureFlag = function (key) {
|
|
1035
1049
|
var featureFlags = this.getFeatureFlags();
|
|
1036
1050
|
if (!featureFlags) {
|
|
@@ -1038,8 +1052,9 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1038
1052
|
return undefined;
|
|
1039
1053
|
}
|
|
1040
1054
|
var response = featureFlags[key];
|
|
1055
|
+
// `/decide` v3 returns all flags
|
|
1041
1056
|
if (response === undefined) {
|
|
1042
|
-
//
|
|
1057
|
+
// For cases where the flag is unknown, return false
|
|
1043
1058
|
response = false;
|
|
1044
1059
|
}
|
|
1045
1060
|
if (this.sendFeatureFlagEvent && !this.flagCallReported[key]) {
|
|
@@ -1052,6 +1067,37 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1052
1067
|
// If we have flags we either return the value (true or string) or false
|
|
1053
1068
|
return response;
|
|
1054
1069
|
};
|
|
1070
|
+
PostHogCore.prototype.getFeatureFlagPayload = function (key) {
|
|
1071
|
+
var payloads = this.getFeatureFlagPayloads();
|
|
1072
|
+
if (!payloads) {
|
|
1073
|
+
return undefined;
|
|
1074
|
+
}
|
|
1075
|
+
var response = payloads[key];
|
|
1076
|
+
// Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
|
|
1077
|
+
if (response === undefined) {
|
|
1078
|
+
return null;
|
|
1079
|
+
}
|
|
1080
|
+
return this._parsePayload(response);
|
|
1081
|
+
};
|
|
1082
|
+
PostHogCore.prototype.getFeatureFlagPayloads = function () {
|
|
1083
|
+
var _this = this;
|
|
1084
|
+
var payloads = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlagPayloads);
|
|
1085
|
+
if (payloads) {
|
|
1086
|
+
return Object.fromEntries(Object.entries(payloads).map(function (_a) {
|
|
1087
|
+
var k = _a[0], v = _a[1];
|
|
1088
|
+
return [k, _this._parsePayload(v)];
|
|
1089
|
+
}));
|
|
1090
|
+
}
|
|
1091
|
+
return payloads;
|
|
1092
|
+
};
|
|
1093
|
+
PostHogCore.prototype._parsePayload = function (response) {
|
|
1094
|
+
try {
|
|
1095
|
+
return JSON.parse(response);
|
|
1096
|
+
}
|
|
1097
|
+
catch (_a) {
|
|
1098
|
+
return response;
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1055
1101
|
PostHogCore.prototype.getFeatureFlags = function () {
|
|
1056
1102
|
var flags = this.getPersistedProperty(PostHogPersistedProperty.FeatureFlags);
|
|
1057
1103
|
var overriddenFlags = this.getPersistedProperty(PostHogPersistedProperty.OverrideFeatureFlags);
|
|
@@ -1069,6 +1115,14 @@ var PostHogCore = /** @class */ (function () {
|
|
|
1069
1115
|
}
|
|
1070
1116
|
return flags;
|
|
1071
1117
|
};
|
|
1118
|
+
PostHogCore.prototype.getFeatureFlagsAndPayloads = function () {
|
|
1119
|
+
var flags = this.getFeatureFlags();
|
|
1120
|
+
var payloads = this.getFeatureFlagPayloads();
|
|
1121
|
+
return {
|
|
1122
|
+
flags: flags,
|
|
1123
|
+
payloads: payloads,
|
|
1124
|
+
};
|
|
1125
|
+
};
|
|
1072
1126
|
PostHogCore.prototype.isFeatureEnabled = function (key) {
|
|
1073
1127
|
var response = this.getFeatureFlag(key);
|
|
1074
1128
|
if (response === undefined) {
|
|
@@ -1357,6 +1411,7 @@ function () {
|
|
|
1357
1411
|
this.pollingInterval = pollingInterval;
|
|
1358
1412
|
this.personalApiKey = personalApiKey;
|
|
1359
1413
|
this.featureFlags = [];
|
|
1414
|
+
this.featureFlagsByKey = {};
|
|
1360
1415
|
this.groupTypeMapping = {};
|
|
1361
1416
|
this.loadedSuccessfullyOnce = false;
|
|
1362
1417
|
this.timeout = timeout;
|
|
@@ -1415,10 +1470,9 @@ function () {
|
|
|
1415
1470
|
if (featureFlag !== undefined) {
|
|
1416
1471
|
try {
|
|
1417
1472
|
response = this.computeFlagLocally(featureFlag, distinctId, groups, personProperties, groupProperties);
|
|
1418
|
-
console.debug("Successfully computed flag locally: ".concat(key, " -> ").concat(response));
|
|
1419
1473
|
} catch (e) {
|
|
1420
1474
|
if (e instanceof InconclusiveMatchError) {
|
|
1421
|
-
console.
|
|
1475
|
+
console.error("InconclusiveMatchError when computing flag locally: ".concat(key, ": ").concat(e));
|
|
1422
1476
|
} else if (e instanceof Error) {
|
|
1423
1477
|
console.error("Error computing flag locally: ".concat(key, ": ").concat(e));
|
|
1424
1478
|
}
|
|
@@ -1433,7 +1487,51 @@ function () {
|
|
|
1433
1487
|
});
|
|
1434
1488
|
};
|
|
1435
1489
|
|
|
1436
|
-
FeatureFlagsPoller.prototype.
|
|
1490
|
+
FeatureFlagsPoller.prototype.computeFeatureFlagPayloadLocally = function (key, matchValue) {
|
|
1491
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
1492
|
+
|
|
1493
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1494
|
+
var response;
|
|
1495
|
+
return __generator(this, function (_j) {
|
|
1496
|
+
switch (_j.label) {
|
|
1497
|
+
case 0:
|
|
1498
|
+
return [4
|
|
1499
|
+
/*yield*/
|
|
1500
|
+
, this.loadFeatureFlags()];
|
|
1501
|
+
|
|
1502
|
+
case 1:
|
|
1503
|
+
_j.sent();
|
|
1504
|
+
|
|
1505
|
+
response = undefined;
|
|
1506
|
+
|
|
1507
|
+
if (!this.loadedSuccessfullyOnce) {
|
|
1508
|
+
return [2
|
|
1509
|
+
/*return*/
|
|
1510
|
+
, undefined];
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
if (typeof matchValue == 'boolean') {
|
|
1514
|
+
response = (_d = (_c = (_b = (_a = this.featureFlagsByKey) === null || _a === void 0 ? void 0 : _a[key]) === null || _b === void 0 ? void 0 : _b.filters) === null || _c === void 0 ? void 0 : _c.payloads) === null || _d === void 0 ? void 0 : _d[matchValue.toString()];
|
|
1515
|
+
} else if (typeof matchValue == 'string') {
|
|
1516
|
+
response = (_h = (_g = (_f = (_e = this.featureFlagsByKey) === null || _e === void 0 ? void 0 : _e[key]) === null || _f === void 0 ? void 0 : _f.filters) === null || _g === void 0 ? void 0 : _g.payloads) === null || _h === void 0 ? void 0 : _h[matchValue];
|
|
1517
|
+
} // Undefined means a loading or missing data issue. Null means evaluation happened and there was no match
|
|
1518
|
+
|
|
1519
|
+
|
|
1520
|
+
if (response === undefined) {
|
|
1521
|
+
return [2
|
|
1522
|
+
/*return*/
|
|
1523
|
+
, null];
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
return [2
|
|
1527
|
+
/*return*/
|
|
1528
|
+
, response];
|
|
1529
|
+
}
|
|
1530
|
+
});
|
|
1531
|
+
});
|
|
1532
|
+
};
|
|
1533
|
+
|
|
1534
|
+
FeatureFlagsPoller.prototype.getAllFlagsAndPayloads = function (distinctId, groups, personProperties, groupProperties) {
|
|
1437
1535
|
if (groups === void 0) {
|
|
1438
1536
|
groups = {};
|
|
1439
1537
|
}
|
|
@@ -1447,7 +1545,7 @@ function () {
|
|
|
1447
1545
|
}
|
|
1448
1546
|
|
|
1449
1547
|
return __awaiter(this, void 0, void 0, function () {
|
|
1450
|
-
var response, fallbackToDecide;
|
|
1548
|
+
var response, payloads, fallbackToDecide;
|
|
1451
1549
|
|
|
1452
1550
|
var _this = this;
|
|
1453
1551
|
|
|
@@ -1462,22 +1560,58 @@ function () {
|
|
|
1462
1560
|
_a.sent();
|
|
1463
1561
|
|
|
1464
1562
|
response = {};
|
|
1563
|
+
payloads = {};
|
|
1465
1564
|
fallbackToDecide = this.featureFlags.length == 0;
|
|
1466
1565
|
this.featureFlags.map(function (flag) {
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1566
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
1567
|
+
var matchValue, matchPayload, e_1;
|
|
1568
|
+
return __generator(this, function (_a) {
|
|
1569
|
+
switch (_a.label) {
|
|
1570
|
+
case 0:
|
|
1571
|
+
_a.trys.push([0, 2,, 3]);
|
|
1572
|
+
|
|
1573
|
+
matchValue = this.computeFlagLocally(flag, distinctId, groups, personProperties, groupProperties);
|
|
1574
|
+
response[flag.key] = matchValue;
|
|
1575
|
+
return [4
|
|
1576
|
+
/*yield*/
|
|
1577
|
+
, this.computeFeatureFlagPayloadLocally(flag.key, matchValue)];
|
|
1578
|
+
|
|
1579
|
+
case 1:
|
|
1580
|
+
matchPayload = _a.sent();
|
|
1581
|
+
|
|
1582
|
+
if (matchPayload) {
|
|
1583
|
+
payloads[flag.key] = matchPayload;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
return [3
|
|
1587
|
+
/*break*/
|
|
1588
|
+
, 3];
|
|
1589
|
+
|
|
1590
|
+
case 2:
|
|
1591
|
+
e_1 = _a.sent();
|
|
1592
|
+
|
|
1593
|
+
if (e_1 instanceof InconclusiveMatchError) ; else if (e_1 instanceof Error) {
|
|
1594
|
+
console.error("Error computing flag locally: ".concat(flag.key, ": ").concat(e_1));
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
fallbackToDecide = true;
|
|
1598
|
+
return [3
|
|
1599
|
+
/*break*/
|
|
1600
|
+
, 3];
|
|
1601
|
+
|
|
1602
|
+
case 3:
|
|
1603
|
+
return [2
|
|
1604
|
+
/*return*/
|
|
1605
|
+
];
|
|
1606
|
+
}
|
|
1607
|
+
});
|
|
1608
|
+
});
|
|
1476
1609
|
});
|
|
1477
1610
|
return [2
|
|
1478
1611
|
/*return*/
|
|
1479
1612
|
, {
|
|
1480
1613
|
response: response,
|
|
1614
|
+
payloads: payloads,
|
|
1481
1615
|
fallbackToDecide: fallbackToDecide
|
|
1482
1616
|
}];
|
|
1483
1617
|
}
|
|
@@ -1718,6 +1852,14 @@ function () {
|
|
|
1718
1852
|
throw new ClientError("Your personalApiKey is invalid. Are you sure you're not using your Project API key? More information: https://posthog.com/docs/api/overview");
|
|
1719
1853
|
}
|
|
1720
1854
|
|
|
1855
|
+
if (res && res.status !== 200) {
|
|
1856
|
+
// something else went wrong, or the server is down.
|
|
1857
|
+
// In this case, don't override existing flags
|
|
1858
|
+
return [2
|
|
1859
|
+
/*return*/
|
|
1860
|
+
];
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1721
1863
|
return [4
|
|
1722
1864
|
/*yield*/
|
|
1723
1865
|
, res.json()];
|
|
@@ -1730,6 +1872,9 @@ function () {
|
|
|
1730
1872
|
}
|
|
1731
1873
|
|
|
1732
1874
|
this.featureFlags = responseJson.flags || [];
|
|
1875
|
+
this.featureFlagsByKey = this.featureFlags.reduce(function (acc, curr) {
|
|
1876
|
+
return acc[curr.key] = curr, acc;
|
|
1877
|
+
}, {});
|
|
1733
1878
|
this.groupTypeMapping = responseJson.group_type_mapping || {};
|
|
1734
1879
|
this.loadedSuccessfullyOnce = true;
|
|
1735
1880
|
return [3
|
|
@@ -2001,7 +2146,7 @@ function () {
|
|
|
2001
2146
|
pollingInterval: typeof options.featureFlagsPollingInterval === 'number' ? options.featureFlagsPollingInterval : THIRTY_SECONDS,
|
|
2002
2147
|
personalApiKey: options.personalApiKey,
|
|
2003
2148
|
projectApiKey: apiKey,
|
|
2004
|
-
timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a :
|
|
2149
|
+
timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a : 10000,
|
|
2005
2150
|
host: this._sharedClient.host,
|
|
2006
2151
|
fetch: options.fetch
|
|
2007
2152
|
});
|
|
@@ -2143,6 +2288,101 @@ function () {
|
|
|
2143
2288
|
});
|
|
2144
2289
|
};
|
|
2145
2290
|
|
|
2291
|
+
PostHog.prototype.getFeatureFlagPayload = function (key, distinctId, matchValue, options) {
|
|
2292
|
+
var _a;
|
|
2293
|
+
|
|
2294
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2295
|
+
var _b, groups, personProperties, groupProperties, _c, onlyEvaluateLocally, response, payloadWasLocallyEvaluated;
|
|
2296
|
+
|
|
2297
|
+
return __generator(this, function (_d) {
|
|
2298
|
+
switch (_d.label) {
|
|
2299
|
+
case 0:
|
|
2300
|
+
_b = options || {}, groups = _b.groups, personProperties = _b.personProperties, groupProperties = _b.groupProperties;
|
|
2301
|
+
_c = options || {}, onlyEvaluateLocally = _c.onlyEvaluateLocally, _c.sendFeatureFlagEvents;
|
|
2302
|
+
response = undefined;
|
|
2303
|
+
if (!!matchValue) return [3
|
|
2304
|
+
/*break*/
|
|
2305
|
+
, 2];
|
|
2306
|
+
return [4
|
|
2307
|
+
/*yield*/
|
|
2308
|
+
, this.getFeatureFlag(key, distinctId, __assign(__assign({}, options), {
|
|
2309
|
+
onlyEvaluateLocally: true
|
|
2310
|
+
}))];
|
|
2311
|
+
|
|
2312
|
+
case 1:
|
|
2313
|
+
matchValue = _d.sent();
|
|
2314
|
+
_d.label = 2;
|
|
2315
|
+
|
|
2316
|
+
case 2:
|
|
2317
|
+
if (!matchValue) return [3
|
|
2318
|
+
/*break*/
|
|
2319
|
+
, 4];
|
|
2320
|
+
return [4
|
|
2321
|
+
/*yield*/
|
|
2322
|
+
, (_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.computeFeatureFlagPayloadLocally(key, matchValue)];
|
|
2323
|
+
|
|
2324
|
+
case 3:
|
|
2325
|
+
response = _d.sent();
|
|
2326
|
+
_d.label = 4;
|
|
2327
|
+
|
|
2328
|
+
case 4:
|
|
2329
|
+
// set defaults
|
|
2330
|
+
if (onlyEvaluateLocally == undefined) {
|
|
2331
|
+
onlyEvaluateLocally = false;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
|
|
2335
|
+
if (onlyEvaluateLocally == undefined) {
|
|
2336
|
+
onlyEvaluateLocally = false;
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
payloadWasLocallyEvaluated = response !== undefined;
|
|
2340
|
+
if (!(!payloadWasLocallyEvaluated && !onlyEvaluateLocally)) return [3
|
|
2341
|
+
/*break*/
|
|
2342
|
+
, 6];
|
|
2343
|
+
this.reInit(distinctId);
|
|
2344
|
+
|
|
2345
|
+
if (groups != undefined) {
|
|
2346
|
+
this._sharedClient.groups(groups);
|
|
2347
|
+
}
|
|
2348
|
+
|
|
2349
|
+
if (personProperties) {
|
|
2350
|
+
this._sharedClient.personProperties(personProperties);
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
if (groupProperties) {
|
|
2354
|
+
this._sharedClient.groupProperties(groupProperties);
|
|
2355
|
+
}
|
|
2356
|
+
|
|
2357
|
+
return [4
|
|
2358
|
+
/*yield*/
|
|
2359
|
+
, this._sharedClient.reloadFeatureFlagsAsync(false)];
|
|
2360
|
+
|
|
2361
|
+
case 5:
|
|
2362
|
+
_d.sent();
|
|
2363
|
+
|
|
2364
|
+
response = this._sharedClient.getFeatureFlagPayload(key);
|
|
2365
|
+
_d.label = 6;
|
|
2366
|
+
|
|
2367
|
+
case 6:
|
|
2368
|
+
try {
|
|
2369
|
+
return [2
|
|
2370
|
+
/*return*/
|
|
2371
|
+
, JSON.parse(response)];
|
|
2372
|
+
} catch (_e) {
|
|
2373
|
+
return [2
|
|
2374
|
+
/*return*/
|
|
2375
|
+
, response];
|
|
2376
|
+
}
|
|
2377
|
+
|
|
2378
|
+
return [2
|
|
2379
|
+
/*return*/
|
|
2380
|
+
];
|
|
2381
|
+
}
|
|
2382
|
+
});
|
|
2383
|
+
});
|
|
2384
|
+
};
|
|
2385
|
+
|
|
2146
2386
|
PostHog.prototype.isFeatureEnabled = function (key, distinctId, options) {
|
|
2147
2387
|
return __awaiter(this, void 0, void 0, function () {
|
|
2148
2388
|
var feat;
|
|
@@ -2171,10 +2411,30 @@ function () {
|
|
|
2171
2411
|
};
|
|
2172
2412
|
|
|
2173
2413
|
PostHog.prototype.getAllFlags = function (distinctId, options) {
|
|
2414
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
2415
|
+
var response;
|
|
2416
|
+
return __generator(this, function (_a) {
|
|
2417
|
+
switch (_a.label) {
|
|
2418
|
+
case 0:
|
|
2419
|
+
return [4
|
|
2420
|
+
/*yield*/
|
|
2421
|
+
, this.getAllFlagsAndPayloads(distinctId, options)];
|
|
2422
|
+
|
|
2423
|
+
case 1:
|
|
2424
|
+
response = _a.sent();
|
|
2425
|
+
return [2
|
|
2426
|
+
/*return*/
|
|
2427
|
+
, response.featureFlags];
|
|
2428
|
+
}
|
|
2429
|
+
});
|
|
2430
|
+
});
|
|
2431
|
+
};
|
|
2432
|
+
|
|
2433
|
+
PostHog.prototype.getAllFlagsAndPayloads = function (distinctId, options) {
|
|
2174
2434
|
var _a;
|
|
2175
2435
|
|
|
2176
2436
|
return __awaiter(this, void 0, void 0, function () {
|
|
2177
|
-
var _b, groups, personProperties, groupProperties, onlyEvaluateLocally, localEvaluationResult,
|
|
2437
|
+
var _b, groups, personProperties, groupProperties, onlyEvaluateLocally, localEvaluationResult, featureFlags, featureFlagPayloads, fallbackToDecide, remoteEvaluationResult;
|
|
2178
2438
|
|
|
2179
2439
|
return __generator(this, function (_c) {
|
|
2180
2440
|
switch (_c.label) {
|
|
@@ -2188,15 +2448,17 @@ function () {
|
|
|
2188
2448
|
|
|
2189
2449
|
return [4
|
|
2190
2450
|
/*yield*/
|
|
2191
|
-
, (_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.
|
|
2451
|
+
, (_a = this.featureFlagsPoller) === null || _a === void 0 ? void 0 : _a.getAllFlagsAndPayloads(distinctId, groups, personProperties, groupProperties)];
|
|
2192
2452
|
|
|
2193
2453
|
case 1:
|
|
2194
2454
|
localEvaluationResult = _c.sent();
|
|
2195
|
-
|
|
2455
|
+
featureFlags = {};
|
|
2456
|
+
featureFlagPayloads = {};
|
|
2196
2457
|
fallbackToDecide = true;
|
|
2197
2458
|
|
|
2198
2459
|
if (localEvaluationResult) {
|
|
2199
|
-
|
|
2460
|
+
featureFlags = localEvaluationResult.response;
|
|
2461
|
+
featureFlagPayloads = localEvaluationResult.payloads;
|
|
2200
2462
|
fallbackToDecide = localEvaluationResult.fallbackToDecide;
|
|
2201
2463
|
}
|
|
2202
2464
|
|
|
@@ -2224,15 +2486,18 @@ function () {
|
|
|
2224
2486
|
case 2:
|
|
2225
2487
|
_c.sent();
|
|
2226
2488
|
|
|
2227
|
-
remoteEvaluationResult = this._sharedClient.
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2489
|
+
remoteEvaluationResult = this._sharedClient.getFeatureFlagsAndPayloads();
|
|
2490
|
+
featureFlags = __assign(__assign({}, featureFlags), remoteEvaluationResult.flags || {});
|
|
2491
|
+
featureFlagPayloads = __assign(__assign({}, featureFlagPayloads), remoteEvaluationResult.payloads || {});
|
|
2492
|
+
_c.label = 3;
|
|
2231
2493
|
|
|
2232
2494
|
case 3:
|
|
2233
2495
|
return [2
|
|
2234
2496
|
/*return*/
|
|
2235
|
-
,
|
|
2497
|
+
, {
|
|
2498
|
+
featureFlags: featureFlags,
|
|
2499
|
+
featureFlagPayloads: featureFlagPayloads
|
|
2500
|
+
}];
|
|
2236
2501
|
}
|
|
2237
2502
|
});
|
|
2238
2503
|
});
|
|
@@ -2242,6 +2507,7 @@ function () {
|
|
|
2242
2507
|
var groupType = _a.groupType,
|
|
2243
2508
|
groupKey = _a.groupKey,
|
|
2244
2509
|
properties = _a.properties;
|
|
2510
|
+
this.reInit("$".concat(groupType, "_").concat(groupKey));
|
|
2245
2511
|
|
|
2246
2512
|
this._sharedClient.groupIdentify(groupType, groupKey, properties);
|
|
2247
2513
|
};
|