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/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ /// <reference types="node" />
1
2
  declare type PosthogCoreOptions = {
2
3
  host?: string;
3
4
  flushAt?: number;
@@ -9,6 +10,21 @@ declare type PosthogCoreOptions = {
9
10
  fetchRetryDelay?: number;
10
11
  sessionExpirationTimeSeconds?: number;
11
12
  captureMode?: 'json' | 'form';
13
+ };
14
+ declare type PostHogFetchOptions = {
15
+ method: 'GET' | 'POST' | 'PUT' | 'PATCH';
16
+ mode?: 'no-cors';
17
+ credentials?: 'omit';
18
+ headers: {
19
+ [key: string]: string;
20
+ };
21
+ body?: string;
22
+ signal?: AbortSignal;
23
+ };
24
+ declare type PostHogFetchResponse = {
25
+ status: number;
26
+ text: () => Promise<string>;
27
+ json: () => Promise<any>;
12
28
  };
13
29
 
14
30
  interface IdentifyMessageV1 {
@@ -134,6 +150,7 @@ declare type PostHogOptions = PosthogCoreOptions & {
134
150
  featureFlagsPollingInterval?: number;
135
151
  requestTimeout?: number;
136
152
  maxCacheSize?: number;
153
+ fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
137
154
  };
138
155
  declare class PostHog implements PostHogNodeV1 {
139
156
  private _sharedClient;
package/lib/index.esm.js CHANGED
@@ -1,5 +1,5 @@
1
- import undici, { request } from 'undici';
2
1
  import { createHash } from 'crypto';
2
+ import axios from 'axios';
3
3
 
4
4
  /******************************************************************************
5
5
  Copyright (c) Microsoft Corporation.
@@ -43,6 +43,18 @@ var __assign = function () {
43
43
  };
44
44
  return __assign.apply(this, arguments);
45
45
  };
46
+ function __rest(s, e) {
47
+ var t = {};
48
+ for (var p in s)
49
+ if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
50
+ t[p] = s[p];
51
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
52
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
53
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
54
+ t[p[i]] = s[p[i]];
55
+ }
56
+ return t;
57
+ }
46
58
  function __awaiter(thisArg, _arguments, P, generator) {
47
59
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
60
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -143,7 +155,7 @@ function __spreadArray(to, from, pack) {
143
155
  return to.concat(ar || Array.prototype.slice.call(from));
144
156
  }
145
157
 
146
- var version = "2.0.2";
158
+ var version = "2.1.0";
147
159
 
148
160
  var PostHogPersistedProperty;
149
161
  (function (PostHogPersistedProperty) {
@@ -1222,6 +1234,42 @@ var PostHogMemoryStorage = /** @class */ (function () {
1222
1234
  return PostHogMemoryStorage;
1223
1235
  }());
1224
1236
 
1237
+ // So that alternative implementations can be used if desired
1238
+
1239
+ var fetch = function (url, options) {
1240
+ return __awaiter(void 0, void 0, void 0, function () {
1241
+ var res;
1242
+ return __generator(this, function (_a) {
1243
+ switch (_a.label) {
1244
+ case 0:
1245
+ return [4
1246
+ /*yield*/
1247
+ , axios.request({
1248
+ url: url,
1249
+ headers: options.headers,
1250
+ method: options.method.toLowerCase(),
1251
+ data: options.body,
1252
+ signal: options.signal
1253
+ })];
1254
+
1255
+ case 1:
1256
+ res = _a.sent();
1257
+ return [2
1258
+ /*return*/
1259
+ , {
1260
+ status: res.status,
1261
+ text: function () {
1262
+ return res.data;
1263
+ },
1264
+ json: function () {
1265
+ return res.data;
1266
+ }
1267
+ }];
1268
+ }
1269
+ });
1270
+ });
1271
+ };
1272
+
1225
1273
  var LONG_SCALE = 0xfffffffffffffff;
1226
1274
 
1227
1275
  var ClientError =
@@ -1270,7 +1318,9 @@ function () {
1270
1318
  personalApiKey = _a.personalApiKey,
1271
1319
  projectApiKey = _a.projectApiKey,
1272
1320
  timeout = _a.timeout,
1273
- host = _a.host;
1321
+ host = _a.host,
1322
+ options = __rest(_a, ["pollingInterval", "personalApiKey", "projectApiKey", "timeout", "host"]);
1323
+
1274
1324
  this.pollingInterval = pollingInterval;
1275
1325
  this.personalApiKey = personalApiKey;
1276
1326
  this.featureFlags = [];
@@ -1279,7 +1329,9 @@ function () {
1279
1329
  this.timeout = timeout;
1280
1330
  this.projectApiKey = projectApiKey;
1281
1331
  this.host = host;
1282
- this.poller = undefined;
1332
+ this.poller = undefined; // NOTE: as any is required here as the AbortSignal typing is slightly misaligned but works just fine
1333
+
1334
+ this.fetch = options.fetch || fetch;
1283
1335
  void this.loadFeatureFlags();
1284
1336
  }
1285
1337
 
@@ -1591,13 +1643,13 @@ function () {
1591
1643
  case 2:
1592
1644
  res = _a.sent();
1593
1645
 
1594
- if (res && res.statusCode === 401) {
1646
+ if (res && res.status === 401) {
1595
1647
  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");
1596
1648
  }
1597
1649
 
1598
1650
  return [4
1599
1651
  /*yield*/
1600
- , res.body.json()];
1652
+ , res.json()];
1601
1653
 
1602
1654
  case 3:
1603
1655
  responseJson = _a.sent();
@@ -1636,48 +1688,57 @@ function () {
1636
1688
 
1637
1689
  FeatureFlagsPoller.prototype._requestFeatureFlagDefinitions = function () {
1638
1690
  return __awaiter(this, void 0, void 0, function () {
1639
- var url, headers, options, res, err_2;
1691
+ var url, options, abortTimeout, controller_1, err_2;
1640
1692
  return __generator(this, function (_a) {
1641
1693
  switch (_a.label) {
1642
1694
  case 0:
1643
1695
  url = "".concat(this.host, "/api/feature_flag/local_evaluation?token=").concat(this.projectApiKey);
1644
- headers = {
1645
- 'Content-Type': 'application/json',
1646
- Authorization: "Bearer ".concat(this.personalApiKey),
1647
- 'user-agent': "posthog-node/".concat(version)
1648
- };
1649
1696
  options = {
1650
1697
  method: 'GET',
1651
- headers: headers
1698
+ headers: {
1699
+ 'Content-Type': 'application/json',
1700
+ Authorization: "Bearer ".concat(this.personalApiKey),
1701
+ 'user-agent': "posthog-node/".concat(version)
1702
+ }
1652
1703
  };
1704
+ abortTimeout = null;
1653
1705
 
1654
1706
  if (this.timeout && typeof this.timeout === 'number') {
1655
- options.bodyTimeout = this.timeout;
1707
+ controller_1 = new AbortController();
1708
+ abortTimeout = safeSetTimeout(function () {
1709
+ controller_1.abort();
1710
+ }, this.timeout);
1711
+ options.signal = controller_1.signal;
1656
1712
  }
1657
1713
 
1658
1714
  _a.label = 1;
1659
1715
 
1660
1716
  case 1:
1661
- _a.trys.push([1, 3,, 4]);
1717
+ _a.trys.push([1, 3, 4, 5]);
1662
1718
 
1663
1719
  return [4
1664
1720
  /*yield*/
1665
- , request(url, options)];
1721
+ , this.fetch(url, options)];
1666
1722
 
1667
1723
  case 2:
1668
- res = _a.sent();
1669
- return [3
1670
- /*break*/
1671
- , 4];
1724
+ return [2
1725
+ /*return*/
1726
+ , _a.sent()];
1672
1727
 
1673
1728
  case 3:
1674
1729
  err_2 = _a.sent();
1675
1730
  throw new Error("Request failed with error: ".concat(err_2));
1676
1731
 
1677
1732
  case 4:
1733
+ clearTimeout(abortTimeout);
1734
+ return [7
1735
+ /*endfinally*/
1736
+ ];
1737
+
1738
+ case 5:
1678
1739
  return [2
1679
1740
  /*return*/
1680
- , res];
1741
+ ];
1681
1742
  }
1682
1743
  });
1683
1744
  });
@@ -1787,6 +1848,7 @@ function (_super) {
1787
1848
  options.sendFeatureFlagEvent = false; // Let `posthog-node` handle this on its own, since we're dealing with multiple distinctIDs
1788
1849
 
1789
1850
  _this = _super.call(this, apiKey, options) || this;
1851
+ _this.options = options;
1790
1852
  _this._memoryStorage = new PostHogMemoryStorage();
1791
1853
  return _this;
1792
1854
  }
@@ -1805,7 +1867,7 @@ function (_super) {
1805
1867
  };
1806
1868
 
1807
1869
  PostHogClient.prototype.fetch = function (url, options) {
1808
- return undici.fetch(url, options);
1870
+ return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
1809
1871
  };
1810
1872
 
1811
1873
  PostHogClient.prototype.getLibraryId = function () {
@@ -1840,7 +1902,8 @@ function () {
1840
1902
  personalApiKey: options.personalApiKey,
1841
1903
  projectApiKey: apiKey,
1842
1904
  timeout: options.requestTimeout,
1843
- host: this._sharedClient.host
1905
+ host: this._sharedClient.host,
1906
+ fetch: options.fetch
1844
1907
  });
1845
1908
  }
1846
1909