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