posthog-js-lite 3.2.0 → 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.d.ts CHANGED
@@ -330,6 +330,7 @@ declare class PostHog extends PostHogCore {
330
330
  private _storageCache;
331
331
  private _storageKey;
332
332
  constructor(apiKey: string, options?: PostHogOptions);
333
+ private getWindow;
333
334
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
334
335
  setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
335
336
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
package/lib/index.esm.js CHANGED
@@ -61,6 +61,9 @@ function safeSetTimeout(fn, timeout) {
61
61
  // We unref if available to prevent Node.js hanging on exit
62
62
  t?.unref && t?.unref();
63
63
  return t;
64
+ }
65
+ function getFetch() {
66
+ return typeof fetch !== 'undefined' ? fetch : typeof global.fetch !== 'undefined' ? global.fetch : undefined;
64
67
  }
65
68
 
66
69
  // Copyright (c) 2013 Pieroxy <pieroxy@pieroxy.net>
@@ -1913,15 +1916,18 @@ class PostHogCore extends PostHogCoreStateless {
1913
1916
  }
1914
1917
  }
1915
1918
 
1916
- var version = "3.2.0";
1919
+ var version = "3.2.1";
1917
1920
 
1918
1921
  function getContext(window) {
1919
1922
  let context = {};
1920
- if (window.navigator) {
1923
+ if (window?.navigator) {
1921
1924
  const userAgent = window.navigator.userAgent;
1925
+ const osValue = os(window);
1922
1926
  context = {
1923
1927
  ...context,
1924
- $os: os(window),
1928
+ ...(osValue !== undefined && {
1929
+ $os: osValue
1930
+ }),
1925
1931
  $browser: browser(userAgent, window.navigator.vendor, !!window.opera),
1926
1932
  $referrer: window.document.referrer,
1927
1933
  $referring_domain: referringDomain(window.document.referrer),
@@ -2023,6 +2029,9 @@ function browserVersion(userAgent, vendor, opera) {
2023
2029
  return parseFloat(matches[matches.length - 2]);
2024
2030
  }
2025
2031
  function os(window) {
2032
+ if (!window?.navigator) {
2033
+ return undefined;
2034
+ }
2026
2035
  const a = window.navigator.userAgent;
2027
2036
  if (/Windows/i.test(a)) {
2028
2037
  if (/Phone/.test(a) || /WPDesktop/.test(a)) {
@@ -2042,7 +2051,7 @@ function os(window) {
2042
2051
  } else if (/CrOS/.test(a)) {
2043
2052
  return 'Chrome OS';
2044
2053
  } else {
2045
- return '';
2054
+ return undefined;
2046
2055
  }
2047
2056
  }
2048
2057
  function device(userAgent) {
@@ -2146,9 +2155,6 @@ const createStorageLike = store => {
2146
2155
  };
2147
2156
  };
2148
2157
  const checkStoreIsSupported = (storage, key = '__mplssupport__') => {
2149
- if (!window) {
2150
- return false;
2151
- }
2152
2158
  try {
2153
2159
  const val = 'xyz';
2154
2160
  storage.setItem(key, val);
@@ -2220,12 +2226,15 @@ class PostHog extends PostHogCore {
2220
2226
  super(apiKey, options);
2221
2227
  // posthog-js stores options in one object on
2222
2228
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`;
2223
- this._storage = getStorage(options?.persistence || 'localStorage', window);
2229
+ this._storage = getStorage(options?.persistence || 'localStorage', this.getWindow());
2224
2230
  this.setupBootstrap(options);
2225
2231
  if (options?.preloadFeatureFlags !== false) {
2226
2232
  this.reloadFeatureFlags();
2227
2233
  }
2228
2234
  }
2235
+ getWindow() {
2236
+ return typeof window !== 'undefined' ? window : undefined;
2237
+ }
2229
2238
  getPersistedProperty(key) {
2230
2239
  if (!this._storageCache) {
2231
2240
  this._storageCache = JSON.parse(this._storage.getItem(this._storageKey) || '{}') || {};
@@ -2244,7 +2253,12 @@ class PostHog extends PostHogCore {
2244
2253
  this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache));
2245
2254
  }
2246
2255
  fetch(url, options) {
2247
- return window.fetch(url, options);
2256
+ const fetchFn = getFetch();
2257
+ if (!fetchFn) {
2258
+ // error will be handled by the caller (fetchWithRetry)
2259
+ return Promise.reject(new Error('Fetch API is not available in this environment.'));
2260
+ }
2261
+ return fetchFn(url, options);
2248
2262
  }
2249
2263
  getLibraryId() {
2250
2264
  return 'posthog-js-lite';
@@ -2258,7 +2272,7 @@ class PostHog extends PostHogCore {
2258
2272
  getCommonEventProperties() {
2259
2273
  return {
2260
2274
  ...super.getCommonEventProperties(),
2261
- ...getContext(window)
2275
+ ...getContext(this.getWindow())
2262
2276
  };
2263
2277
  }
2264
2278
  }