posthog-node 3.1.2 → 3.2.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 CHANGED
@@ -1,3 +1,12 @@
1
+ # 3.2.0 - 2023-12-05
2
+
3
+ 1. Fixes issues with Axios imports for non-node environments like Cloudflare workers
4
+ 2. Uses the globally defined `fetch` if available, otherwise imports and uses axios as a polyfill
5
+
6
+ # 3.1.3 - 2023-10-27
7
+
8
+ 1. Updates axios dependency
9
+
1
10
  # 3.1.2 - 2023-08-17
2
11
 
3
12
  1. Returns the current flag property with $feature_flag_called events, to make it easier to use in experiments
package/lib/index.cjs.js CHANGED
@@ -3,11 +3,6 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var rusha = require('rusha');
6
- var axios = require('axios');
7
-
8
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
9
-
10
- var axios__default = /*#__PURE__*/_interopDefaultLegacy(axios);
11
6
 
12
7
  /******************************************************************************
13
8
  Copyright (c) Microsoft Corporation.
@@ -163,7 +158,7 @@ function __spreadArray(to, from, pack) {
163
158
  return to.concat(ar || Array.prototype.slice.call(from));
164
159
  }
165
160
 
166
- var version = "3.1.2";
161
+ var version = "3.2.0";
167
162
 
168
163
  var PostHogPersistedProperty;
169
164
  (function (PostHogPersistedProperty) {
@@ -179,6 +174,8 @@ var PostHogPersistedProperty;
179
174
  PostHogPersistedProperty["SessionLastTimestamp"] = "session_timestamp";
180
175
  PostHogPersistedProperty["PersonProperties"] = "person_properties";
181
176
  PostHogPersistedProperty["GroupProperties"] = "group_properties";
177
+ PostHogPersistedProperty["InstalledAppBuild"] = "installed_app_build";
178
+ PostHogPersistedProperty["InstalledAppVersion"] = "installed_app_version";
182
179
  })(PostHogPersistedProperty || (PostHogPersistedProperty = {}));
183
180
 
184
181
  function assert(truthyValue, message) {
@@ -737,7 +734,10 @@ var PostHogFetchHttpError = /** @class */ (function (_super) {
737
734
  var PostHogFetchNetworkError = /** @class */ (function (_super) {
738
735
  __extends(PostHogFetchNetworkError, _super);
739
736
  function PostHogFetchNetworkError(error) {
740
- var _this = _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
737
+ var _this =
738
+ // TRICKY: "cause" is a newer property but is just ignored otherwise. Cast to any to ignore the type issue.
739
+ // @ts-ignore
740
+ _super.call(this, 'Network error while fetching PostHog', error instanceof Error ? { cause: error } : {}) || this;
741
741
  _this.error = error;
742
742
  _this.name = 'PostHogFetchNetworkError';
743
743
  return _this;
@@ -1375,13 +1375,20 @@ var PostHogCoreStateless = /** @class */ (function () {
1375
1375
  /***
1376
1376
  * PROPERTIES
1377
1377
  ***/
1378
- PostHogCore.prototype.personProperties = function (properties) {
1378
+ PostHogCore.prototype.setPersonPropertiesForFlags = function (properties) {
1379
1379
  // Get persisted person properties
1380
1380
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.PersonProperties) || {};
1381
1381
  this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, __assign(__assign({}, existingProperties), properties));
1382
1382
  return this;
1383
1383
  };
1384
- PostHogCore.prototype.groupProperties = function (properties) {
1384
+ PostHogCore.prototype.resetPersonPropertiesForFlags = function () {
1385
+ this.setPersistedProperty(PostHogPersistedProperty.PersonProperties, {});
1386
+ };
1387
+ /** @deprecated - Renamed to setPersonPropertiesForFlags */
1388
+ PostHogCore.prototype.personProperties = function (properties) {
1389
+ return this.setPersonPropertiesForFlags(properties);
1390
+ };
1391
+ PostHogCore.prototype.setGroupPropertiesForFlags = function (properties) {
1385
1392
  // Get persisted group properties
1386
1393
  var existingProperties = this.getPersistedProperty(PostHogPersistedProperty.GroupProperties) || {};
1387
1394
  if (Object.keys(existingProperties).length !== 0) {
@@ -1393,6 +1400,13 @@ var PostHogCoreStateless = /** @class */ (function () {
1393
1400
  this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, __assign(__assign({}, existingProperties), properties));
1394
1401
  return this;
1395
1402
  };
1403
+ PostHogCore.prototype.resetGroupPropertiesForFlags = function () {
1404
+ this.setPersistedProperty(PostHogPersistedProperty.GroupProperties, {});
1405
+ };
1406
+ /** @deprecated - Renamed to setGroupPropertiesForFlags */
1407
+ PostHogCore.prototype.groupProperties = function (properties) {
1408
+ return this.setGroupPropertiesForFlags(properties);
1409
+ };
1396
1410
  /***
1397
1411
  *** FEATURE FLAGS
1398
1412
  ***/
@@ -1597,57 +1611,76 @@ var PostHogMemoryStorage = /** @class */ (function () {
1597
1611
  return PostHogMemoryStorage;
1598
1612
  }());
1599
1613
 
1600
- // So that alternative implementations can be used if desired
1601
-
1602
- var fetch = function (url, options) {
1603
- return __awaiter(void 0, void 0, void 0, function () {
1604
- var res;
1605
- return __generator(this, function (_a) {
1606
- switch (_a.label) {
1607
- case 0:
1608
- return [4
1609
- /*yield*/
1610
- , axios__default["default"].request({
1611
- url: url,
1612
- headers: options.headers,
1613
- method: options.method.toLowerCase(),
1614
- data: options.body,
1615
- signal: options.signal,
1616
- // fetch only throws on network errors, not on HTTP errors
1617
- validateStatus: function () {
1618
- return true;
1619
- }
1620
- })];
1621
-
1622
- case 1:
1623
- res = _a.sent();
1624
- return [2
1625
- /*return*/
1626
- , {
1627
- status: res.status,
1628
- text: function () {
1629
- return __awaiter(void 0, void 0, void 0, function () {
1630
- return __generator(this, function (_a) {
1631
- return [2
1632
- /*return*/
1633
- , res.data];
1614
+ /**
1615
+ * Fetch wrapper
1616
+ *
1617
+ * We want to polyfill fetch when not available with axios but use it when it is.
1618
+ * NOTE: The current version of Axios has an issue when in non-node environments like Clouflare Workers.
1619
+ * This is currently solved by using the global fetch if available instead.
1620
+ * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
1621
+ */
1622
+
1623
+ var _fetch = // eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error
1624
+ // @ts-ignore
1625
+ typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
1626
+
1627
+ if (!_fetch) {
1628
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
1629
+ var axios_1 = require('axios');
1630
+
1631
+ _fetch = function (url, options) {
1632
+ return __awaiter(void 0, void 0, void 0, function () {
1633
+ var res;
1634
+ return __generator(this, function (_a) {
1635
+ switch (_a.label) {
1636
+ case 0:
1637
+ return [4
1638
+ /*yield*/
1639
+ , axios_1.request({
1640
+ url: url,
1641
+ headers: options.headers,
1642
+ method: options.method.toLowerCase(),
1643
+ data: options.body,
1644
+ signal: options.signal,
1645
+ // fetch only throws on network errors, not on HTTP errors
1646
+ validateStatus: function () {
1647
+ return true;
1648
+ }
1649
+ })];
1650
+
1651
+ case 1:
1652
+ res = _a.sent();
1653
+ return [2
1654
+ /*return*/
1655
+ , {
1656
+ status: res.status,
1657
+ text: function () {
1658
+ return __awaiter(void 0, void 0, void 0, function () {
1659
+ return __generator(this, function (_a) {
1660
+ return [2
1661
+ /*return*/
1662
+ , res.data];
1663
+ });
1634
1664
  });
1635
- });
1636
- },
1637
- json: function () {
1638
- return __awaiter(void 0, void 0, void 0, function () {
1639
- return __generator(this, function (_a) {
1640
- return [2
1641
- /*return*/
1642
- , res.data];
1665
+ },
1666
+ json: function () {
1667
+ return __awaiter(void 0, void 0, void 0, function () {
1668
+ return __generator(this, function (_a) {
1669
+ return [2
1670
+ /*return*/
1671
+ , res.data];
1672
+ });
1643
1673
  });
1644
- });
1645
- }
1646
- }];
1647
- }
1674
+ }
1675
+ }];
1676
+ }
1677
+ });
1648
1678
  });
1649
- });
1650
- };
1679
+ };
1680
+ } // 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
1681
+
1682
+
1683
+ var fetch$1 = _fetch;
1651
1684
 
1652
1685
  var LONG_SCALE = 0xfffffffffffffff;
1653
1686
 
@@ -1713,7 +1746,7 @@ function () {
1713
1746
  this.host = host;
1714
1747
  this.poller = undefined; // NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
1715
1748
 
1716
- this.fetch = options.fetch || fetch;
1749
+ this.fetch = options.fetch || fetch$1;
1717
1750
  void this.loadFeatureFlags();
1718
1751
  }
1719
1752
 
@@ -2222,7 +2255,7 @@ function () {
2222
2255
 
2223
2256
  FeatureFlagsPoller.prototype._requestFeatureFlagDefinitions = function () {
2224
2257
  return __awaiter(this, void 0, void 0, function () {
2225
- var url, options, abortTimeout, controller_1, err_2;
2258
+ var url, options, abortTimeout, controller_1;
2226
2259
  return __generator(this, function (_a) {
2227
2260
  switch (_a.label) {
2228
2261
  case 0:
@@ -2248,7 +2281,7 @@ function () {
2248
2281
  _a.label = 1;
2249
2282
 
2250
2283
  case 1:
2251
- _a.trys.push([1, 3, 4, 5]);
2284
+ _a.trys.push([1,, 3, 4]);
2252
2285
 
2253
2286
  return [4
2254
2287
  /*yield*/
@@ -2260,16 +2293,12 @@ function () {
2260
2293
  , _a.sent()];
2261
2294
 
2262
2295
  case 3:
2263
- err_2 = _a.sent();
2264
- throw err_2;
2265
-
2266
- case 4:
2267
2296
  clearTimeout(abortTimeout);
2268
2297
  return [7
2269
2298
  /*endfinally*/
2270
2299
  ];
2271
2300
 
2272
- case 5:
2301
+ case 4:
2273
2302
  return [2
2274
2303
  /*return*/
2275
2304
  ];
@@ -2349,14 +2378,16 @@ function matchProperty(property, propertyValues) {
2349
2378
 
2350
2379
  case 'is_date_after':
2351
2380
  case 'is_date_before':
2352
- var parsedDate = convertToDateTime(value);
2353
- var overrideDate = convertToDateTime(overrideValue);
2381
+ {
2382
+ var parsedDate = convertToDateTime(value);
2383
+ var overrideDate = convertToDateTime(overrideValue);
2354
2384
 
2355
- if (operator === 'is_date_before') {
2356
- return overrideDate < parsedDate;
2357
- }
2385
+ if (operator === 'is_date_before') {
2386
+ return overrideDate < parsedDate;
2387
+ }
2358
2388
 
2359
- return overrideDate > parsedDate;
2389
+ return overrideDate > parsedDate;
2390
+ }
2360
2391
 
2361
2392
  default:
2362
2393
  console.error("Unknown operator: ".concat(operator));
@@ -2549,7 +2580,7 @@ function (_super) {
2549
2580
  };
2550
2581
 
2551
2582
  PostHog.prototype.fetch = function (url, options) {
2552
- return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
2583
+ return this.options.fetch ? this.options.fetch(url, options) : fetch$1(url, options);
2553
2584
  };
2554
2585
 
2555
2586
  PostHog.prototype.getLibraryId = function () {