posthog-node 3.6.2 → 4.0.0-beta.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
@@ -1,21 +1,37 @@
1
1
  /// <reference types="node" />
2
- declare type PosthogCoreOptions = {
2
+ declare type PostHogCoreOptions = {
3
+ /** PostHog API host, usually 'https://app.posthog.com' or 'https://eu.posthog.com' */
3
4
  host?: string;
5
+ /** The number of events to queue before sending to PostHog (flushing) */
4
6
  flushAt?: number;
7
+ /** The interval in milliseconds between periodic flushes */
5
8
  flushInterval?: number;
6
- enable?: boolean;
9
+ /** If set to true the SDK is essentially disabled (useful for local environments where you don't want to track anything) */
10
+ disabled?: boolean;
11
+ /** If set to false the SDK will not track until the `optIn` function is called. */
12
+ defaultOptIn?: boolean;
13
+ /** Whether to track that `getFeatureFlag` was called (used by Experiments) */
7
14
  sendFeatureFlagEvent?: boolean;
15
+ /** Whether to load feature flags when initialized or not */
8
16
  preloadFeatureFlags?: boolean;
17
+ /** Option to bootstrap the library with given distinctId and feature flags */
9
18
  bootstrap?: {
10
19
  distinctId?: string;
11
20
  isIdentifiedId?: boolean;
12
21
  featureFlags?: Record<string, boolean | string>;
13
22
  featureFlagPayloads?: Record<string, JsonType>;
14
23
  };
24
+ /** How many times we will retry HTTP requests. Defaults to 3. */
15
25
  fetchRetryCount?: number;
26
+ /** The delay between HTTP request retries, Defaults to 3 seconds. */
16
27
  fetchRetryDelay?: number;
28
+ /** Timeout in milliseconds for any calls. Defaults to 10 seconds. */
17
29
  requestTimeout?: number;
30
+ /** Timeout in milliseconds for feature flag calls. Defaults to 10 seconds for stateful clients, and 3 seconds for stateless. */
31
+ featureFlagsRequestTimeoutMs?: number;
32
+ /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
18
33
  sessionExpirationTimeSeconds?: number;
34
+ /** Whether to post events to PostHog in JSON or compressed format */
19
35
  captureMode?: 'json' | 'form';
20
36
  disableGeoip?: boolean;
21
37
  };
@@ -79,7 +95,7 @@ declare type PostHogDecideResponse = {
79
95
  errorsWhileComputingFlags: boolean;
80
96
  sessionRecording: boolean;
81
97
  };
82
- declare type PosthogFlagsAndPayloadsResponse = {
98
+ declare type PostHogFlagsAndPayloadsResponse = {
83
99
  featureFlags: PostHogDecideResponse['featureFlags'];
84
100
  featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];
85
101
  };
@@ -88,9 +104,9 @@ declare type JsonType = string | number | boolean | null | {
88
104
  } | Array<JsonType>;
89
105
 
90
106
  interface RetriableOptions {
91
- retryCount?: number;
92
- retryDelay?: number;
93
- retryCheck?: (err: any) => boolean;
107
+ retryCount: number;
108
+ retryDelay: number;
109
+ retryCheck: (err: any) => boolean;
94
110
  }
95
111
 
96
112
  declare class SimpleEventEmitter {
@@ -108,44 +124,49 @@ declare abstract class PostHogCoreStateless {
108
124
  private flushAt;
109
125
  private flushInterval;
110
126
  private requestTimeout;
127
+ private featureFlagsRequestTimeoutMs;
111
128
  private captureMode;
112
129
  private removeDebugCallback?;
113
- private debugMode;
114
130
  private disableGeoip;
115
- private _optoutOverride;
131
+ disabled: boolean;
132
+ private defaultOptIn;
116
133
  private pendingPromises;
117
134
  protected _events: SimpleEventEmitter;
118
135
  protected _flushTimer?: any;
119
136
  protected _retryOptions: RetriableOptions;
137
+ protected _initPromise: Promise<void>;
138
+ protected _isInitialized: boolean;
120
139
  abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
121
140
  abstract getLibraryId(): string;
122
141
  abstract getLibraryVersion(): string;
123
142
  abstract getCustomUserAgent(): string | void;
124
143
  abstract getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
125
144
  abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
126
- constructor(apiKey: string, options?: PosthogCoreOptions);
145
+ constructor(apiKey: string, options?: PostHogCoreOptions);
146
+ protected wrap(fn: () => void): void;
127
147
  protected getCommonEventProperties(): any;
128
148
  get optedOut(): boolean;
129
- optIn(): void;
130
- optOut(): void;
149
+ optIn(): Promise<void>;
150
+ optOut(): Promise<void>;
131
151
  on(event: string, cb: (...args: any[]) => void): () => void;
132
152
  debug(enabled?: boolean): void;
153
+ get isDebug(): boolean;
133
154
  private buildPayload;
134
155
  protected addPendingPromise(promise: Promise<any>): void;
135
156
  /***
136
157
  *** TRACKING
137
158
  ***/
138
- protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): this;
159
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PostHogCaptureOptions): void;
139
160
  protected captureStateless(distinctId: string, event: string, properties?: {
140
161
  [key: string]: any;
141
- }, options?: PostHogCaptureOptions): this;
162
+ }, options?: PostHogCaptureOptions): void;
142
163
  protected aliasStateless(alias: string, distinctId: string, properties?: {
143
164
  [key: string]: any;
144
- }, options?: PostHogCaptureOptions): this;
165
+ }, options?: PostHogCaptureOptions): void;
145
166
  /***
146
167
  *** GROUPS
147
168
  ***/
148
- protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
169
+ protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PostHogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): void;
149
170
  /***
150
171
  *** FEATURE FLAGS
151
172
  ***/
@@ -166,8 +187,8 @@ declare abstract class PostHogCoreStateless {
166
187
  flushAsync(): Promise<any>;
167
188
  flush(callback?: (err?: any, data?: any) => void): void;
168
189
  private fetchWithRetry;
169
- shutdownAsync(): Promise<void>;
170
- shutdown(): void;
190
+ shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
191
+ shutdown(shutdownTimeoutMs?: number): void;
171
192
  }
172
193
 
173
194
  interface IdentifyMessage {
@@ -305,15 +326,16 @@ declare type PostHogNodeV1 = {
305
326
  /**
306
327
  * @description Flushes the events still in the queue and clears the feature flags poller to allow for
307
328
  * a clean shutdown.
329
+ *
330
+ * @param shutdownTimeoutMs The shutdown timeout, in milliseconds. Defaults to 30000 (30s).
308
331
  */
309
- shutdown(): void;
332
+ shutdown(shutdownTimeoutMs?: number): void;
310
333
  };
311
334
 
312
- declare type PostHogOptions = PosthogCoreOptions & {
335
+ declare type PostHogOptions = PostHogCoreOptions & {
313
336
  persistence?: 'memory';
314
337
  personalApiKey?: string;
315
338
  featureFlagsPollingInterval?: number;
316
- requestTimeout?: number;
317
339
  maxCacheSize?: number;
318
340
  fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
319
341
  };
@@ -330,8 +352,8 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
330
352
  getLibraryId(): string;
331
353
  getLibraryVersion(): string;
332
354
  getCustomUserAgent(): string;
333
- enable(): void;
334
- disable(): void;
355
+ enable(): Promise<void>;
356
+ disable(): Promise<void>;
335
357
  debug(enabled?: boolean): void;
336
358
  capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp, disableGeoip, uuid, }: EventMessage): void;
337
359
  identify({ distinctId, properties, disableGeoip }: IdentifyMessage): void;
@@ -377,11 +399,11 @@ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
377
399
  groupProperties?: Record<string, Record<string, string>>;
378
400
  onlyEvaluateLocally?: boolean;
379
401
  disableGeoip?: boolean;
380
- }): Promise<PosthogFlagsAndPayloadsResponse>;
402
+ }): Promise<PostHogFlagsAndPayloadsResponse>;
381
403
  groupIdentify({ groupType, groupKey, properties, distinctId, disableGeoip }: GroupIdentifyMessage): void;
382
404
  reloadFeatureFlags(): Promise<void>;
383
- shutdown(): void;
384
- shutdownAsync(): Promise<void>;
405
+ shutdown(shutdownTimeoutMs?: number): void;
406
+ shutdownAsync(shutdownTimeoutMs?: number): Promise<void>;
385
407
  private addLocalPersonAndGroupProperties;
386
408
  }
387
409