posthog-node 2.3.1 → 2.5.0

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
@@ -18,6 +18,20 @@ declare type PosthogCoreOptions = {
18
18
  sessionExpirationTimeSeconds?: number;
19
19
  captureMode?: 'json' | 'form';
20
20
  };
21
+ declare enum PostHogPersistedProperty {
22
+ AnonymousId = "anonymous_id",
23
+ DistinctId = "distinct_id",
24
+ Props = "props",
25
+ FeatureFlags = "feature_flags",
26
+ FeatureFlagPayloads = "feature_flag_payloads",
27
+ OverrideFeatureFlags = "override_feature_flags",
28
+ Queue = "queue",
29
+ OptedOut = "opted_out",
30
+ SessionId = "session_id",
31
+ SessionLastTimestamp = "session_timestamp",
32
+ PersonProperties = "person_properties",
33
+ GroupProperties = "group_properties"
34
+ }
21
35
  declare type PostHogFetchOptions = {
22
36
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
23
37
  mode?: 'no-cors';
@@ -28,11 +42,17 @@ declare type PostHogFetchOptions = {
28
42
  body?: string;
29
43
  signal?: AbortSignal;
30
44
  };
45
+ declare type PosthogCaptureOptions = {
46
+ timestamp?: Date;
47
+ };
31
48
  declare type PostHogFetchResponse = {
32
49
  status: number;
33
50
  text: () => Promise<string>;
34
51
  json: () => Promise<any>;
35
52
  };
53
+ declare type PostHogEventProperties = {
54
+ [key: string]: any;
55
+ };
36
56
  declare type PostHogDecideResponse = {
37
57
  config: {
38
58
  enable_collect_everything: boolean;
@@ -60,6 +80,85 @@ declare type JsonType = string | number | boolean | null | {
60
80
  [key: string]: JsonType;
61
81
  } | Array<JsonType>;
62
82
 
83
+ interface RetriableOptions {
84
+ retryCount?: number;
85
+ retryDelay?: number;
86
+ retryCheck?: (err: any) => true;
87
+ }
88
+
89
+ declare class SimpleEventEmitter {
90
+ events: {
91
+ [key: string]: ((...args: any[]) => void)[];
92
+ };
93
+ constructor();
94
+ on(event: string, listener: (...args: any[]) => void): () => void;
95
+ emit(event: string, payload: any): void;
96
+ }
97
+
98
+ declare abstract class PostHogCoreStateless {
99
+ private apiKey;
100
+ host: string;
101
+ private flushAt;
102
+ private flushInterval;
103
+ private requestTimeout;
104
+ private captureMode;
105
+ private removeDebugCallback?;
106
+ private _optoutOverride;
107
+ protected _events: SimpleEventEmitter;
108
+ protected _flushTimer?: any;
109
+ protected _retryOptions: RetriableOptions;
110
+ abstract fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
111
+ abstract getLibraryId(): string;
112
+ abstract getLibraryVersion(): string;
113
+ abstract getCustomUserAgent(): string | void;
114
+ abstract getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
115
+ abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
116
+ constructor(apiKey: string, options?: PosthogCoreOptions);
117
+ protected getCommonEventProperties(): any;
118
+ get optedOut(): boolean;
119
+ optIn(): void;
120
+ optOut(): void;
121
+ on(event: string, cb: (...args: any[]) => void): () => void;
122
+ debug(enabled?: boolean): void;
123
+ private buildPayload;
124
+ /***
125
+ *** TRACKING
126
+ ***/
127
+ protected identifyStateless(distinctId: string, properties?: PostHogEventProperties, options?: PosthogCaptureOptions): this;
128
+ protected captureStateless(distinctId: string, event: string, properties?: {
129
+ [key: string]: any;
130
+ }, options?: PosthogCaptureOptions): this;
131
+ protected aliasStateless(alias: string, distinctId: string, properties?: {
132
+ [key: string]: any;
133
+ }): this;
134
+ /***
135
+ *** GROUPS
136
+ ***/
137
+ protected groupIdentifyStateless(groupType: string, groupKey: string | number, groupProperties?: PostHogEventProperties, options?: PosthogCaptureOptions, distinctId?: string, eventProperties?: PostHogEventProperties): this;
138
+ /***
139
+ *** FEATURE FLAGS
140
+ ***/
141
+ protected getDecide(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, extraPayload?: Record<string, any>): Promise<PostHogDecideResponse | undefined>;
142
+ protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<boolean | string | undefined>;
143
+ protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<JsonType | undefined>;
144
+ protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
145
+ protected _parsePayload(response: any): any;
146
+ protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
147
+ protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<{
148
+ flags: PostHogDecideResponse['featureFlags'] | undefined;
149
+ payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
150
+ }>;
151
+ /***
152
+ *** QUEUEING AND FLUSHING
153
+ ***/
154
+ protected enqueue(type: string, _message: any, options?: PosthogCaptureOptions): void;
155
+ flushAsync(): Promise<any>;
156
+ flush(callback?: (err?: any, data?: any) => void): void;
157
+ private fetchWithRetry;
158
+ shutdownAsync(): Promise<void>;
159
+ shutdown(): void;
160
+ }
161
+
63
162
  interface IdentifyMessageV1 {
64
163
  distinctId: string;
65
164
  properties?: Record<string | number, any>;
@@ -68,6 +167,7 @@ interface EventMessageV1 extends IdentifyMessageV1 {
68
167
  event: string;
69
168
  groups?: Record<string, string | number>;
70
169
  sendFeatureFlags?: boolean;
170
+ timestamp?: Date;
71
171
  }
72
172
  interface GroupIdentifyMessage {
73
173
  groupType: string;
@@ -202,16 +302,22 @@ declare type PostHogOptions = PosthogCoreOptions & {
202
302
  maxCacheSize?: number;
203
303
  fetch?: (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
204
304
  };
205
- declare class PostHog implements PostHogNodeV1 {
206
- private _sharedClient;
305
+ declare class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
306
+ private _memoryStorage;
207
307
  private featureFlagsPoller?;
208
308
  private maxCacheSize;
309
+ private options;
209
310
  distinctIdHasSentFlagCalls: Record<string, string[]>;
210
311
  constructor(apiKey: string, options?: PostHogOptions);
211
- private reInit;
312
+ getPersistedProperty(key: PostHogPersistedProperty): any | undefined;
313
+ setPersistedProperty(key: PostHogPersistedProperty, value: any | null): void;
314
+ fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse>;
315
+ getLibraryId(): string;
316
+ getLibraryVersion(): string;
317
+ getCustomUserAgent(): string;
212
318
  enable(): void;
213
319
  disable(): void;
214
- capture({ distinctId, event, properties, groups, sendFeatureFlags }: EventMessageV1): void;
320
+ capture({ distinctId, event, properties, groups, sendFeatureFlags, timestamp }: EventMessageV1): void;
215
321
  identify({ distinctId, properties }: IdentifyMessageV1): void;
216
322
  alias(data: {
217
323
  distinctId: string;
@@ -252,10 +358,8 @@ declare class PostHog implements PostHogNodeV1 {
252
358
  }): Promise<PosthogFlagsAndPayloadsResponse>;
253
359
  groupIdentify({ groupType, groupKey, properties }: GroupIdentifyMessage): void;
254
360
  reloadFeatureFlags(): Promise<void>;
255
- flush(): void;
256
361
  shutdown(): void;
257
362
  shutdownAsync(): Promise<void>;
258
- debug(enabled?: boolean): void;
259
363
  }
260
364
 
261
365
  export { PostHog, PostHogOptions };