interface Options { ns: string st: 'localStorage' | 'sessionStorage' } export default class Storage { private ns: string private st constructor ({ ns = 'iot', st = 'localStorage' } = {} as Options) { this.ns = `hz_${location.pathname}@${ns}:` this.st = window[st] } async get (key) { let ret try { ret = JSON.parse(this.st[`${this.ns}${key}`]) } catch (e) { // } return ret || null } async set (key, value) { try { if (value == null) { value = null } this.st[`${this.ns}${key}`] = JSON.stringify(value) } catch (e) { // } return this.get(key) } async remove (key) { delete this.st[`${this.ns}${key}`] } } const localStorage = new Storage() export { localStorage }