1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/dashboard/client/utils/createStorage.ts
Sebastian Malton b1ff34879a cleanup Lens repo with tighter linting
Signed-off-by: Sebastian Malton <smalton@mirantis.com>
2020-07-09 17:00:23 -04:00

66 lines
1.6 KiB
TypeScript
Executable File

// Helper to work with browser's local/session storage api
export interface StorageHelperOptions {
addKeyPrefix?: boolean;
useSession?: boolean; // use `sessionStorage` instead of `localStorage`
}
export class StorageHelper<T> {
static keyPrefix = "lens_";
static defaultOptions: StorageHelperOptions = {
addKeyPrefix: true,
useSession: false,
}
constructor(protected key: string, protected defaultValue?: T, protected options?: StorageHelperOptions) {
this.options = Object.assign({}, StorageHelper.defaultOptions, options);
if (this.options.addKeyPrefix) {
this.key = StorageHelper.keyPrefix + key;
}
}
protected get storage(): Storage {
if (this.options.useSession) {
return window.sessionStorage;
}
return window.localStorage;
}
get(): T {
const strValue = this.storage.getItem(this.key);
if (strValue != null) {
try {
return JSON.parse(strValue);
} catch (e) {
console.error(`Parsing json failed for pair: ${this.key}=${strValue}`);
}
}
return this.defaultValue;
}
set(value: T): StorageHelper<T> {
this.storage.setItem(this.key, JSON.stringify(value));
return this;
}
merge(value: Partial<T>): StorageHelper<T> {
const currentValue = this.get();
return this.set(Object.assign(currentValue, value));
}
clear(): StorageHelper<T> {
this.storage.removeItem(this.key);
return this;
}
getDefaultValue(): T {
return this.defaultValue;
}
restoreDefaultValue(): StorageHelper<T> {
return this.set(this.defaultValue);
}
}