posthog-js-lite 2.0.0-alpha2 → 2.0.0-alpha5

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,7 @@ export declare abstract class PostHogCore {
11
11
  private captureMode;
12
12
  private sendFeatureFlagEvent;
13
13
  private flagCallReported;
14
+ private removeDebugCallback?;
14
15
  protected _events: SimpleEventEmitter;
15
16
  protected _flushTimer?: any;
16
17
  protected _decideResponsePromise?: Promise<PostHogDecideResponse>;
@@ -33,10 +34,12 @@ export declare abstract class PostHogCore {
33
34
  get optedOut(): boolean;
34
35
  optIn(): void;
35
36
  optOut(): void;
36
- on(event: string, cb: (e: any) => void): () => void;
37
+ on(event: string, cb: (...args: any[]) => void): () => void;
37
38
  reset(): void;
39
+ debug(enabled?: boolean): void;
38
40
  private buildPayload;
39
41
  getSessionId(): string | undefined;
42
+ resetSessionId(): void;
40
43
  getDistinctId(): string;
41
44
  register(properties: {
42
45
  [key: string]: any;
@@ -1,8 +1,5 @@
1
- import { PostHogCore, PosthogCoreOptions, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty } from '../../posthog-core/src';
2
- export interface PostHogOptions extends PosthogCoreOptions {
3
- autocapture?: boolean;
4
- persistence_name?: string;
5
- }
1
+ import { PostHogCore, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty } from '../../posthog-core/src';
2
+ import { PostHogOptions } from './types';
6
3
  export declare class PostHog extends PostHogCore {
7
4
  private _storage;
8
5
  private _storageCache;
@@ -1,3 +1,4 @@
1
+ import { PostHogOptions } from './types';
1
2
  export declare type PostHogStorage = {
2
3
  getItem: (key: string) => string | null | undefined;
3
4
  setItem: (key: string, value: string) => void;
@@ -10,3 +11,4 @@ export declare const _localStore: PostHogStorage;
10
11
  export declare const _sessionStore: PostHogStorage;
11
12
  export declare const localStore: PostHogStorage | undefined;
12
13
  export declare const sessionStorage: PostHogStorage | undefined;
14
+ export declare const getStorage: (type: PostHogOptions['persistence']) => PostHogStorage;
@@ -0,0 +1,6 @@
1
+ import { PosthogCoreOptions } from '../../posthog-core/src';
2
+ export declare type PostHogOptions = {
3
+ autocapture?: boolean;
4
+ persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory';
5
+ persistence_name?: string;
6
+ } & PosthogCoreOptions;
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "posthog-js-lite",
3
- "version": "2.0.0-alpha2",
3
+ "version": "2.0.0-alpha5",
4
4
  "main": "lib/index.cjs.js",
5
5
  "module": "lib/index.esm.js",
6
- "types": "lib/index.d.ts"
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "test": "jest -c jest.config.js",
9
+ "prepublish": "cd .. && yarn build"
10
+ }
7
11
  }
package/src/context.ts CHANGED
@@ -1,8 +1,5 @@
1
1
  import { utils } from '../../posthog-core'
2
- // import { version } from '../package.json'
3
-
4
- // TODO: Get this from package.json
5
- const version = '2.0.0-alpha'
2
+ import { version } from '../package.json'
6
3
 
7
4
  export function getContext(window: Window): any {
8
5
  let context = {}
@@ -1,21 +1,16 @@
1
1
  import {
2
2
  PostHogCore,
3
- PosthogCoreOptions,
4
3
  PostHogFetchOptions,
5
4
  PostHogFetchResponse,
6
5
  PostHogPersistedProperty,
7
6
  } from '../../posthog-core/src'
8
7
  import { getContext } from './context'
9
- import { localStore, cookieStore, sessionStorage } from './storage'
8
+ import { PostHogStorage, getStorage } from './storage'
10
9
  import { version } from '../package.json'
11
-
12
- export interface PostHogOptions extends PosthogCoreOptions {
13
- autocapture?: boolean
14
- persistence_name?: string
15
- }
10
+ import { PostHogOptions } from './types'
16
11
 
17
12
  export class PostHog extends PostHogCore {
18
- private _storage = localStore || sessionStorage || cookieStore
13
+ private _storage: PostHogStorage
19
14
  private _storageCache: any
20
15
  private _storageKey: string
21
16
 
@@ -24,6 +19,7 @@ export class PostHog extends PostHogCore {
24
19
 
25
20
  // posthog-js stores options in one object on
26
21
  this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog`
22
+ this._storage = getStorage(options?.persistence || 'localStorage')
27
23
  }
28
24
 
29
25
  getPersistedProperty<T>(key: PostHogPersistedProperty): T | undefined {
@@ -41,18 +37,18 @@ export class PostHog extends PostHogCore {
41
37
 
42
38
  if (value === null) {
43
39
  delete this._storageCache[key]
44
- this._storage.removeItem(this._storageKey)
45
40
  } else {
46
41
  this._storageCache[key] = value
47
- this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))
48
42
  }
43
+
44
+ this._storage.setItem(this._storageKey, JSON.stringify(this._storageCache))
49
45
  }
50
46
 
51
47
  fetch(url: string, options: PostHogFetchOptions): Promise<PostHogFetchResponse> {
52
48
  return window.fetch(url, options)
53
49
  }
54
50
  getLibraryId(): string {
55
- return 'posthog-web'
51
+ return 'posthog-js-lite'
56
52
  }
57
53
  getLibraryVersion(): string {
58
54
  return version
package/src/storage.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { PostHogOptions } from './types'
2
+
1
3
  export type PostHogStorage = {
2
4
  getItem: (key: string) => string | null | undefined
3
5
  setItem: (key: string, value: string) => void
@@ -31,8 +33,7 @@ export const cookieStore: PostHogStorage = {
31
33
  expires = '',
32
34
  secure = ''
33
35
 
34
- const new_cookie_val =
35
- key + '=' + encodeURIComponent(JSON.stringify(value)) + expires + '; path=/' + cdomain + secure
36
+ const new_cookie_val = key + '=' + encodeURIComponent(value) + expires + '; path=/' + cdomain + secure
36
37
  document.cookie = new_cookie_val
37
38
  } catch (err) {
38
39
  return
@@ -72,7 +73,7 @@ const createStorageLike = (store: any): PostHogStorage => {
72
73
  },
73
74
 
74
75
  setItem(key, value) {
75
- store.setItem(key, JSON.stringify(value))
76
+ store.setItem(key, value)
76
77
  },
77
78
 
78
79
  removeItem(key) {
@@ -113,3 +114,49 @@ const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__')
113
114
 
114
115
  export const localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined
115
116
  export const sessionStorage = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined
117
+
118
+ const createMemoryStorage = (): PostHogStorage => {
119
+ const _cache: { [key: string]: any | undefined } = {}
120
+
121
+ const store: PostHogStorage = {
122
+ getItem(key) {
123
+ return _cache[key]
124
+ },
125
+
126
+ setItem(key, value) {
127
+ _cache[key] = value !== null ? value : undefined
128
+ },
129
+
130
+ removeItem(key) {
131
+ delete _cache[key]
132
+ },
133
+ clear() {
134
+ for (const key in _cache) {
135
+ delete _cache[key]
136
+ }
137
+ },
138
+ getAllKeys() {
139
+ const keys = []
140
+ for (const key in _cache) {
141
+ keys.push(key)
142
+ }
143
+ return keys
144
+ },
145
+ }
146
+ return store
147
+ }
148
+
149
+ export const getStorage = (type: PostHogOptions['persistence']): PostHogStorage => {
150
+ switch (type) {
151
+ case 'cookie':
152
+ return cookieStore || localStore || sessionStorage || createMemoryStorage()
153
+ case 'localStorage':
154
+ return localStore || sessionStorage || createMemoryStorage()
155
+ case 'sessionStorage':
156
+ return sessionStorage || createMemoryStorage()
157
+ case 'memory':
158
+ return createMemoryStorage()
159
+ default:
160
+ return createMemoryStorage()
161
+ }
162
+ }
package/src/types.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { PosthogCoreOptions } from '../../posthog-core/src'
2
+
3
+ export type PostHogOptions = {
4
+ autocapture?: boolean
5
+ persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory'
6
+ persistence_name?: string
7
+ } & PosthogCoreOptions