move IAPIObject to different module
This commit is contained in:
parent
c618c361b8
commit
5604c20903
|
@ -0,0 +1,57 @@
|
|||
|
||||
/**
|
||||
* Basic API interaction & local caching interface
|
||||
*/
|
||||
class IAPIObject {
|
||||
/**
|
||||
* Local storage key used to save data.
|
||||
* @type {string}
|
||||
*/
|
||||
static storage_key = undefined;
|
||||
|
||||
static _checkStorageSupport() {
|
||||
if (this.storage_key === undefined) {
|
||||
throw Error('This doesn\'t support local storage');
|
||||
}
|
||||
}
|
||||
|
||||
_getStorageKey() {
|
||||
return Object.getPrototypeOf(this).constructor.storage_key;
|
||||
}
|
||||
|
||||
_IcheckStorageSupport() {
|
||||
if (this._getStorageKey() === undefined) {
|
||||
throw Error('This doesn\'t support local storage');
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {ThisType} */
|
||||
static restoreFromLocalStorage() {
|
||||
_checkStorageSupport();
|
||||
if (!window.sessionStorage.getItem(this.storage_key))
|
||||
return false;
|
||||
return new APIToken(window.sessionStorage.getItem(this.storage_key));
|
||||
}
|
||||
|
||||
/** @returns {boolean} */
|
||||
static isCached() {
|
||||
this._checkStorageSupport();
|
||||
if (window.sessionStorage.getItem(this.storage_key)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save this object to local storage
|
||||
* @throws {QuotaExceededError}
|
||||
* @returns {void}
|
||||
*/
|
||||
saveToLocalStorage() {
|
||||
this._IcheckStorageSupport();
|
||||
|
||||
window.sessionStorage.setItem(this._getStorageKey(), JSON.stringify(this));
|
||||
}
|
||||
}
|
||||
|
||||
export { IAPIObject };
|
|
@ -1,6 +1,8 @@
|
|||
import axios from 'axios';
|
||||
import constants from '../constants';
|
||||
|
||||
import { IAPIObject } from './IAPIObject';
|
||||
|
||||
const { API_ROOT, api_path } = constants;
|
||||
|
||||
class UserLoginResponse {
|
||||
|
@ -14,58 +16,6 @@ class UserLoginResponse {
|
|||
id;
|
||||
}
|
||||
|
||||
class IAPIObject {
|
||||
/**
|
||||
* Local storage key used to save data.
|
||||
* @type {string}
|
||||
*/
|
||||
static storage_key = undefined;
|
||||
|
||||
static _checkStorageSupport() {
|
||||
if (this.storage_key === undefined) {
|
||||
throw Error('This doesn\'t support local storage');
|
||||
}
|
||||
}
|
||||
|
||||
_getStorageKey() {
|
||||
return Object.getPrototypeOf(this).constructor.storage_key;
|
||||
}
|
||||
|
||||
_IcheckStorageSupport() {
|
||||
if (this._getStorageKey() === undefined) {
|
||||
throw Error('This doesn\'t support local storage');
|
||||
}
|
||||
}
|
||||
|
||||
/** @returns {ThisType} */
|
||||
static restoreFromLocalStorage() {
|
||||
_checkStorageSupport();
|
||||
if (!window.sessionStorage.getItem(this.storage_key))
|
||||
return false;
|
||||
return new APIToken(window.sessionStorage.getItem(this.storage_key));
|
||||
}
|
||||
|
||||
/** @returns {boolean} */
|
||||
static isCached() {
|
||||
this._checkStorageSupport();
|
||||
if (window.sessionStorage.getItem(this.storage_key)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save this object to local storage
|
||||
* @throws {QuotaExceededError}
|
||||
* @returns {void}
|
||||
*/
|
||||
saveToLocalStorage() {
|
||||
this._IcheckStorageSupport();
|
||||
|
||||
window.sessionStorage.setItem(this._getStorageKey(), JSON.stringify(this));
|
||||
}
|
||||
}
|
||||
|
||||
class APIToken extends IAPIObject {
|
||||
|
||||
static storage_key = 'pairent_api_key';
|
||||
|
|
Loading…
Reference in New Issue