posthog-node 4.0.0-beta.2 → 4.0.0-beta.3

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
@@ -195,6 +195,9 @@ declare abstract class PostHogCoreStateless {
195
195
  */
196
196
  private flushBackground;
197
197
  flush(): Promise<any[]>;
198
+ protected getCustomHeaders(): {
199
+ [key: string]: string;
200
+ };
198
201
  private _flush;
199
202
  private fetchWithRetry;
200
203
  shutdown(shutdownTimeoutMs?: number): Promise<void>;
package/lib/index.esm.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createHash } from 'rusha';
2
2
 
3
- var version = "4.0.0-beta.2";
3
+ var version = "4.0.0-beta.3";
4
4
 
5
5
  var PostHogPersistedProperty;
6
6
  (function (PostHogPersistedProperty) {
@@ -1107,7 +1107,7 @@ class PostHogCoreStateless {
1107
1107
  const url = `${this.host}/decide/?v=3`;
1108
1108
  const fetchOptions = {
1109
1109
  method: 'POST',
1110
- headers: { 'Content-Type': 'application/json' },
1110
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1111
1111
  body: JSON.stringify({
1112
1112
  token: this.apiKey,
1113
1113
  distinct_id: distinctId,
@@ -1251,6 +1251,18 @@ class PostHogCoreStateless {
1251
1251
  }
1252
1252
  return this.flushPromise;
1253
1253
  }
1254
+ getCustomHeaders() {
1255
+ // Don't set the user agent if we're not on a browser. The latest spec allows
1256
+ // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1257
+ // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1258
+ // but browsers such as Chrome and Safari have not caught up.
1259
+ const customUserAgent = this.getCustomUserAgent();
1260
+ const headers = {};
1261
+ if (customUserAgent && customUserAgent !== '') {
1262
+ headers['User-Agent'] = customUserAgent;
1263
+ }
1264
+ return headers;
1265
+ }
1254
1266
  async _flush() {
1255
1267
  this.clearFlushTimer();
1256
1268
  await this._initPromise;
@@ -1269,11 +1281,6 @@ class PostHogCoreStateless {
1269
1281
  batch: messages,
1270
1282
  sent_at: currentISOTime(),
1271
1283
  };
1272
- // Don't set the user agent if we're not on a browser. The latest spec allows
1273
- // the User-Agent header (see https://fetch.spec.whatwg.org/#terminology-headers
1274
- // and https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader),
1275
- // but browsers such as Chrome and Safari have not caught up.
1276
- this.getCustomUserAgent();
1277
1284
  const payload = JSON.stringify(data);
1278
1285
  const url = this.captureMode === 'form'
1279
1286
  ? `${this.host}/e/?ip=1&_=${currentTimestamp()}&v=${this.getLibraryVersion()}`
@@ -1283,12 +1290,12 @@ class PostHogCoreStateless {
1283
1290
  method: 'POST',
1284
1291
  mode: 'no-cors',
1285
1292
  credentials: 'omit',
1286
- headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
1293
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/x-www-form-urlencoded' },
1287
1294
  body: `data=${encodeURIComponent(LZString.compressToBase64(payload))}&compression=lz64`,
1288
1295
  }
1289
1296
  : {
1290
1297
  method: 'POST',
1291
- headers: { 'Content-Type': 'application/json' },
1298
+ headers: { ...this.getCustomHeaders(), 'Content-Type': 'application/json' },
1292
1299
  body: payload,
1293
1300
  };
1294
1301
  try {
@@ -1449,6 +1456,7 @@ class FeatureFlagsPoller {
1449
1456
  projectApiKey,
1450
1457
  timeout,
1451
1458
  host,
1459
+ customHeaders,
1452
1460
  ...options
1453
1461
  }) {
1454
1462
  this.debugMode = false;
@@ -1466,6 +1474,7 @@ class FeatureFlagsPoller {
1466
1474
 
1467
1475
  this.fetch = options.fetch || fetch$1;
1468
1476
  this.onError = options.onError;
1477
+ this.customHeaders = customHeaders;
1469
1478
  void this.loadFeatureFlags();
1470
1479
  }
1471
1480
 
@@ -1768,10 +1777,9 @@ class FeatureFlagsPoller {
1768
1777
  const url = `${this.host}/api/feature_flag/local_evaluation?token=${this.projectApiKey}&send_cohorts`;
1769
1778
  const options = {
1770
1779
  method: 'GET',
1771
- headers: {
1780
+ headers: { ...this.customHeaders,
1772
1781
  'Content-Type': 'application/json',
1773
- Authorization: `Bearer ${this.personalApiKey}`,
1774
- 'user-agent': `posthog-node/${version}`
1782
+ Authorization: `Bearer ${this.personalApiKey}`
1775
1783
  }
1776
1784
  };
1777
1785
  let abortTimeout = null;
@@ -2113,7 +2121,8 @@ class PostHog extends PostHogCoreStateless {
2113
2121
  fetch: options.fetch,
2114
2122
  onError: err => {
2115
2123
  this._events.emit('error', err);
2116
- }
2124
+ },
2125
+ customHeaders: this.getCustomHeaders()
2117
2126
  });
2118
2127
  }
2119
2128
 
@@ -2142,7 +2151,7 @@ class PostHog extends PostHogCoreStateless {
2142
2151
  }
2143
2152
 
2144
2153
  getCustomUserAgent() {
2145
- return `posthog-node/${version}`;
2154
+ return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
2146
2155
  }
2147
2156
 
2148
2157
  enable() {