posthog-node 2.0.2 → 2.1.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 +6 -1
- package/lib/index.cjs.js +87 -24
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +17 -0
- package/lib/index.esm.js +86 -23
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-core/src/types.d.ts +3 -1
- package/lib/posthog-node/src/feature-flags.d.ts +5 -3
- package/lib/posthog-node/src/fetch.d.ts +2 -0
- package/lib/posthog-node/src/posthog-node.d.ts +2 -1
- package/package.json +3 -3
- package/src/feature-flags.ts +35 -19
- package/src/fetch.ts +20 -0
- package/src/posthog-node.ts +6 -3
- package/test/feature-flags.spec.ts +113 -115
- package/test/posthog-node.spec.ts +41 -40
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# 2.1.0 - 2022-09-08
|
|
2
|
+
|
|
3
|
+
1. Swaps `unidici` for `axios` in order to support older versions of Node
|
|
4
|
+
2. The `fetch` implementation can be overridden as an option for those who wish to use an alternative implementation
|
|
5
|
+
3. Fixes the minimum Node version to >=14.17.0
|
|
6
|
+
|
|
1
7
|
# 2.0.2 - 2022-08-23
|
|
2
8
|
|
|
3
9
|
1. Removes references to `cli.js`
|
|
@@ -13,7 +19,6 @@ Breaking changes:
|
|
|
13
19
|
4. The `callback` parameter passed as an optional last argument to most of the methods is no longer supported
|
|
14
20
|
5. The CLI is no longer supported
|
|
15
21
|
|
|
16
|
-
|
|
17
22
|
What's new:
|
|
18
23
|
|
|
19
24
|
1. You can now evaluate feature flags locally (i.e. without sending a request to your PostHog servers) by setting a personal API key, and passing in groups and person properties to `isFeatureEnabled` and `getFeatureFlag` calls.
|
package/lib/index.cjs.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var undici = require('undici');
|
|
6
5
|
var crypto = require('crypto');
|
|
6
|
+
var axios = require('axios');
|
|
7
7
|
|
|
8
8
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
9
9
|
|
|
10
|
-
var
|
|
10
|
+
var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
|
|
11
11
|
|
|
12
12
|
/******************************************************************************
|
|
13
13
|
Copyright (c) Microsoft Corporation.
|
|
@@ -51,6 +51,18 @@ var __assign = function () {
|
|
|
51
51
|
};
|
|
52
52
|
return __assign.apply(this, arguments);
|
|
53
53
|
};
|
|
54
|
+
function __rest(s, e) {
|
|
55
|
+
var t = {};
|
|
56
|
+
for (var p in s)
|
|
57
|
+
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
58
|
+
t[p] = s[p];
|
|
59
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
60
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
61
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
62
|
+
t[p[i]] = s[p[i]];
|
|
63
|
+
}
|
|
64
|
+
return t;
|
|
65
|
+
}
|
|
54
66
|
function __awaiter(thisArg, _arguments, P, generator) {
|
|
55
67
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
56
68
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -151,7 +163,7 @@ function __spreadArray(to, from, pack) {
|
|
|
151
163
|
return to.concat(ar || Array.prototype.slice.call(from));
|
|
152
164
|
}
|
|
153
165
|
|
|
154
|
-
var version = "2.0
|
|
166
|
+
var version = "2.1.0";
|
|
155
167
|
|
|
156
168
|
var PostHogPersistedProperty;
|
|
157
169
|
(function (PostHogPersistedProperty) {
|
|
@@ -1230,6 +1242,42 @@ var PostHogMemoryStorage = /** @class */ (function () {
|
|
|
1230
1242
|
return PostHogMemoryStorage;
|
|
1231
1243
|
}());
|
|
1232
1244
|
|
|
1245
|
+
// So that alternative implementations can be used if desired
|
|
1246
|
+
|
|
1247
|
+
var fetch = function (url, options) {
|
|
1248
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
1249
|
+
var res;
|
|
1250
|
+
return __generator(this, function (_a) {
|
|
1251
|
+
switch (_a.label) {
|
|
1252
|
+
case 0:
|
|
1253
|
+
return [4
|
|
1254
|
+
/*yield*/
|
|
1255
|
+
, axios__default["default"].request({
|
|
1256
|
+
url: url,
|
|
1257
|
+
headers: options.headers,
|
|
1258
|
+
method: options.method.toLowerCase(),
|
|
1259
|
+
data: options.body,
|
|
1260
|
+
signal: options.signal
|
|
1261
|
+
})];
|
|
1262
|
+
|
|
1263
|
+
case 1:
|
|
1264
|
+
res = _a.sent();
|
|
1265
|
+
return [2
|
|
1266
|
+
/*return*/
|
|
1267
|
+
, {
|
|
1268
|
+
status: res.status,
|
|
1269
|
+
text: function () {
|
|
1270
|
+
return res.data;
|
|
1271
|
+
},
|
|
1272
|
+
json: function () {
|
|
1273
|
+
return res.data;
|
|
1274
|
+
}
|
|
1275
|
+
}];
|
|
1276
|
+
}
|
|
1277
|
+
});
|
|
1278
|
+
});
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1233
1281
|
var LONG_SCALE = 0xfffffffffffffff;
|
|
1234
1282
|
|
|
1235
1283
|
var ClientError =
|
|
@@ -1278,7 +1326,9 @@ function () {
|
|
|
1278
1326
|
personalApiKey = _a.personalApiKey,
|
|
1279
1327
|
projectApiKey = _a.projectApiKey,
|
|
1280
1328
|
timeout = _a.timeout,
|
|
1281
|
-
host = _a.host
|
|
1329
|
+
host = _a.host,
|
|
1330
|
+
options = __rest(_a, ["pollingInterval", "personalApiKey", "projectApiKey", "timeout", "host"]);
|
|
1331
|
+
|
|
1282
1332
|
this.pollingInterval = pollingInterval;
|
|
1283
1333
|
this.personalApiKey = personalApiKey;
|
|
1284
1334
|
this.featureFlags = [];
|
|
@@ -1287,7 +1337,9 @@ function () {
|
|
|
1287
1337
|
this.timeout = timeout;
|
|
1288
1338
|
this.projectApiKey = projectApiKey;
|
|
1289
1339
|
this.host = host;
|
|
1290
|
-
this.poller = undefined;
|
|
1340
|
+
this.poller = undefined; // NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
|
|
1341
|
+
|
|
1342
|
+
this.fetch = options.fetch || fetch;
|
|
1291
1343
|
void this.loadFeatureFlags();
|
|
1292
1344
|
}
|
|
1293
1345
|
|
|
@@ -1599,13 +1651,13 @@ function () {
|
|
|
1599
1651
|
case 2:
|
|
1600
1652
|
res = _a.sent();
|
|
1601
1653
|
|
|
1602
|
-
if (res && res.
|
|
1654
|
+
if (res && res.status === 401) {
|
|
1603
1655
|
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");
|
|
1604
1656
|
}
|
|
1605
1657
|
|
|
1606
1658
|
return [4
|
|
1607
1659
|
/*yield*/
|
|
1608
|
-
, res.
|
|
1660
|
+
, res.json()];
|
|
1609
1661
|
|
|
1610
1662
|
case 3:
|
|
1611
1663
|
responseJson = _a.sent();
|
|
@@ -1644,48 +1696,57 @@ function () {
|
|
|
1644
1696
|
|
|
1645
1697
|
FeatureFlagsPoller.prototype._requestFeatureFlagDefinitions = function () {
|
|
1646
1698
|
return __awaiter(this, void 0, void 0, function () {
|
|
1647
|
-
var url,
|
|
1699
|
+
var url, options, abortTimeout, controller_1, err_2;
|
|
1648
1700
|
return __generator(this, function (_a) {
|
|
1649
1701
|
switch (_a.label) {
|
|
1650
1702
|
case 0:
|
|
1651
1703
|
url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey);
|
|
1652
|
-
headers = {
|
|
1653
|
-
'Content-Type': 'application/json',
|
|
1654
|
-
Authorization: "Bearer ".concat(this.personalApiKey),
|
|
1655
|
-
'user-agent': "posthog-node/".concat(version)
|
|
1656
|
-
};
|
|
1657
1704
|
options = {
|
|
1658
1705
|
method: 'GET',
|
|
1659
|
-
headers:
|
|
1706
|
+
headers: {
|
|
1707
|
+
'Content-Type': 'application/json',
|
|
1708
|
+
Authorization: "Bearer ".concat(this.personalApiKey),
|
|
1709
|
+
'user-agent': "posthog-node/".concat(version)
|
|
1710
|
+
}
|
|
1660
1711
|
};
|
|
1712
|
+
abortTimeout = null;
|
|
1661
1713
|
|
|
1662
1714
|
if (this.timeout && typeof this.timeout === 'number') {
|
|
1663
|
-
|
|
1715
|
+
controller_1 = new AbortController();
|
|
1716
|
+
abortTimeout = safeSetTimeout(function () {
|
|
1717
|
+
controller_1.abort();
|
|
1718
|
+
}, this.timeout);
|
|
1719
|
+
options.signal = controller_1.signal;
|
|
1664
1720
|
}
|
|
1665
1721
|
|
|
1666
1722
|
_a.label = 1;
|
|
1667
1723
|
|
|
1668
1724
|
case 1:
|
|
1669
|
-
_a.trys.push([1, 3
|
|
1725
|
+
_a.trys.push([1, 3, 4, 5]);
|
|
1670
1726
|
|
|
1671
1727
|
return [4
|
|
1672
1728
|
/*yield*/
|
|
1673
|
-
,
|
|
1729
|
+
, this.fetch(url, options)];
|
|
1674
1730
|
|
|
1675
1731
|
case 2:
|
|
1676
|
-
|
|
1677
|
-
return
|
|
1678
|
-
|
|
1679
|
-
, 4];
|
|
1732
|
+
return [2
|
|
1733
|
+
/*return*/
|
|
1734
|
+
, _a.sent()];
|
|
1680
1735
|
|
|
1681
1736
|
case 3:
|
|
1682
1737
|
err_2 = _a.sent();
|
|
1683
1738
|
throw new Error("Request failed with error: ".concat(err_2));
|
|
1684
1739
|
|
|
1685
1740
|
case 4:
|
|
1741
|
+
clearTimeout(abortTimeout);
|
|
1742
|
+
return [7
|
|
1743
|
+
/*endfinally*/
|
|
1744
|
+
];
|
|
1745
|
+
|
|
1746
|
+
case 5:
|
|
1686
1747
|
return [2
|
|
1687
1748
|
/*return*/
|
|
1688
|
-
|
|
1749
|
+
];
|
|
1689
1750
|
}
|
|
1690
1751
|
});
|
|
1691
1752
|
});
|
|
@@ -1795,6 +1856,7 @@ function (_super) {
|
|
|
1795
1856
|
options.sendFeatureFlagEvent = false; // Let `posthog-node` handle this on its own, since we're dealing with multiple distinctIDs
|
|
1796
1857
|
|
|
1797
1858
|
_this = _super.call(this, apiKey, options) || this;
|
|
1859
|
+
_this.options = options;
|
|
1798
1860
|
_this._memoryStorage = new PostHogMemoryStorage();
|
|
1799
1861
|
return _this;
|
|
1800
1862
|
}
|
|
@@ -1813,7 +1875,7 @@ function (_super) {
|
|
|
1813
1875
|
};
|
|
1814
1876
|
|
|
1815
1877
|
PostHogClient.prototype.fetch = function (url, options) {
|
|
1816
|
-
return
|
|
1878
|
+
return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
|
|
1817
1879
|
};
|
|
1818
1880
|
|
|
1819
1881
|
PostHogClient.prototype.getLibraryId = function () {
|
|
@@ -1848,7 +1910,8 @@ function () {
|
|
|
1848
1910
|
personalApiKey: options.personalApiKey,
|
|
1849
1911
|
projectApiKey: apiKey,
|
|
1850
1912
|
timeout: options.requestTimeout,
|
|
1851
|
-
host: this._sharedClient.host
|
|
1913
|
+
host: this._sharedClient.host,
|
|
1914
|
+
fetch: options.fetch
|
|
1852
1915
|
});
|
|
1853
1916
|
}
|
|
1854
1917
|
|