posthog-node 3.1.3 → 3.2.1

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/lib/index.esm.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { createHash } from 'rusha';
2
- import axios from 'axios';
3
2
 
4
3
  /******************************************************************************
5
4
  Copyright (c) Microsoft Corporation.
@@ -155,7 +154,7 @@ function __spreadArray(to, from, pack) {
155
154
  return to.concat(ar || Array.prototype.slice.call(from));
156
155
  }
157
156
 
158
- var version = "3.1.3";
157
+ var version = "3.2.1";
159
158
 
160
159
  var PostHogPersistedProperty;
161
160
  (function (PostHogPersistedProperty) {
@@ -1001,7 +1000,7 @@ var PostHogCoreStateless = /** @class */ (function () {
1001
1000
  var _this = this;
1002
1001
  var _a;
1003
1002
  if (this.optedOut) {
1004
- this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.enable()");
1003
+ this._events.emit(type, "Library is disabled. Not sending event. To re-enable, call posthog.optIn()");
1005
1004
  return;
1006
1005
  }
1007
1006
  var message = __assign(__assign({}, _message), { type: type, library: this.getLibraryId(), library_version: this.getLibraryVersion(), timestamp: (options === null || options === void 0 ? void 0 : options.timestamp) ? options === null || options === void 0 ? void 0 : options.timestamp : currentISOTime() });
@@ -1372,13 +1371,20 @@ var PostHogCoreStateless = /** @class */ (function () {
1372
1371
  /***
1373
1372
  * PROPERTIES
1374
1373
  ***/
1375
- PostHogCore.prototype.personProperties = function (properties) {
1374
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1376
1375
  // Get persisted person properties
1377
1376
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1378
1377
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1379
1378
  return this;
1380
1379
  };
1381
- PostHogCore.prototype.groupProperties = function (properties) {
1380
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1381
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1382
+ };
1383
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1384
+ PostHogCore.prototype.personProperties = function (properties) {
1385
+ return this.setPersonPropertiesForFlags(properties);
1386
+ };
1387
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1382
1388
  // Get persisted group properties
1383
1389
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1384
1390
  if (Object.keys(existingProperties).length !== 0) {
@@ -1390,6 +1396,13 @@ var PostHogCoreStateless = /** @class */ (function () {
1390
1396
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1391
1397
  return this;
1392
1398
  };
1399
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1400
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1401
+ };
1402
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1403
+ PostHogCore.prototype.groupProperties = function (properties) {
1404
+ return this.setGroupPropertiesForFlags(properties);
1405
+ };
1393
1406
  /***
1394
1407
  *** FEATURE FLAGS
1395
1408
  ***/
@@ -1594,57 +1607,76 @@ var PostHogMemoryStorage = /** @class */ (function () {
1594
1607
  return PostHogMemoryStorage;
1595
1608
  }());
1596
1609
 
1597
- // So that alternative implementations can be used if desired
1598
-
1599
- var fetch = function (url, options) {
1600
- return __awaiter(void 0, void 0, void 0, function () {
1601
- var res;
1602
- return __generator(this, function (_a) {
1603
- switch (_a.label) {
1604
- case 0:
1605
- return [4
1606
- /*yield*/
1607
- , axios.request({
1608
- url: url,
1609
- headers: options.headers,
1610
- method: options.method.toLowerCase(),
1611
- data: options.body,
1612
- signal: options.signal,
1613
- // fetch only throws on network errors, not on HTTP errors
1614
- validateStatus: function () {
1615
- return true;
1616
- }
1617
- })];
1618
-
1619
- case 1:
1620
- res = _a.sent();
1621
- return [2
1622
- /*return*/
1623
- , {
1624
- status: res.status,
1625
- text: function () {
1626
- return __awaiter(void 0, void 0, void 0, function () {
1627
- return __generator(this, function (_a) {
1628
- return [2
1629
- /*return*/
1630
- , res.data];
1610
+ /**
1611
+ * Fetch wrapper
1612
+ *
1613
+ * We want to polyfill fetch when not available with axios but use it when it is.
1614
+ * NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
1615
+ * This is currently solved by using the global fetch if available instead.
1616
+ * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
1617
+ */
1618
+
1619
+ var _fetch = // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
1620
+ // @ts-ignore
1621
+ typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
1622
+
1623
+ if (!_fetch) {
1624
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
1625
+ var axios_1 = require('axios');
1626
+
1627
+ _fetch = function (url, options) {
1628
+ return __awaiter(void 0, void 0, void 0, function () {
1629
+ var res;
1630
+ return __generator(this, function (_a) {
1631
+ switch (_a.label) {
1632
+ case 0:
1633
+ return [4
1634
+ /*yield*/
1635
+ , axios_1.request({
1636
+ url: url,
1637
+ headers: options.headers,
1638
+ method: options.method.toLowerCase(),
1639
+ data: options.body,
1640
+ signal: options.signal,
1641
+ // fetch only throws on network errors, not on HTTP errors
1642
+ validateStatus: function () {
1643
+ return true;
1644
+ }
1645
+ })];
1646
+
1647
+ case 1:
1648
+ res = _a.sent();
1649
+ return [2
1650
+ /*return*/
1651
+ , {
1652
+ status: res.status,
1653
+ text: function () {
1654
+ return __awaiter(void 0, void 0, void 0, function () {
1655
+ return __generator(this, function (_a) {
1656
+ return [2
1657
+ /*return*/
1658
+ , res.data];
1659
+ });
1631
1660
  });
1632
- });
1633
- },
1634
- json: function () {
1635
- return __awaiter(void 0, void 0, void 0, function () {
1636
- return __generator(this, function (_a) {
1637
- return [2
1638
- /*return*/
1639
- , res.data];
1661
+ },
1662
+ json: function () {
1663
+ return __awaiter(void 0, void 0, void 0, function () {
1664
+ return __generator(this, function (_a) {
1665
+ return [2
1666
+ /*return*/
1667
+ , res.data];
1668
+ });
1640
1669
  });
1641
- });
1642
- }
1643
- }];
1644
- }
1670
+ }
1671
+ }];
1672
+ }
1673
+ });
1645
1674
  });
1646
- });
1647
- };
1675
+ };
1676
+ } // NOTE: We have to export this as default, even though we prefer named exports as we are relying on detecting "fetch" in the global scope
1677
+
1678
+
1679
+ var fetch$1 = _fetch;
1648
1680
 
1649
1681
  var LONG_SCALE = 0xfffffffffffffff;
1650
1682
 
@@ -1710,7 +1742,8 @@ function () {
1710
1742
  this.host = host;
1711
1743
  this.poller = undefined; // NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
1712
1744
 
1713
- this.fetch = options.fetch || fetch;
1745
+ this.fetch = options.fetch || fetch$1;
1746
+ this.onError = options.onError;
1714
1747
  void this.loadFeatureFlags();
1715
1748
  }
1716
1749
 
@@ -1870,10 +1903,13 @@ function () {
1870
1903
  this.featureFlags.map(function (flag) {
1871
1904
  return __awaiter(_this, void 0, void 0, function () {
1872
1905
  var matchValue, matchPayload, e_1;
1873
- return __generator(this, function (_a) {
1874
- switch (_a.label) {
1906
+
1907
+ var _a;
1908
+
1909
+ return __generator(this, function (_b) {
1910
+ switch (_b.label) {
1875
1911
  case 0:
1876
- _a.trys.push([0, 2,, 3]);
1912
+ _b.trys.push([0, 2,, 3]);
1877
1913
 
1878
1914
  matchValue = this.computeFlagLocally(flag, distinctId, groups, personProperties, groupProperties);
1879
1915
  response[flag.key] = matchValue;
@@ -1882,7 +1918,7 @@ function () {
1882
1918
  , this.computeFeatureFlagPayloadLocally(flag.key, matchValue)];
1883
1919
 
1884
1920
  case 1:
1885
- matchPayload = _a.sent();
1921
+ matchPayload = _b.sent();
1886
1922
 
1887
1923
  if (matchPayload) {
1888
1924
  payloads[flag.key] = matchPayload;
@@ -1893,10 +1929,10 @@ function () {
1893
1929
  , 3];
1894
1930
 
1895
1931
  case 2:
1896
- e_1 = _a.sent();
1932
+ e_1 = _b.sent();
1897
1933
 
1898
1934
  if (e_1 instanceof InconclusiveMatchError) ; else if (e_1 instanceof Error) {
1899
- console.error("Error computing flag locally: ".concat(flag.key, ": ").concat(e_1));
1935
+ (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, new Error("Error computing flag locally: ".concat(flag.key, ": ").concat(e_1)));
1900
1936
  }
1901
1937
 
1902
1938
  fallbackToDecide = true;
@@ -2134,13 +2170,15 @@ function () {
2134
2170
  };
2135
2171
 
2136
2172
  FeatureFlagsPoller.prototype._loadFeatureFlags = function () {
2173
+ var _a;
2174
+
2137
2175
  return __awaiter(this, void 0, void 0, function () {
2138
2176
  var res, responseJson, err_1;
2139
2177
 
2140
2178
  var _this = this;
2141
2179
 
2142
- return __generator(this, function (_a) {
2143
- switch (_a.label) {
2180
+ return __generator(this, function (_b) {
2181
+ switch (_b.label) {
2144
2182
  case 0:
2145
2183
  if (this.poller) {
2146
2184
  clearTimeout(this.poller);
@@ -2150,17 +2188,17 @@ function () {
2150
2188
  this.poller = setTimeout(function () {
2151
2189
  return _this._loadFeatureFlags();
2152
2190
  }, this.pollingInterval);
2153
- _a.label = 1;
2191
+ _b.label = 1;
2154
2192
 
2155
2193
  case 1:
2156
- _a.trys.push([1, 4,, 5]);
2194
+ _b.trys.push([1, 4,, 5]);
2157
2195
 
2158
2196
  return [4
2159
2197
  /*yield*/
2160
2198
  , this._requestFeatureFlagDefinitions()];
2161
2199
 
2162
2200
  case 2:
2163
- res = _a.sent();
2201
+ res = _b.sent();
2164
2202
 
2165
2203
  if (res && res.status === 401) {
2166
2204
  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");
@@ -2179,7 +2217,7 @@ function () {
2179
2217
  , res.json()];
2180
2218
 
2181
2219
  case 3:
2182
- responseJson = _a.sent();
2220
+ responseJson = _b.sent();
2183
2221
 
2184
2222
  if (!('flags' in responseJson)) {
2185
2223
  console.error("Invalid response when getting feature flags: ".concat(JSON.stringify(responseJson)));
@@ -2197,11 +2235,11 @@ function () {
2197
2235
  , 5];
2198
2236
 
2199
2237
  case 4:
2200
- err_1 = _a.sent(); // if an error that is not an instance of ClientError is thrown
2238
+ err_1 = _b.sent(); // if an error that is not an instance of ClientError is thrown
2201
2239
  // we silently ignore the error when reloading feature flags
2202
2240
 
2203
2241
  if (err_1 instanceof ClientError) {
2204
- throw err_1;
2242
+ (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, err_1);
2205
2243
  }
2206
2244
 
2207
2245
  return [3
@@ -2526,7 +2564,10 @@ function (_super) {
2526
2564
  projectApiKey: apiKey,
2527
2565
  timeout: (_a = options.requestTimeout) !== null && _a !== void 0 ? _a : 10000,
2528
2566
  host: _this.host,
2529
- fetch: options.fetch
2567
+ fetch: options.fetch,
2568
+ onError: function (err) {
2569
+ _this._events.emit('error', err);
2570
+ }
2530
2571
  });
2531
2572
  }
2532
2573
 
@@ -2544,7 +2585,7 @@ function (_super) {
2544
2585
  };
2545
2586
 
2546
2587
  PostHog.prototype.fetch = function (url, options) {
2547
- return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
2588
+ return this.options.fetch ? this.options.fetch(url, options) : fetch$1(url, options);
2548
2589
  };
2549
2590
 
2550
2591
  PostHog.prototype.getLibraryId = function () {