kawkab-frontend
Version:
Kawkab frontend is a frontend library for the Kawkab framework
53 lines (52 loc) • 1.3 kB
JavaScript
export class ClientStorage {
storage;
type;
constructor(type = 'localStorage') {
this.type = type;
if (typeof window !== 'undefined') {
this.storage = this.type === 'sessionStorage' ? window.sessionStorage : window.localStorage;
}
else {
this.storage = {
getItem: () => null,
setItem: () => { },
removeItem: () => { },
clear: () => { },
length: 0,
key: () => null
};
}
}
isSupported() {
try {
const testKey = '__test__';
this.storage.setItem(testKey, testKey);
this.storage.removeItem(testKey);
return true;
}
catch (e) {
return false;
}
}
setItem(key, value) {
if (this.isSupported()) {
this.storage.setItem(key, value);
}
}
getItem(key) {
if (this.isSupported()) {
return this.storage.getItem(key);
}
return null;
}
removeItem(key) {
if (this.isSupported()) {
this.storage.removeItem(key);
}
}
clear() {
if (this.isSupported()) {
this.storage.clear();
}
}
}