frontend: refactor storage.js
This commit is contained in:
parent
b084de7ffc
commit
b93b610eef
5 changed files with 37 additions and 45 deletions
|
@ -1,5 +1,5 @@
|
|||
import gateway, { GatewayEventType } from "./gateway";
|
||||
import { setAuthToken } from "./storage";
|
||||
import { removeItem, setItem } from "./storage";
|
||||
import { overlayStore } from "./stores";
|
||||
|
||||
function useAuthHandlers() {
|
||||
|
@ -15,12 +15,12 @@ function useAuthHandlers() {
|
|||
|
||||
export function authWithToken(token, shouldUpdate=false) {
|
||||
if (shouldUpdate)
|
||||
setAuthToken(token);
|
||||
setItem("token", token);
|
||||
gateway.init(token);
|
||||
}
|
||||
|
||||
export function logOut() {
|
||||
setAuthToken(null);
|
||||
removeItem("token");
|
||||
gateway.close();
|
||||
gateway.dispatch(GatewayEventType.BadAuth, -1);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import logging from "./logging";
|
||||
import { getAuthToken, getItem } from "./storage";
|
||||
import { getItem } from "./storage";
|
||||
|
||||
export const GatewayErrors = {
|
||||
BAD_PAYLOAD: 4001,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import Main from './components/Main.svelte';
|
||||
import gateway from './gateway';
|
||||
import { getAuthToken, initStorageDefaults } from './storage';
|
||||
import { getItem, init } from './storage';
|
||||
import logging from "./logging";
|
||||
import { authWithToken } from './auth';
|
||||
import { initResponsiveHandlers } from './responsive';
|
||||
|
@ -10,9 +10,9 @@ window.__waffle = {
|
|||
gateway
|
||||
};
|
||||
|
||||
initStorageDefaults();
|
||||
init();
|
||||
initResponsiveHandlers();
|
||||
authWithToken(getAuthToken());
|
||||
authWithToken(getItem("token"));
|
||||
|
||||
// Remove loading screen
|
||||
const loadingElement = document.getElementById("pre--loading-screen");
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { getAuthToken } from "./storage";
|
||||
import { getItem } from "./storage";
|
||||
|
||||
export default async function(method, endpoint, auth=true, body=null) {
|
||||
const options = {
|
||||
|
@ -14,7 +14,7 @@ export default async function(method, endpoint, auth=true, body=null) {
|
|||
}
|
||||
|
||||
if (auth) {
|
||||
const token = getAuthToken();
|
||||
const token = getItem("token");
|
||||
if (token) {
|
||||
options.headers = {
|
||||
...options.headers || {},
|
||||
|
|
|
@ -1,54 +1,46 @@
|
|||
const defaults = {
|
||||
apiBase: `${window.location.origin}/api/v1`,
|
||||
gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`,
|
||||
doAnimations: true
|
||||
doAnimations: true,
|
||||
token: ""
|
||||
};
|
||||
const store = new Map(Object.entries(defaults));
|
||||
const persistentProvider = localStorage;
|
||||
|
||||
const dummyProvider = {
|
||||
_store: {},
|
||||
getItem(key) {
|
||||
return this._store[key];
|
||||
},
|
||||
setItem(key, value) {
|
||||
this._store[key] = value;
|
||||
export function setItem(key, value) {
|
||||
store.set(key, value);
|
||||
if (persistentProvider) {
|
||||
persistentProvider.setItem(key, typeof value === "string" ? value : JSON.stringify(value));
|
||||
}
|
||||
};
|
||||
|
||||
function getProvider() {
|
||||
return window.localStorage || dummyProvider;
|
||||
}
|
||||
|
||||
export function getItem(key) {
|
||||
return getProvider().getItem(key);
|
||||
return store.get(key);
|
||||
}
|
||||
|
||||
export function setItem(key, value) {
|
||||
return getProvider().setItem(key, value);
|
||||
export function removeItem(key) {
|
||||
store.delete(key);
|
||||
if (persistentProvider) {
|
||||
persistentProvider.removeItem(key);
|
||||
}
|
||||
}
|
||||
|
||||
export function getAuthToken() {
|
||||
return getItem("token");
|
||||
}
|
||||
export function init() {
|
||||
if (!persistentProvider)
|
||||
return;
|
||||
|
||||
export function setAuthToken(value) {
|
||||
return setItem("token", value);
|
||||
store.forEach((defaultValue, key) => {
|
||||
const override = persistentProvider.getItem(key);
|
||||
if (override !== null) {
|
||||
try {
|
||||
store.set(key, typeof defaultValue === "string" ? override : JSON.parse(override));
|
||||
} catch (o_O) {
|
||||
console.warn("[Storage]", `init(): An exception was thrown while parsing the value of key "${key}" from _persistentProvider`, o_O);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function apiRoute(fragment) {
|
||||
return `${getItem("apiBase")}/${fragment}`;
|
||||
}
|
||||
|
||||
export function setItemIfNull(key, value) {
|
||||
const provider = getProvider();
|
||||
if (provider.getItem(key) === undefined) {
|
||||
provider.setItem(key, value);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function initStorageDefaults() {
|
||||
for (const [k, v] of Object.entries(defaults)) {
|
||||
setItemIfNull(k, v);
|
||||
}
|
||||
return `${getItem("apiBase")}/${fragment}`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue