posthog-js-lite
Advanced tools
Comparing version 2.0.0-alpha4 to 2.0.0-alpha5
@@ -1128,3 +1128,3 @@ 'use strict'; | ||
var version = "2.0.0-alpha4"; | ||
var version = "2.0.0-alpha5"; | ||
@@ -1420,2 +1420,51 @@ function getContext(window) { | ||
var createMemoryStorage = function () { | ||
var _cache = {}; | ||
var store = { | ||
getItem: function (key) { | ||
return _cache[key]; | ||
}, | ||
setItem: function (key, value) { | ||
_cache[key] = value !== null ? value : undefined; | ||
}, | ||
removeItem: function (key) { | ||
delete _cache[key]; | ||
}, | ||
clear: function () { | ||
for (var key in _cache) { | ||
delete _cache[key]; | ||
} | ||
}, | ||
getAllKeys: function () { | ||
var keys = []; | ||
for (var key in _cache) { | ||
keys.push(key); | ||
} | ||
return keys; | ||
} | ||
}; | ||
return store; | ||
}; | ||
var getStorage = function (type) { | ||
switch (type) { | ||
case 'cookie': | ||
return cookieStore || localStore || sessionStorage || createMemoryStorage(); | ||
case 'localStorage': | ||
return localStore || sessionStorage || createMemoryStorage(); | ||
case 'sessionStorage': | ||
return sessionStorage || createMemoryStorage(); | ||
case 'memory': | ||
return createMemoryStorage(); | ||
default: | ||
return createMemoryStorage(); | ||
} | ||
}; | ||
var PostHog = | ||
@@ -1427,7 +1476,7 @@ /** @class */ | ||
function PostHog(apiKey, options) { | ||
var _this = _super.call(this, apiKey, options) || this; | ||
var _this = _super.call(this, apiKey, options) || this; // posthog-js stores options in one object on | ||
_this._storage = localStore || sessionStorage || cookieStore; // posthog-js stores options in one object on | ||
_this._storageKey = (options === null || options === void 0 ? void 0 : options.persistence_name) ? "ph_".concat(options.persistence_name) : "ph_".concat(apiKey, "_posthog"); | ||
_this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage'); | ||
return _this; | ||
@@ -1434,0 +1483,0 @@ } |
@@ -162,6 +162,8 @@ declare type PosthogCoreOptions = { | ||
interface PostHogOptions extends PosthogCoreOptions { | ||
declare type PostHogOptions = { | ||
autocapture?: boolean; | ||
persistence?: 'localStorage' | 'sessionStorage' | 'cookie' | 'memory'; | ||
persistence_name?: string; | ||
} | ||
} & PosthogCoreOptions; | ||
declare class PostHog extends PostHogCore { | ||
@@ -181,2 +183,2 @@ private _storage; | ||
export { PostHog, PostHogOptions, PostHog as default }; | ||
export { PostHog, PostHog as default }; |
@@ -1124,3 +1124,3 @@ /****************************************************************************** | ||
var version = "2.0.0-alpha4"; | ||
var version = "2.0.0-alpha5"; | ||
@@ -1416,2 +1416,51 @@ function getContext(window) { | ||
var createMemoryStorage = function () { | ||
var _cache = {}; | ||
var store = { | ||
getItem: function (key) { | ||
return _cache[key]; | ||
}, | ||
setItem: function (key, value) { | ||
_cache[key] = value !== null ? value : undefined; | ||
}, | ||
removeItem: function (key) { | ||
delete _cache[key]; | ||
}, | ||
clear: function () { | ||
for (var key in _cache) { | ||
delete _cache[key]; | ||
} | ||
}, | ||
getAllKeys: function () { | ||
var keys = []; | ||
for (var key in _cache) { | ||
keys.push(key); | ||
} | ||
return keys; | ||
} | ||
}; | ||
return store; | ||
}; | ||
var getStorage = function (type) { | ||
switch (type) { | ||
case 'cookie': | ||
return cookieStore || localStore || sessionStorage || createMemoryStorage(); | ||
case 'localStorage': | ||
return localStore || sessionStorage || createMemoryStorage(); | ||
case 'sessionStorage': | ||
return sessionStorage || createMemoryStorage(); | ||
case 'memory': | ||
return createMemoryStorage(); | ||
default: | ||
return createMemoryStorage(); | ||
} | ||
}; | ||
var PostHog = | ||
@@ -1423,7 +1472,7 @@ /** @class */ | ||
function PostHog(apiKey, options) { | ||
var _this = _super.call(this, apiKey, options) || this; | ||
var _this = _super.call(this, apiKey, options) || this; // posthog-js stores options in one object on | ||
_this._storage = localStore || sessionStorage || cookieStore; // posthog-js stores options in one object on | ||
_this._storageKey = (options === null || options === void 0 ? void 0 : options.persistence_name) ? "ph_".concat(options.persistence_name) : "ph_".concat(apiKey, "_posthog"); | ||
_this._storage = getStorage((options === null || options === void 0 ? void 0 : options.persistence) || 'localStorage'); | ||
return _this; | ||
@@ -1430,0 +1479,0 @@ } |
@@ -1,6 +0,3 @@ | ||
import { PostHogCore, PosthogCoreOptions, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty } from '../../posthog-core/src'; | ||
export interface PostHogOptions extends PosthogCoreOptions { | ||
autocapture?: boolean; | ||
persistence_name?: string; | ||
} | ||
import { PostHogCore, PostHogFetchOptions, PostHogFetchResponse, PostHogPersistedProperty } from '../../posthog-core/src'; | ||
import { PostHogOptions } from './types'; | ||
export declare class PostHog extends PostHogCore { | ||
@@ -7,0 +4,0 @@ private _storage; |
@@ -0,1 +1,2 @@ | ||
import { PostHogOptions } from './types'; | ||
export declare type PostHogStorage = { | ||
@@ -13,1 +14,2 @@ getItem: (key: string) => string | null | undefined; | ||
export declare const sessionStorage: PostHogStorage | undefined; | ||
export declare const getStorage: (type: PostHogOptions['persistence']) => PostHogStorage; |
{ | ||
"name": "posthog-js-lite", | ||
"version": "2.0.0-alpha4", | ||
"version": "2.0.0-alpha5", | ||
"main": "lib/index.cjs.js", | ||
@@ -5,0 +5,0 @@ "module": "lib/index.esm.js", |
import { | ||
PostHogCore, | ||
PosthogCoreOptions, | ||
PostHogFetchOptions, | ||
@@ -9,12 +8,8 @@ PostHogFetchResponse, | ||
import { getContext } from './context' | ||
import { localStore, cookieStore, sessionStorage } from './storage' | ||
import { PostHogStorage, getStorage } from './storage' | ||
import { version } from '../package.json' | ||
import { PostHogOptions } from './types' | ||
export interface PostHogOptions extends PosthogCoreOptions { | ||
autocapture?: boolean | ||
persistence_name?: string | ||
} | ||
export class PostHog extends PostHogCore { | ||
private _storage = localStore || sessionStorage || cookieStore | ||
private _storage: PostHogStorage | ||
private _storageCache: any | ||
@@ -28,2 +23,3 @@ private _storageKey: string | ||
this._storageKey = options?.persistence_name ? `ph_${options.persistence_name}` : `ph_${apiKey}_posthog` | ||
this._storage = getStorage(options?.persistence || 'localStorage') | ||
} | ||
@@ -30,0 +26,0 @@ |
@@ -0,1 +1,3 @@ | ||
import { PostHogOptions } from './types' | ||
export type PostHogStorage = { | ||
@@ -115,1 +117,47 @@ getItem: (key: string) => string | null | undefined | ||
export const sessionStorage = checkStoreIsSupported(_sessionStore) ? _sessionStore : undefined | ||
const createMemoryStorage = (): PostHogStorage => { | ||
const _cache: { [key: string]: any | undefined } = {} | ||
const store: PostHogStorage = { | ||
getItem(key) { | ||
return _cache[key] | ||
}, | ||
setItem(key, value) { | ||
_cache[key] = value !== null ? value : undefined | ||
}, | ||
removeItem(key) { | ||
delete _cache[key] | ||
}, | ||
clear() { | ||
for (const key in _cache) { | ||
delete _cache[key] | ||
} | ||
}, | ||
getAllKeys() { | ||
const keys = [] | ||
for (const key in _cache) { | ||
keys.push(key) | ||
} | ||
return keys | ||
}, | ||
} | ||
return store | ||
} | ||
export const getStorage = (type: PostHogOptions['persistence']): PostHogStorage => { | ||
switch (type) { | ||
case 'cookie': | ||
return cookieStore || localStore || sessionStorage || createMemoryStorage() | ||
case 'localStorage': | ||
return localStore || sessionStorage || createMemoryStorage() | ||
case 'sessionStorage': | ||
return sessionStorage || createMemoryStorage() | ||
case 'memory': | ||
return createMemoryStorage() | ||
default: | ||
return createMemoryStorage() | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
385421
25
3740