Files
storage-service-ui/src/api/LocalStorageAPI.js

40 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {LOCAL_STORAGE_TYPE} from './consts';
/**
* Класс работы с Local Storage браузера
* @class LocalStorageAPI
* @param {string} key - уникальный ключ для local или session storage
* @param {'LOCAL' | 'SESSION'} type - тип storage
*/
class LocalStorageAPI {
constructor (key, type = LOCAL_STORAGE_TYPE.LOCAL) {
this.key = key;
this.api = type === LOCAL_STORAGE_TYPE.LOCAL ? localStorage : sessionStorage;
}
/**
* Возвращает распарсенный объект из Local Storage по ключу из конструктора
*/
request () {
const value = this.api.getItem(this.key) || 'null';
return JSON.parse(value);
}
/**
* Записывает данные в Local Storage по ключу из конструктора
* @param {Object} value - значение в Local Storage
*/
createOrUpdate (value) {
this.api.setItem(this.key, JSON.stringify(value));
}
/**
* Очищает значение Local Storage по ключу из конструктора
*/
remove () {
this.api.removeItem(this.key);
}
}
export default LocalStorageAPI;