@klevu/core
Version:
Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.
76 lines (75 loc) • 2.53 kB
JavaScript
;
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.KlevuStorage = exports.StorageType = void 0;
const index_js_1 = require("../index.js");
var StorageType;
(function (StorageType) {
StorageType["SESSION"] = "session";
StorageType["LOCAL"] = "local";
})(StorageType || (exports.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);
};
class KlevuStorage {
static listKeys() {
return this.dataProtectedKeys;
}
}
exports.KlevuStorage = KlevuStorage;
_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 &&
index_js_1.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 &&
index_js_1.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 &&
index_js_1.KlevuConfig.getDefault().isConsentDisallowed()) {
return;
}
if (isLocalStorage(storageType)) {
return window.localStorage.removeItem(key);
}
if (isSessionStorage(storageType)) {
return window.sessionStorage.removeItem(key);
}
};