posthog-node 5.8.1 → 5.8.2

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.
@@ -778,7 +778,7 @@ function setupExpressErrorHandler(_posthog, app) {
778
778
  });
779
779
  }
780
780
 
781
- var version = "5.8.1";
781
+ var version = "5.8.2";
782
782
 
783
783
  /**
784
784
  * A lazy value that is only computed when needed. Inspired by C#'s Lazy<T> class.
@@ -1679,6 +1679,35 @@ const THIRTY_SECONDS = 30 * 1000;
1679
1679
  const MAX_CACHE_SIZE = 50 * 1000;
1680
1680
  // The actual exported Nodejs API.
1681
1681
  class PostHogBackendClient extends PostHogCoreStateless {
1682
+ /**
1683
+ * Initialize a new PostHog client instance.
1684
+ *
1685
+ * @example
1686
+ * ```ts
1687
+ * // Basic initialization
1688
+ * const client = new PostHogBackendClient(
1689
+ * 'your-api-key',
1690
+ * { host: 'https://app.posthog.com' }
1691
+ * )
1692
+ * ```
1693
+ *
1694
+ * @example
1695
+ * ```ts
1696
+ * // With personal API key
1697
+ * const client = new PostHogBackendClient(
1698
+ * 'your-api-key',
1699
+ * {
1700
+ * host: 'https://app.posthog.com',
1701
+ * personalApiKey: 'your-personal-api-key'
1702
+ * }
1703
+ * )
1704
+ * ```
1705
+ *
1706
+ * {@label Initialization}
1707
+ *
1708
+ * @param apiKey - Your PostHog project API key
1709
+ * @param options - Configuration options for the client
1710
+ */
1682
1711
  constructor(apiKey, options = {}) {
1683
1712
  super(apiKey, options);
1684
1713
  this._memoryStorage = new PostHogMemoryStorage();
@@ -1714,31 +1743,185 @@ class PostHogBackendClient extends PostHogCoreStateless {
1714
1743
  this.distinctIdHasSentFlagCalls = {};
1715
1744
  this.maxCacheSize = options.maxCacheSize || MAX_CACHE_SIZE;
1716
1745
  }
1746
+ /**
1747
+ * Get a persisted property value from memory storage.
1748
+ *
1749
+ * @example
1750
+ * ```ts
1751
+ * // Get user ID
1752
+ * const userId = client.getPersistedProperty('userId')
1753
+ * ```
1754
+ *
1755
+ * @example
1756
+ * ```ts
1757
+ * // Get session ID
1758
+ * const sessionId = client.getPersistedProperty('sessionId')
1759
+ * ```
1760
+ *
1761
+ * {@label Initialization}
1762
+ *
1763
+ * @param key - The property key to retrieve
1764
+ * @returns The stored property value or undefined if not found
1765
+ */
1717
1766
  getPersistedProperty(key) {
1718
1767
  return this._memoryStorage.getProperty(key);
1719
1768
  }
1769
+ /**
1770
+ * Set a persisted property value in memory storage.
1771
+ *
1772
+ * @example
1773
+ * ```ts
1774
+ * // Set user ID
1775
+ * client.setPersistedProperty('userId', 'user_123')
1776
+ * ```
1777
+ *
1778
+ * @example
1779
+ * ```ts
1780
+ * // Set session ID
1781
+ * client.setPersistedProperty('sessionId', 'session_456')
1782
+ * ```
1783
+ *
1784
+ * {@label Initialization}
1785
+ *
1786
+ * @param key - The property key to set
1787
+ * @param value - The value to store (null to remove)
1788
+ */
1720
1789
  setPersistedProperty(key, value) {
1721
1790
  return this._memoryStorage.setProperty(key, value);
1722
1791
  }
1792
+ /**
1793
+ * Make an HTTP request using the configured fetch function or default fetch.
1794
+ *
1795
+ * @example
1796
+ * ```ts
1797
+ * // POST request
1798
+ * const response = await client.fetch('/api/endpoint', {
1799
+ * method: 'POST',
1800
+ * headers: { 'Content-Type': 'application/json' },
1801
+ * body: JSON.stringify(data)
1802
+ * })
1803
+ * ```
1804
+ *
1805
+ * @internal
1806
+ *
1807
+ * {@label Initialization}
1808
+ *
1809
+ * @param url - The URL to fetch
1810
+ * @param options - Fetch options
1811
+ * @returns Promise resolving to the fetch response
1812
+ */
1723
1813
  fetch(url, options) {
1724
1814
  return this.options.fetch ? this.options.fetch(url, options) : fetch(url, options);
1725
1815
  }
1816
+ /**
1817
+ * Get the library version from package.json.
1818
+ *
1819
+ * @example
1820
+ * ```ts
1821
+ * // Get version
1822
+ * const version = client.getLibraryVersion()
1823
+ * console.log(`Using PostHog SDK version: ${version}`)
1824
+ * ```
1825
+ *
1826
+ * {@label Initialization}
1827
+ *
1828
+ * @returns The current library version string
1829
+ */
1726
1830
  getLibraryVersion() {
1727
1831
  return version;
1728
1832
  }
1833
+ /**
1834
+ * Get the custom user agent string for this client.
1835
+ *
1836
+ * @example
1837
+ * ```ts
1838
+ * // Get user agent
1839
+ * const userAgent = client.getCustomUserAgent()
1840
+ * // Returns: "posthog-node/5.7.0"
1841
+ * ```
1842
+ *
1843
+ * {@label Identification}
1844
+ *
1845
+ * @returns The formatted user agent string
1846
+ */
1729
1847
  getCustomUserAgent() {
1730
1848
  return `${this.getLibraryId()}/${this.getLibraryVersion()}`;
1731
1849
  }
1850
+ /**
1851
+ * Enable the PostHog client (opt-in).
1852
+ *
1853
+ * @example
1854
+ * ```ts
1855
+ * // Enable client
1856
+ * await client.enable()
1857
+ * // Client is now enabled and will capture events
1858
+ * ```
1859
+ *
1860
+ * {@label Privacy}
1861
+ *
1862
+ * @returns Promise that resolves when the client is enabled
1863
+ */
1732
1864
  enable() {
1733
1865
  return super.optIn();
1734
1866
  }
1867
+ /**
1868
+ * Disable the PostHog client (opt-out).
1869
+ *
1870
+ * @example
1871
+ * ```ts
1872
+ * // Disable client
1873
+ * await client.disable()
1874
+ * // Client is now disabled and will not capture events
1875
+ * ```
1876
+ *
1877
+ * {@label Privacy}
1878
+ *
1879
+ * @returns Promise that resolves when the client is disabled
1880
+ */
1735
1881
  disable() {
1736
1882
  return super.optOut();
1737
1883
  }
1884
+ /**
1885
+ * Enable or disable debug logging.
1886
+ *
1887
+ * @example
1888
+ * ```ts
1889
+ * // Enable debug logging
1890
+ * client.debug(true)
1891
+ * ```
1892
+ *
1893
+ * @example
1894
+ * ```ts
1895
+ * // Disable debug logging
1896
+ * client.debug(false)
1897
+ * ```
1898
+ *
1899
+ * {@label Initialization}
1900
+ *
1901
+ * @param enabled - Whether to enable debug logging
1902
+ */
1738
1903
  debug(enabled = true) {
1739
1904
  super.debug(enabled);
1740
1905
  this.featureFlagsPoller?.debug(enabled);
1741
1906
  }
1907
+ /**
1908
+ * Capture an event manually.
1909
+ *
1910
+ * @example
1911
+ * ```ts
1912
+ * // Basic capture
1913
+ * client.capture({
1914
+ * distinctId: 'user_123',
1915
+ * event: 'button_clicked',
1916
+ * properties: { button_color: 'red' }
1917
+ * })
1918
+ * ```
1919
+ *
1920
+ * {@label Capture}
1921
+ *
1922
+ * @param props - The event properties
1923
+ * @returns void
1924
+ */
1742
1925
  capture(props) {
1743
1926
  if (typeof props === 'string') {
1744
1927
  this.logMsgIfDebug(() => console.warn('Called capture() with a string as the first argument when an object was expected.'));
@@ -1760,6 +1943,49 @@ class PostHogBackendClient extends PostHogCoreStateless {
1760
1943
  }
1761
1944
  }));
1762
1945
  }
1946
+ /**
1947
+ * Capture an event immediately (synchronously).
1948
+ *
1949
+ * @example
1950
+ * ```ts
1951
+ * // Basic immediate capture
1952
+ * await client.captureImmediate({
1953
+ * distinctId: 'user_123',
1954
+ * event: 'button_clicked',
1955
+ * properties: { button_color: 'red' }
1956
+ * })
1957
+ * ```
1958
+ *
1959
+ * @example
1960
+ * ```ts
1961
+ * // With feature flags
1962
+ * await client.captureImmediate({
1963
+ * distinctId: 'user_123',
1964
+ * event: 'user_action',
1965
+ * sendFeatureFlags: true
1966
+ * })
1967
+ * ```
1968
+ *
1969
+ * @example
1970
+ * ```ts
1971
+ * // With custom feature flags options
1972
+ * await client.captureImmediate({
1973
+ * distinctId: 'user_123',
1974
+ * event: 'user_action',
1975
+ * sendFeatureFlags: {
1976
+ * onlyEvaluateLocally: true,
1977
+ * personProperties: { plan: 'premium' },
1978
+ * groupProperties: { org: { tier: 'enterprise' } }
1979
+ * flagKeys: ['flag1', 'flag2']
1980
+ * }
1981
+ * })
1982
+ * ```
1983
+ *
1984
+ * {@label Capture}
1985
+ *
1986
+ * @param props - The event properties
1987
+ * @returns Promise that resolves when the event is captured
1988
+ */
1763
1989
  async captureImmediate(props) {
1764
1990
  if (typeof props === 'string') {
1765
1991
  this.logMsgIfDebug(() => console.warn('Called captureImmediate() with a string as the first argument when an object was expected.'));
@@ -1781,6 +2007,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
1781
2007
  }
1782
2008
  }));
1783
2009
  }
2010
+ /**
2011
+ * Identify a user and set their properties.
2012
+ *
2013
+ * @example
2014
+ * ```ts
2015
+ * // Basic identify with properties
2016
+ * client.identify({
2017
+ * distinctId: 'user_123',
2018
+ * properties: {
2019
+ * name: 'John Doe',
2020
+ * email: 'john@example.com',
2021
+ * plan: 'premium'
2022
+ * }
2023
+ * })
2024
+ * ```
2025
+ *
2026
+ * @example
2027
+ * ```ts
2028
+ * // Using $set and $set_once
2029
+ * client.identify({
2030
+ * distinctId: 'user_123',
2031
+ * properties: {
2032
+ * $set: { name: 'John Doe', email: 'john@example.com' },
2033
+ * $set_once: { first_login: new Date().toISOString() }
2034
+ * }
2035
+ * })
2036
+ * ```
2037
+ *
2038
+ * {@label Identification}
2039
+ *
2040
+ * @param data - The identify data containing distinctId and properties
2041
+ */
1784
2042
  identify({
1785
2043
  distinctId,
1786
2044
  properties,
@@ -1799,6 +2057,26 @@ class PostHogBackendClient extends PostHogCoreStateless {
1799
2057
  disableGeoip
1800
2058
  });
1801
2059
  }
2060
+ /**
2061
+ * Identify a user and set their properties immediately (synchronously).
2062
+ *
2063
+ * @example
2064
+ * ```ts
2065
+ * // Basic immediate identify
2066
+ * await client.identifyImmediate({
2067
+ * distinctId: 'user_123',
2068
+ * properties: {
2069
+ * name: 'John Doe',
2070
+ * email: 'john@example.com'
2071
+ * }
2072
+ * })
2073
+ * ```
2074
+ *
2075
+ * {@label Identification}
2076
+ *
2077
+ * @param data - The identify data containing distinctId and properties
2078
+ * @returns Promise that resolves when the identify is processed
2079
+ */
1802
2080
  async identifyImmediate({
1803
2081
  distinctId,
1804
2082
  properties,
@@ -1816,19 +2094,96 @@ class PostHogBackendClient extends PostHogCoreStateless {
1816
2094
  disableGeoip
1817
2095
  });
1818
2096
  }
2097
+ /**
2098
+ * Create an alias to link two distinct IDs together.
2099
+ *
2100
+ * @example
2101
+ * ```ts
2102
+ * // Link an anonymous user to an identified user
2103
+ * client.alias({
2104
+ * distinctId: 'anonymous_123',
2105
+ * alias: 'user_456'
2106
+ * })
2107
+ * ```
2108
+ *
2109
+ * {@label Identification}
2110
+ *
2111
+ * @param data - The alias data containing distinctId and alias
2112
+ */
1819
2113
  alias(data) {
1820
2114
  super.aliasStateless(data.alias, data.distinctId, undefined, {
1821
2115
  disableGeoip: data.disableGeoip
1822
2116
  });
1823
2117
  }
2118
+ /**
2119
+ * Create an alias to link two distinct IDs together immediately (synchronously).
2120
+ *
2121
+ * @example
2122
+ * ```ts
2123
+ * // Link an anonymous user to an identified user immediately
2124
+ * await client.aliasImmediate({
2125
+ * distinctId: 'anonymous_123',
2126
+ * alias: 'user_456'
2127
+ * })
2128
+ * ```
2129
+ *
2130
+ * {@label Identification}
2131
+ *
2132
+ * @param data - The alias data containing distinctId and alias
2133
+ * @returns Promise that resolves when the alias is processed
2134
+ */
1824
2135
  async aliasImmediate(data) {
1825
2136
  await super.aliasStatelessImmediate(data.alias, data.distinctId, undefined, {
1826
2137
  disableGeoip: data.disableGeoip
1827
2138
  });
1828
2139
  }
2140
+ /**
2141
+ * Check if local evaluation of feature flags is ready.
2142
+ *
2143
+ * @example
2144
+ * ```ts
2145
+ * // Check if ready
2146
+ * if (client.isLocalEvaluationReady()) {
2147
+ * // Local evaluation is ready, can evaluate flags locally
2148
+ * const flag = await client.getFeatureFlag('flag-key', 'user_123')
2149
+ * } else {
2150
+ * // Local evaluation not ready, will use remote evaluation
2151
+ * const flag = await client.getFeatureFlag('flag-key', 'user_123')
2152
+ * }
2153
+ * ```
2154
+ *
2155
+ * {@label Feature flags}
2156
+ *
2157
+ * @returns true if local evaluation is ready, false otherwise
2158
+ */
1829
2159
  isLocalEvaluationReady() {
1830
2160
  return this.featureFlagsPoller?.isLocalEvaluationReady() ?? false;
1831
2161
  }
2162
+ /**
2163
+ * Wait for local evaluation of feature flags to be ready.
2164
+ *
2165
+ * @example
2166
+ * ```ts
2167
+ * // Wait for local evaluation
2168
+ * const isReady = await client.waitForLocalEvaluationReady()
2169
+ * if (isReady) {
2170
+ * console.log('Local evaluation is ready')
2171
+ * } else {
2172
+ * console.log('Local evaluation timed out')
2173
+ * }
2174
+ * ```
2175
+ *
2176
+ * @example
2177
+ * ```ts
2178
+ * // Wait with custom timeout
2179
+ * const isReady = await client.waitForLocalEvaluationReady(10000) // 10 seconds
2180
+ * ```
2181
+ *
2182
+ * {@label Feature flags}
2183
+ *
2184
+ * @param timeoutMs - Timeout in milliseconds (default: 30000)
2185
+ * @returns Promise that resolves to true if ready, false if timed out
2186
+ */
1832
2187
  async waitForLocalEvaluationReady(timeoutMs = THIRTY_SECONDS) {
1833
2188
  if (this.isLocalEvaluationReady()) {
1834
2189
  return true;
@@ -1848,6 +2203,47 @@ class PostHogBackendClient extends PostHogCoreStateless {
1848
2203
  });
1849
2204
  });
1850
2205
  }
2206
+ /**
2207
+ * Get the value of a feature flag for a specific user.
2208
+ *
2209
+ * @example
2210
+ * ```ts
2211
+ * // Basic feature flag check
2212
+ * const flagValue = await client.getFeatureFlag('new-feature', 'user_123')
2213
+ * if (flagValue === 'variant-a') {
2214
+ * // Show variant A
2215
+ * } else if (flagValue === 'variant-b') {
2216
+ * // Show variant B
2217
+ * } else {
2218
+ * // Flag is disabled or not found
2219
+ * }
2220
+ * ```
2221
+ *
2222
+ * @example
2223
+ * ```ts
2224
+ * // With groups and properties
2225
+ * const flagValue = await client.getFeatureFlag('org-feature', 'user_123', {
2226
+ * groups: { organization: 'acme-corp' },
2227
+ * personProperties: { plan: 'enterprise' },
2228
+ * groupProperties: { organization: { tier: 'premium' } }
2229
+ * })
2230
+ * ```
2231
+ *
2232
+ * @example
2233
+ * ```ts
2234
+ * // Only evaluate locally
2235
+ * const flagValue = await client.getFeatureFlag('local-flag', 'user_123', {
2236
+ * onlyEvaluateLocally: true
2237
+ * })
2238
+ * ```
2239
+ *
2240
+ * {@label Feature flags}
2241
+ *
2242
+ * @param key - The feature flag key
2243
+ * @param distinctId - The user's distinct ID
2244
+ * @param options - Optional configuration for flag evaluation
2245
+ * @returns Promise that resolves to the flag value or undefined
2246
+ */
1851
2247
  async getFeatureFlag(key, distinctId, options) {
1852
2248
  const {
1853
2249
  groups,
@@ -1867,7 +2263,7 @@ class PostHogBackendClient extends PostHogCoreStateless {
1867
2263
  onlyEvaluateLocally = false;
1868
2264
  }
1869
2265
  if (sendFeatureFlagEvents == undefined) {
1870
- sendFeatureFlagEvents = true;
2266
+ sendFeatureFlagEvents = this.options.sendFeatureFlagEvent ?? true;
1871
2267
  }
1872
2268
  let response = await this.featureFlagsPoller?.getFeatureFlag(key, distinctId, groups, personProperties, groupProperties);
1873
2269
  const flagWasLocallyEvaluated = response !== undefined;
@@ -1911,6 +2307,41 @@ class PostHogBackendClient extends PostHogCoreStateless {
1911
2307
  }
1912
2308
  return response;
1913
2309
  }
2310
+ /**
2311
+ * Get the payload for a feature flag.
2312
+ *
2313
+ * @example
2314
+ * ```ts
2315
+ * // Get payload for a feature flag
2316
+ * const payload = await client.getFeatureFlagPayload('flag-key', 'user_123')
2317
+ * if (payload) {
2318
+ * console.log('Flag payload:', payload)
2319
+ * }
2320
+ * ```
2321
+ *
2322
+ * @example
2323
+ * ```ts
2324
+ * // Get payload with specific match value
2325
+ * const payload = await client.getFeatureFlagPayload('flag-key', 'user_123', 'variant-a')
2326
+ * ```
2327
+ *
2328
+ * @example
2329
+ * ```ts
2330
+ * // With groups and properties
2331
+ * const payload = await client.getFeatureFlagPayload('org-flag', 'user_123', undefined, {
2332
+ * groups: { organization: 'acme-corp' },
2333
+ * personProperties: { plan: 'enterprise' }
2334
+ * })
2335
+ * ```
2336
+ *
2337
+ * {@label Feature flags}
2338
+ *
2339
+ * @param key - The feature flag key
2340
+ * @param distinctId - The user's distinct ID
2341
+ * @param matchValue - Optional match value to get payload for
2342
+ * @param options - Optional configuration for flag evaluation
2343
+ * @returns Promise that resolves to the flag payload or undefined
2344
+ */
1914
2345
  async getFeatureFlagPayload(key, distinctId, matchValue, options) {
1915
2346
  const {
1916
2347
  groups,
@@ -1918,7 +2349,6 @@ class PostHogBackendClient extends PostHogCoreStateless {
1918
2349
  } = options || {};
1919
2350
  let {
1920
2351
  onlyEvaluateLocally,
1921
- sendFeatureFlagEvents,
1922
2352
  personProperties,
1923
2353
  groupProperties
1924
2354
  } = options || {};
@@ -1943,15 +2373,30 @@ class PostHogBackendClient extends PostHogCoreStateless {
1943
2373
  if (onlyEvaluateLocally == undefined) {
1944
2374
  onlyEvaluateLocally = false;
1945
2375
  }
1946
- if (sendFeatureFlagEvents == undefined) {
1947
- sendFeatureFlagEvents = true;
1948
- }
1949
2376
  const payloadWasLocallyEvaluated = response !== undefined;
1950
2377
  if (!payloadWasLocallyEvaluated && !onlyEvaluateLocally) {
1951
2378
  response = await super.getFeatureFlagPayloadStateless(key, distinctId, groups, personProperties, groupProperties, disableGeoip);
1952
2379
  }
1953
2380
  return response;
1954
2381
  }
2382
+ /**
2383
+ * Get the remote config payload for a feature flag.
2384
+ *
2385
+ * @example
2386
+ * ```ts
2387
+ * // Get remote config payload
2388
+ * const payload = await client.getRemoteConfigPayload('flag-key')
2389
+ * if (payload) {
2390
+ * console.log('Remote config payload:', payload)
2391
+ * }
2392
+ * ```
2393
+ *
2394
+ * {@label Feature flags}
2395
+ *
2396
+ * @param flagKey - The feature flag key
2397
+ * @returns Promise that resolves to the remote config payload or undefined
2398
+ * @throws Error if personal API key is not provided
2399
+ */
1955
2400
  async getRemoteConfigPayload(flagKey) {
1956
2401
  if (!this.options.personalApiKey) {
1957
2402
  throw new Error('Personal API key is required for remote config payload decryption');
@@ -1975,6 +2420,38 @@ class PostHogBackendClient extends PostHogCoreStateless {
1975
2420
  }
1976
2421
  return parsed;
1977
2422
  }
2423
+ /**
2424
+ * Check if a feature flag is enabled for a specific user.
2425
+ *
2426
+ * @example
2427
+ * ```ts
2428
+ * // Basic feature flag check
2429
+ * const isEnabled = await client.isFeatureEnabled('new-feature', 'user_123')
2430
+ * if (isEnabled) {
2431
+ * // Feature is enabled
2432
+ * console.log('New feature is active')
2433
+ * } else {
2434
+ * // Feature is disabled
2435
+ * console.log('New feature is not active')
2436
+ * }
2437
+ * ```
2438
+ *
2439
+ * @example
2440
+ * ```ts
2441
+ * // With groups and properties
2442
+ * const isEnabled = await client.isFeatureEnabled('org-feature', 'user_123', {
2443
+ * groups: { organization: 'acme-corp' },
2444
+ * personProperties: { plan: 'enterprise' }
2445
+ * })
2446
+ * ```
2447
+ *
2448
+ * {@label Feature flags}
2449
+ *
2450
+ * @param key - The feature flag key
2451
+ * @param distinctId - The user's distinct ID
2452
+ * @param options - Optional configuration for flag evaluation
2453
+ * @returns Promise that resolves to true if enabled, false if disabled, undefined if not found
2454
+ */
1978
2455
  async isFeatureEnabled(key, distinctId, options) {
1979
2456
  const feat = await this.getFeatureFlag(key, distinctId, options);
1980
2457
  if (feat === undefined) {
@@ -1982,10 +2459,77 @@ class PostHogBackendClient extends PostHogCoreStateless {
1982
2459
  }
1983
2460
  return !!feat || false;
1984
2461
  }
2462
+ /**
2463
+ * Get all feature flag values for a specific user.
2464
+ *
2465
+ * @example
2466
+ * ```ts
2467
+ * // Get all flags for a user
2468
+ * const allFlags = await client.getAllFlags('user_123')
2469
+ * console.log('User flags:', allFlags)
2470
+ * // Output: { 'flag-1': 'variant-a', 'flag-2': false, 'flag-3': 'variant-b' }
2471
+ * ```
2472
+ *
2473
+ * @example
2474
+ * ```ts
2475
+ * // With specific flag keys
2476
+ * const specificFlags = await client.getAllFlags('user_123', {
2477
+ * flagKeys: ['flag-1', 'flag-2']
2478
+ * })
2479
+ * ```
2480
+ *
2481
+ * @example
2482
+ * ```ts
2483
+ * // With groups and properties
2484
+ * const orgFlags = await client.getAllFlags('user_123', {
2485
+ * groups: { organization: 'acme-corp' },
2486
+ * personProperties: { plan: 'enterprise' }
2487
+ * })
2488
+ * ```
2489
+ *
2490
+ * {@label Feature flags}
2491
+ *
2492
+ * @param distinctId - The user's distinct ID
2493
+ * @param options - Optional configuration for flag evaluation
2494
+ * @returns Promise that resolves to a record of flag keys and their values
2495
+ */
1985
2496
  async getAllFlags(distinctId, options) {
1986
2497
  const response = await this.getAllFlagsAndPayloads(distinctId, options);
1987
2498
  return response.featureFlags || {};
1988
2499
  }
2500
+ /**
2501
+ * Get all feature flag values and payloads for a specific user.
2502
+ *
2503
+ * @example
2504
+ * ```ts
2505
+ * // Get all flags and payloads for a user
2506
+ * const result = await client.getAllFlagsAndPayloads('user_123')
2507
+ * console.log('Flags:', result.featureFlags)
2508
+ * console.log('Payloads:', result.featureFlagPayloads)
2509
+ * ```
2510
+ *
2511
+ * @example
2512
+ * ```ts
2513
+ * // With specific flag keys
2514
+ * const result = await client.getAllFlagsAndPayloads('user_123', {
2515
+ * flagKeys: ['flag-1', 'flag-2']
2516
+ * })
2517
+ * ```
2518
+ *
2519
+ * @example
2520
+ * ```ts
2521
+ * // Only evaluate locally
2522
+ * const result = await client.getAllFlagsAndPayloads('user_123', {
2523
+ * onlyEvaluateLocally: true
2524
+ * })
2525
+ * ```
2526
+ *
2527
+ * {@label Feature flags}
2528
+ *
2529
+ * @param distinctId - The user's distinct ID
2530
+ * @param options - Optional configuration for flag evaluation
2531
+ * @returns Promise that resolves to flags and payloads
2532
+ */
1989
2533
  async getAllFlagsAndPayloads(distinctId, options) {
1990
2534
  const {
1991
2535
  groups,
@@ -2029,6 +2573,41 @@ class PostHogBackendClient extends PostHogCoreStateless {
2029
2573
  featureFlagPayloads
2030
2574
  };
2031
2575
  }
2576
+ /**
2577
+ * Create or update a group and its properties.
2578
+ *
2579
+ * @example
2580
+ * ```ts
2581
+ * // Create a company group
2582
+ * client.groupIdentify({
2583
+ * groupType: 'company',
2584
+ * groupKey: 'acme-corp',
2585
+ * properties: {
2586
+ * name: 'Acme Corporation',
2587
+ * industry: 'Technology',
2588
+ * employee_count: 500
2589
+ * },
2590
+ * distinctId: 'user_123'
2591
+ * })
2592
+ * ```
2593
+ *
2594
+ * @example
2595
+ * ```ts
2596
+ * // Update organization properties
2597
+ * client.groupIdentify({
2598
+ * groupType: 'organization',
2599
+ * groupKey: 'org-456',
2600
+ * properties: {
2601
+ * plan: 'enterprise',
2602
+ * region: 'US-West'
2603
+ * }
2604
+ * })
2605
+ * ```
2606
+ *
2607
+ * {@label Identification}
2608
+ *
2609
+ * @param data - The group identify data
2610
+ */
2032
2611
  groupIdentify({
2033
2612
  groupType,
2034
2613
  groupKey,
@@ -2041,12 +2620,49 @@ class PostHogBackendClient extends PostHogCoreStateless {
2041
2620
  }, distinctId);
2042
2621
  }
2043
2622
  /**
2044
- * Reloads the feature flag definitions from the server for local evaluation.
2045
- * This is useful to call if you want to ensure that the feature flags are up to date before calling getFeatureFlag.
2623
+ * Reload feature flag definitions from the server for local evaluation.
2624
+ *
2625
+ * @example
2626
+ * ```ts
2627
+ * // Force reload of feature flags
2628
+ * await client.reloadFeatureFlags()
2629
+ * console.log('Feature flags reloaded')
2630
+ * ```
2631
+ *
2632
+ * @example
2633
+ * ```ts
2634
+ * // Reload before checking a specific flag
2635
+ * await client.reloadFeatureFlags()
2636
+ * const flag = await client.getFeatureFlag('flag-key', 'user_123')
2637
+ * ```
2638
+ *
2639
+ * {@label Feature flags}
2640
+ *
2641
+ * @returns Promise that resolves when flags are reloaded
2046
2642
  */
2047
2643
  async reloadFeatureFlags() {
2048
2644
  await this.featureFlagsPoller?.loadFeatureFlags(true);
2049
2645
  }
2646
+ /**
2647
+ * Shutdown the PostHog client gracefully.
2648
+ *
2649
+ * @example
2650
+ * ```ts
2651
+ * // Shutdown with default timeout
2652
+ * await client._shutdown()
2653
+ * ```
2654
+ *
2655
+ * @example
2656
+ * ```ts
2657
+ * // Shutdown with custom timeout
2658
+ * await client._shutdown(5000) // 5 seconds
2659
+ * ```
2660
+ *
2661
+ * {@label Shutdown}
2662
+ *
2663
+ * @param shutdownTimeoutMs - Timeout in milliseconds for shutdown
2664
+ * @returns Promise that resolves when shutdown is complete
2665
+ */
2050
2666
  async _shutdown(shutdownTimeoutMs) {
2051
2667
  this.featureFlagsPoller?.stopPoller();
2052
2668
  this.errorTracking.shutdown();
@@ -2175,12 +2791,81 @@ class PostHogBackendClient extends PostHogCoreStateless {
2175
2791
  allGroupProperties
2176
2792
  };
2177
2793
  }
2794
+ /**
2795
+ * Capture an error exception as an event.
2796
+ *
2797
+ * @example
2798
+ * ```ts
2799
+ * // Capture an error with user ID
2800
+ * try {
2801
+ * // Some risky operation
2802
+ * riskyOperation()
2803
+ * } catch (error) {
2804
+ * client.captureException(error, 'user_123')
2805
+ * }
2806
+ * ```
2807
+ *
2808
+ * @example
2809
+ * ```ts
2810
+ * // Capture with additional properties
2811
+ * try {
2812
+ * apiCall()
2813
+ * } catch (error) {
2814
+ * client.captureException(error, 'user_123', {
2815
+ * endpoint: '/api/users',
2816
+ * method: 'POST',
2817
+ * status_code: 500
2818
+ * })
2819
+ * }
2820
+ * ```
2821
+ *
2822
+ * {@label Error tracking}
2823
+ *
2824
+ * @param error - The error to capture
2825
+ * @param distinctId - Optional user distinct ID
2826
+ * @param additionalProperties - Optional additional properties to include
2827
+ */
2178
2828
  captureException(error, distinctId, additionalProperties) {
2179
2829
  const syntheticException = new Error('PostHog syntheticException');
2180
2830
  this.addPendingPromise(ErrorTracking.buildEventMessage(error, {
2181
2831
  syntheticException
2182
2832
  }, distinctId, additionalProperties).then(msg => this.capture(msg)));
2183
2833
  }
2834
+ /**
2835
+ * Capture an error exception as an event immediately (synchronously).
2836
+ *
2837
+ * @example
2838
+ * ```ts
2839
+ * // Capture an error immediately with user ID
2840
+ * try {
2841
+ * // Some risky operation
2842
+ * riskyOperation()
2843
+ * } catch (error) {
2844
+ * await client.captureExceptionImmediate(error, 'user_123')
2845
+ * }
2846
+ * ```
2847
+ *
2848
+ * @example
2849
+ * ```ts
2850
+ * // Capture with additional properties
2851
+ * try {
2852
+ * apiCall()
2853
+ * } catch (error) {
2854
+ * await client.captureExceptionImmediate(error, 'user_123', {
2855
+ * endpoint: '/api/users',
2856
+ * method: 'POST',
2857
+ * status_code: 500
2858
+ * })
2859
+ * }
2860
+ * ```
2861
+ *
2862
+ * {@label Error tracking}
2863
+ *
2864
+ * @param error - The error to capture
2865
+ * @param distinctId - Optional user distinct ID
2866
+ * @param additionalProperties - Optional additional properties to include
2867
+ * @returns Promise that resolves when the error is captured
2868
+ */
2184
2869
  async captureExceptionImmediate(error, distinctId, additionalProperties) {
2185
2870
  const syntheticException = new Error('PostHog syntheticException');
2186
2871
  this.addPendingPromise(ErrorTracking.buildEventMessage(error, {