posthog-node 4.2.0 → 4.2.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.
@@ -4,8 +4,8 @@ export * as utils from './utils';
4
4
  import { LZString } from './lz-string';
5
5
  import { SimpleEventEmitter } from './eventemitter';
6
6
  export declare abstract class PostHogCoreStateless {
7
- private apiKey;
8
- host: string;
7
+ readonly apiKey: string;
8
+ readonly host: string;
9
9
  private flushAt;
10
10
  private maxBatchSize;
11
11
  private maxQueueSize;
@@ -17,7 +17,7 @@ export declare abstract class PostHogCoreStateless {
17
17
  private removeDebugCallback?;
18
18
  private disableGeoip;
19
19
  private historicalMigration;
20
- disabled: boolean;
20
+ protected disabled: boolean;
21
21
  private defaultOptIn;
22
22
  private pendingPromises;
23
23
  protected _events: SimpleEventEmitter;
@@ -32,6 +32,7 @@ export declare abstract class PostHogCoreStateless {
32
32
  abstract getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined;
33
33
  abstract setPersistedProperty<T>(key: PostHogPersistedProperty, value: T | null): void;
34
34
  constructor(apiKey: string, options?: PostHogCoreOptions);
35
+ protected logMsgIfDebug(fn: () => void): void;
35
36
  protected wrap(fn: () => void): void;
36
37
  protected getCommonEventProperties(): any;
37
38
  get optedOut(): boolean;
@@ -40,6 +41,7 @@ export declare abstract class PostHogCoreStateless {
40
41
  on(event: string, cb: (...args: any[]) => void): () => void;
41
42
  debug(enabled?: boolean): void;
42
43
  get isDebug(): boolean;
44
+ get isDisabled(): boolean;
43
45
  private buildPayload;
44
46
  protected addPendingPromise<T>(promise: Promise<T>): Promise<T>;
45
47
  /***
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- export declare type PostHogCoreOptions = {
2
+ export type PostHogCoreOptions = {
3
3
  /** PostHog API host, usually 'https://us.i.posthog.com' or 'https://eu.i.posthog.com' */
4
4
  host?: string;
5
5
  /** The number of events to queue before sending to PostHog (flushing) */
@@ -37,7 +37,7 @@ export declare type PostHogCoreOptions = {
37
37
  featureFlagsRequestTimeoutMs?: number;
38
38
  /** For Session Analysis how long before we expire a session (defaults to 30 mins) */
39
39
  sessionExpirationTimeSeconds?: number;
40
- /** Whether to post events to PostHog in JSON or compressed format. Defaults to 'form' */
40
+ /** Whether to post events to PostHog in JSON or compressed format. Defaults to 'json' */
41
41
  captureMode?: 'json' | 'form';
42
42
  disableGeoip?: boolean;
43
43
  /** Special flag to indicate ingested data is for a historical migration. */
@@ -57,9 +57,10 @@ export declare enum PostHogPersistedProperty {
57
57
  PersonProperties = "person_properties",
58
58
  GroupProperties = "group_properties",
59
59
  InstalledAppBuild = "installed_app_build",
60
- InstalledAppVersion = "installed_app_version"
60
+ InstalledAppVersion = "installed_app_version",
61
+ SessionReplay = "session_replay"
61
62
  }
62
- export declare type PostHogFetchOptions = {
63
+ export type PostHogFetchOptions = {
63
64
  method: 'GET' | 'POST' | 'PUT' | 'PATCH';
64
65
  mode?: 'no-cors';
65
66
  credentials?: 'omit';
@@ -69,26 +70,26 @@ export declare type PostHogFetchOptions = {
69
70
  body?: string;
70
71
  signal?: AbortSignal;
71
72
  };
72
- export declare type PostHogCaptureOptions = {
73
+ export type PostHogCaptureOptions = {
73
74
  /** If provided overrides the auto-generated event ID */
74
75
  uuid?: string;
75
76
  /** If provided overrides the auto-generated timestamp */
76
77
  timestamp?: Date;
77
78
  disableGeoip?: boolean;
78
79
  };
79
- export declare type PostHogFetchResponse = {
80
+ export type PostHogFetchResponse = {
80
81
  status: number;
81
82
  text: () => Promise<string>;
82
83
  json: () => Promise<any>;
83
84
  };
84
- export declare type PostHogQueueItem = {
85
+ export type PostHogQueueItem = {
85
86
  message: any;
86
87
  callback?: (err: any) => void;
87
88
  };
88
- export declare type PostHogEventProperties = {
89
+ export type PostHogEventProperties = {
89
90
  [key: string]: any;
90
91
  };
91
- export declare type PostHogAutocaptureElement = {
92
+ export type PostHogAutocaptureElement = {
92
93
  $el_text?: string;
93
94
  tag_name: string;
94
95
  href?: string;
@@ -98,7 +99,7 @@ export declare type PostHogAutocaptureElement = {
98
99
  } & {
99
100
  [key: string]: any;
100
101
  };
101
- export declare type PostHogDecideResponse = {
102
+ export type PostHogDecideResponse = {
102
103
  config: {
103
104
  enable_collect_everything: boolean;
104
105
  };
@@ -115,12 +116,14 @@ export declare type PostHogDecideResponse = {
115
116
  [key: string]: JsonType;
116
117
  };
117
118
  errorsWhileComputingFlags: boolean;
118
- sessionRecording: boolean;
119
+ sessionRecording?: boolean | {
120
+ [key: string]: JsonType;
121
+ };
119
122
  };
120
- export declare type PostHogFlagsAndPayloadsResponse = {
123
+ export type PostHogFlagsAndPayloadsResponse = {
121
124
  featureFlags: PostHogDecideResponse['featureFlags'];
122
125
  featureFlagPayloads: PostHogDecideResponse['featureFlagPayloads'];
123
126
  };
124
- export declare type JsonType = string | number | boolean | null | {
127
+ export type JsonType = string | number | boolean | null | {
125
128
  [key: string]: JsonType;
126
129
  } | Array<JsonType>;
@@ -2,8 +2,8 @@
2
2
  * @file Adapted from [posthog-js](https://github.com/PostHog/posthog-js/blob/8157df935a4d0e71d2fefef7127aa85ee51c82d1/src/extensions/sentry-integration.ts) with modifications for the Node SDK.
3
3
  */
4
4
  import { type PostHog } from '../posthog-node';
5
- declare type _SentryEventProcessor = any;
6
- declare type _SentryHub = any;
5
+ type _SentryEventProcessor = any;
6
+ type _SentryHub = any;
7
7
  interface _SentryIntegration {
8
8
  name: string;
9
9
  setupOnce(addGlobalEventProcessor: (callback: _SentryEventProcessor) => void, getCurrentHub: () => _SentryHub): void;
@@ -7,7 +7,7 @@ declare class ClientError extends Error {
7
7
  declare class InconclusiveMatchError extends Error {
8
8
  constructor(message: string);
9
9
  }
10
- declare type FeatureFlagsPollerOptions = {
10
+ type FeatureFlagsPollerOptions = {
11
11
  personalApiKey: string;
12
12
  projectApiKey: string;
13
13
  host: string;
@@ -39,6 +39,7 @@ declare class FeatureFlagsPoller {
39
39
  };
40
40
  constructor({ pollingInterval, personalApiKey, projectApiKey, timeout, host, customHeaders, ...options }: FeatureFlagsPollerOptions);
41
41
  debug(enabled?: boolean): void;
42
+ private logMsgIfDebug;
42
43
  getFeatureFlag(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<string | boolean | undefined>;
43
44
  computeFeatureFlagPayloadLocally(key: string, matchValue: string | boolean): Promise<JsonType | undefined>;
44
45
  getAllFlagsAndPayloads(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<{
@@ -7,6 +7,6 @@
7
7
  * See https://github.com/PostHog/posthog-js-lite/issues/127 for more info
8
8
  */
9
9
  import { PostHogFetchOptions, PostHogFetchResponse } from 'posthog-core/src';
10
- declare type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
10
+ type FetchLike = (url: string, options: PostHogFetchOptions) => Promise<PostHogFetchResponse>;
11
11
  declare const _default: FetchLike;
12
12
  export default _default;
@@ -1,6 +1,6 @@
1
1
  import { JsonType, PostHogCoreOptions, PostHogCoreStateless, PostHogFetchOptions, PostHogFetchResponse, PostHogFlagsAndPayloadsResponse, PostHogPersistedProperty } from '../../posthog-core/src';
2
2
  import { EventMessage, GroupIdentifyMessage, IdentifyMessage, PostHogNodeV1 } from './types';
3
- export declare type PostHogOptions = PostHogCoreOptions & {
3
+ export type PostHogOptions = PostHogCoreOptions & {
4
4
  persistence?: 'memory';
5
5
  personalApiKey?: string;
6
6
  featureFlagsPollingInterval?: number;
@@ -18,23 +18,23 @@ export interface GroupIdentifyMessage {
18
18
  distinctId?: string;
19
19
  disableGeoip?: boolean;
20
20
  }
21
- export declare type PropertyGroup = {
21
+ export type PropertyGroup = {
22
22
  type: 'AND' | 'OR';
23
23
  values: PropertyGroup[] | FlagProperty[];
24
24
  };
25
- export declare type FlagProperty = {
25
+ export type FlagProperty = {
26
26
  key: string;
27
27
  type?: string;
28
28
  value: string | number | (string | number)[];
29
29
  operator?: string;
30
30
  negation?: boolean;
31
31
  };
32
- export declare type FeatureFlagCondition = {
32
+ export type FeatureFlagCondition = {
33
33
  properties: FlagProperty[];
34
34
  rollout_percentage?: number;
35
35
  variant?: string;
36
36
  };
37
- export declare type PostHogFeatureFlag = {
37
+ export type PostHogFeatureFlag = {
38
38
  id: number;
39
39
  name: string;
40
40
  key: string;
@@ -56,7 +56,7 @@ export declare type PostHogFeatureFlag = {
56
56
  ensure_experience_continuity: boolean;
57
57
  experiment_set: number[];
58
58
  };
59
- export declare type PostHogNodeV1 = {
59
+ export type PostHogNodeV1 = {
60
60
  /**
61
61
  * @description Capture allows you to capture anything a user does within your system,
62
62
  * which you can later use in PostHog to find patterns in usage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-node",
3
- "version": "4.2.0",
3
+ "version": "4.2.2",
4
4
  "description": "PostHog Node.js integration",
5
5
  "repository": {
6
6
  "type": "git",
@@ -90,6 +90,12 @@ class FeatureFlagsPoller {
90
90
  this.debugMode = enabled
91
91
  }
92
92
 
93
+ private logMsgIfDebug(fn: () => void): void {
94
+ if (this.debugMode) {
95
+ fn()
96
+ }
97
+ }
98
+
93
99
  async getFeatureFlag(
94
100
  key: string,
95
101
  distinctId: string,
@@ -99,7 +105,7 @@ class FeatureFlagsPoller {
99
105
  ): Promise<string | boolean | undefined> {
100
106
  await this.loadFeatureFlags()
101
107
 
102
- let response = undefined
108
+ let response: string | boolean | undefined = undefined
103
109
  let featureFlag = undefined
104
110
 
105
111
  if (!this.loadedSuccessfullyOnce) {
@@ -116,14 +122,10 @@ class FeatureFlagsPoller {
116
122
  if (featureFlag !== undefined) {
117
123
  try {
118
124
  response = this.computeFlagLocally(featureFlag, distinctId, groups, personProperties, groupProperties)
119
- if (this.debugMode) {
120
- console.debug(`Successfully computed flag locally: ${key} -> ${response}`)
121
- }
125
+ this.logMsgIfDebug(() => console.debug(`Successfully computed flag locally: ${key} -> ${response}`))
122
126
  } catch (e) {
123
127
  if (e instanceof InconclusiveMatchError) {
124
- if (this.debugMode) {
125
- console.debug(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`)
126
- }
128
+ this.logMsgIfDebug(() => console.debug(`InconclusiveMatchError when computing flag locally: ${key}: ${e}`))
127
129
  } else if (e instanceof Error) {
128
130
  this.onError?.(new Error(`Error computing flag locally: ${key}: ${e}`))
129
131
  }
@@ -219,18 +221,18 @@ class FeatureFlagsPoller {
219
221
  const groupName = this.groupTypeMapping[String(aggregation_group_type_index)]
220
222
 
221
223
  if (!groupName) {
222
- if (this.debugMode) {
224
+ this.logMsgIfDebug(() =>
223
225
  console.warn(
224
226
  `[FEATURE FLAGS] Unknown group type index ${aggregation_group_type_index} for feature flag ${flag.key}`
225
227
  )
226
- }
228
+ )
227
229
  throw new InconclusiveMatchError('Flag has unknown group type index')
228
230
  }
229
231
 
230
232
  if (!(groupName in groups)) {
231
- if (this.debugMode) {
233
+ this.logMsgIfDebug(() =>
232
234
  console.warn(`[FEATURE FLAGS] Can't compute group feature flag: ${flag.key} without group names passed in`)
233
- }
235
+ )
234
236
  return false
235
237
  }
236
238
 
@@ -307,9 +309,7 @@ class FeatureFlagsPoller {
307
309
  ): boolean {
308
310
  const rolloutPercentage = condition.rollout_percentage
309
311
  const warnFunction = (msg: string): void => {
310
- if (this.debugMode) {
311
- console.warn(msg)
312
- }
312
+ this.logMsgIfDebug(() => console.warn(msg))
313
313
  }
314
314
  if ((condition.properties || []).length > 0) {
315
315
  for (const prop of condition.properties) {
@@ -39,7 +39,6 @@ export class PostHog extends PostHogCoreStateless implements PostHogNodeV1 {
39
39
  distinctIdHasSentFlagCalls: Record<string, string[]>
40
40
 
41
41
  constructor(apiKey: string, options: PostHogOptions = {}) {
42
- options.captureMode = options?.captureMode || 'json'
43
42
  super(apiKey, options)
44
43
 
45
44
  this.options = options