UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

72 lines (71 loc) 2.32 kB
var _a; import { KlevuConfig } from "../index.js"; export var StorageType; (function (StorageType) { StorageType["SESSION"] = "session"; StorageType["LOCAL"] = "local"; })(StorageType || (StorageType = {})); const isSessionStorage = (storageType) => { return (storageType === StorageType.SESSION && typeof window !== "undefined" && window.sessionStorage); }; const isLocalStorage = (storageType) => { return (storageType === StorageType.LOCAL && typeof window !== "undefined" && window.localStorage); }; export class KlevuStorage { static listKeys() { return this.dataProtectedKeys; } } _a = KlevuStorage; KlevuStorage.dataProtectedKeys = []; KlevuStorage.addKey = (key) => { if (!_a.dataProtectedKeys.find((k) => k === key)) _a.dataProtectedKeys.push(key); }; KlevuStorage.removeKey = (key) => { const keyIndex = _a.dataProtectedKeys.indexOf(key); if (keyIndex !== -1) { _a.dataProtectedKeys.splice(keyIndex, 1); } }; KlevuStorage.getItem = (key, storageType = StorageType.LOCAL) => { if (_a.dataProtectedKeys.find((k) => k.includes(key)) !== undefined && KlevuConfig.getDefault().isConsentDisallowed()) { return null; } if (isLocalStorage(storageType)) { return window.localStorage.getItem(key); } if (isSessionStorage(storageType)) { return window.sessionStorage.getItem(key); } return null; }; KlevuStorage.setItem = (key, value, storageType = StorageType.LOCAL) => { if (_a.dataProtectedKeys.find((k) => k.includes(key)) !== undefined && KlevuConfig.getDefault().isConsentDisallowed()) { return; } if (isLocalStorage(storageType)) { return window.localStorage.setItem(key, value); } if (isSessionStorage(storageType)) { return window.sessionStorage.setItem(key, value); } }; KlevuStorage.removeItem = (key, storageType = StorageType.LOCAL) => { if (_a.dataProtectedKeys.find((k) => k.includes(key)) !== undefined && KlevuConfig.getDefault().isConsentDisallowed()) { return; } if (isLocalStorage(storageType)) { return window.localStorage.removeItem(key); } if (isSessionStorage(storageType)) { return window.sessionStorage.removeItem(key); } };