move IAPIObject to different module

This commit is contained in:
b1ek 2023-05-17 02:51:09 +10:00
parent c618c361b8
commit 5604c20903
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 59 additions and 52 deletions

View File

@ -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 };

View File

@ -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';