posthog-js-lite 2.0.0-alpha4 → 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.
- package/lib/index.cjs.js +52 -3
- package/lib/index.cjs.js.map +1 -1
- package/lib/index.d.ts +5 -3
- package/lib/index.esm.js +52 -3
- package/lib/index.esm.js.map +1 -1
- package/lib/posthog-web/src/posthog-web.d.ts +2 -5
- package/lib/posthog-web/src/storage.d.ts +2 -0
- package/lib/posthog-web/src/types.d.ts +6 -0
- package/package.json +1 -1
- package/src/posthog-web.ts +4 -8
- package/src/storage.ts +48 -0
- package/src/types.ts +7 -0
package/package.json
CHANGED
package/src/posthog-web.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
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 {
|
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
|
|
@@ -112,3 +114,49 @@ const checkStoreIsSupported = (storage: PostHogStorage, key = '__mplssupport__')
|
|
|
112
114
|
|
|
113
115
|
export const localStore = checkStoreIsSupported(_localStore) ? _localStore : undefined
|
|
114
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