posthog-js-lite 2.2.1 → 2.4.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.
@@ -11,6 +11,9 @@ export declare abstract class PostHogCoreStateless {
11
11
  private requestTimeout;
12
12
  private captureMode;
13
13
  private removeDebugCallback?;
14
+ private debugMode;
15
+ private pendingPromises;
16
+ private disableGeoip;
14
17
  private _optoutOverride;
15
18
  protected _events: SimpleEventEmitter;
16
19
  protected _flushTimer?: any;
@@ -38,7 +41,7 @@ export declare abstract class PostHogCoreStateless {
38
41
  }, options?: PosthogCaptureOptions): this;
39
42
  protected aliasStateless(alias: string, distinctId: string, properties?: {
40
43
  [key: string]: any;
41
- }): this;
44
+ }, options?: PosthogCaptureOptions): this;
42
45
  /***
43
46
  *** GROUPS
44
47
  ***/
@@ -47,12 +50,12 @@ export declare abstract class PostHogCoreStateless {
47
50
  *** FEATURE FLAGS
48
51
  ***/
49
52
  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>;
50
- protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<boolean | string | undefined>;
51
- protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<JsonType | undefined>;
52
- protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
53
+ protected getFeatureFlagStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<boolean | string | undefined>;
54
+ protected getFeatureFlagPayloadStateless(key: string, distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<JsonType | undefined>;
55
+ protected getFeatureFlagPayloadsStateless(distinctId: string, groups?: Record<string, string>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlagPayloads'] | undefined>;
53
56
  protected _parsePayload(response: any): any;
54
- protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
55
- protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>): Promise<{
57
+ protected getFeatureFlagsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<PostHogDecideResponse['featureFlags'] | undefined>;
58
+ protected getFeatureFlagsAndPayloadsStateless(distinctId: string, groups?: Record<string, string | number>, personProperties?: Record<string, string>, groupProperties?: Record<string, Record<string, string>>, disableGeoip?: boolean): Promise<{
56
59
  flags: PostHogDecideResponse['featureFlags'] | undefined;
57
60
  payloads: PostHogDecideResponse['featureFlagPayloads'] | undefined;
58
61
  }>;
@@ -16,6 +16,7 @@ export declare type PosthogCoreOptions = {
16
16
  requestTimeout?: number;
17
17
  sessionExpirationTimeSeconds?: number;
18
18
  captureMode?: 'json' | 'form';
19
+ disableGeoip?: boolean;
19
20
  };
20
21
  export declare enum PostHogPersistedProperty {
21
22
  AnonymousId = "anonymous_id",
@@ -43,6 +44,7 @@ export declare type PostHogFetchOptions = {
43
44
  };
44
45
  export declare type PosthogCaptureOptions = {
45
46
  timestamp?: Date;
47
+ disableGeoip?: boolean;
46
48
  };
47
49
  export declare type PostHogFetchResponse = {
48
50
  status: number;
@@ -3,7 +3,7 @@ export declare function removeTrailingSlash(url: string): string;
3
3
  export interface RetriableOptions {
4
4
  retryCount?: number;
5
5
  retryDelay?: number;
6
- retryCheck?: (err: any) => true;
6
+ retryCheck?: (err: any) => boolean;
7
7
  }
8
8
  export declare function retriable<T>(fn: () => Promise<T>, props?: RetriableOptions): Promise<T>;
9
9
  export declare function generateUUID(globalThis?: any): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "2.2.1",
3
+ "version": "2.4.0",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,6 +21,10 @@ export class PostHog extends PostHogCore {
21
21
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
22
22
  this._storage = getStorage(options?.persistence || 'localStorage', window)
23
23
  this.setupBootstrap(options)
24
+
25
+ if (options?.preloadFeatureFlags !== false) {
26
+ this.reloadFeatureFlags()
27
+ }
24
28
  }
25
29
 
26
30
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
@@ -6,27 +6,89 @@ import { PostHog } from '..'
6
6
 
7
7
  describe('PosthogWeb', () => {
8
8
  let fetch: jest.Mock
9
- jest.useFakeTimers()
9
+ jest.useRealTimers()
10
10
 
11
11
  beforeEach(() => {
12
- ;(global as any).window.fetch = fetch = jest.fn(() =>
13
- Promise.resolve({
12
+ ;(global as any).window.fetch = fetch = jest.fn(async (url) => {
13
+ let res: any = { status: 'ok' }
14
+ if (url.includes('decide')) {
15
+ res = {
16
+ featureFlags: {
17
+ 'feature-1': true,
18
+ 'feature-2': true,
19
+ 'json-payload': true,
20
+ 'feature-variant': 'variant',
21
+ },
22
+ // NOTE: Aren't these supposed to be strings?
23
+
24
+ featureFlagPayloads: {
25
+ 'feature-1': {
26
+ color: 'blue',
27
+ },
28
+ 'json-payload': {
29
+ a: 'payload',
30
+ },
31
+ 'feature-variant': 5,
32
+ },
33
+ }
34
+ }
35
+
36
+ return {
14
37
  status: 200,
15
- json: () => Promise.resolve({ status: 'ok' }),
16
- })
17
- ) as any
38
+ json: () => Promise.resolve(res),
39
+ }
40
+ })
18
41
  })
19
42
 
20
43
  describe('init', () => {
21
44
  it('should initialise', () => {
22
- const postHog = new PostHog('TEST_API_KEY', {
45
+ const posthog = new PostHog('TEST_API_KEY', {
46
+ flushAt: 1,
47
+ })
48
+ expect(posthog.optedOut).toEqual(false)
49
+
50
+ posthog.capture('test')
51
+
52
+ expect(fetch).toHaveBeenCalledTimes(2)
53
+ })
54
+
55
+ it('should load feature flags on init', async () => {
56
+ const posthog = new PostHog('TEST_API_KEY', {
23
57
  flushAt: 1,
24
58
  })
25
- expect(postHog.optedOut).toEqual(false)
26
59
 
27
- postHog.capture('test')
60
+ expect(fetch).toHaveBeenCalledWith('https://app.posthog.com/decide/?v=3', {
61
+ body: JSON.stringify({
62
+ token: 'TEST_API_KEY',
63
+ distinct_id: posthog.getDistinctId(),
64
+ groups: {},
65
+ person_properties: {},
66
+ group_properties: {},
67
+ $anon_distinct_id: posthog.getAnonymousId(),
68
+ }),
69
+ method: 'POST',
70
+ headers: {
71
+ 'Content-Type': 'application/json',
72
+ },
73
+ signal: expect.anything(),
74
+ })
28
75
 
29
- expect(fetch).toHaveBeenCalledTimes(1)
76
+ expect(posthog.getFeatureFlags()).toEqual({
77
+ 'feature-1': true,
78
+ 'feature-2': true,
79
+ 'json-payload': true,
80
+ 'feature-variant': 'variant',
81
+ })
82
+
83
+ expect(posthog.getFeatureFlagPayloads()).toEqual({
84
+ 'feature-1': {
85
+ color: 'blue',
86
+ },
87
+ 'json-payload': {
88
+ a: 'payload',
89
+ },
90
+ 'feature-variant': 5,
91
+ })
30
92
  })
31
93
  })
32
94
  })
package/tsconfig.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "extends": "../tsconfig.json",
3
3
  "compilerOptions": {
4
- "lib": ["DOM"]
4
+ "lib": ["DOM", "ES2015", "ES2022.Error"]
5
5
  }
6
6
  }