2022-04-14 02:12:38 +03:00
|
|
|
const defaults = {
|
|
|
|
apiBase: `${window.location.origin}/api/v1`,
|
2022-04-25 18:47:27 +03:00
|
|
|
gatewayBase: `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/gateway`,
|
|
|
|
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidHlwZSI6MSwiaWF0IjoxNjUwODQzMjY1LCJleHAiOjE2NTEwMTYwNjV9.ssu-MlMkwKQOcP5nmJ98KbqudcGW5XBYPc_d6et4oxo"
|
2022-04-14 02:12:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const dummyProvider = {
|
|
|
|
_store: {},
|
|
|
|
getItem(key) {
|
|
|
|
return this._store[key];
|
|
|
|
},
|
|
|
|
setItem(key, value) {
|
|
|
|
this._store[key] = value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function getProvider() {
|
2022-04-14 16:56:01 +03:00
|
|
|
return window.localStorage || dummyProvider;
|
2022-04-14 02:12:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getItem(key) {
|
|
|
|
return getProvider().getItem(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setItem(key, value) {
|
|
|
|
return getProvider().setItem(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAuthToken() {
|
|
|
|
return getItem("token");
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setAuthToken(value) {
|
|
|
|
return setItem("token", value);
|
|
|
|
}
|
|
|
|
|
2022-04-19 21:21:55 +03:00
|
|
|
export function apiRoute(fragment) {
|
|
|
|
return `${getItem("apiBase")}/${fragment}`;
|
|
|
|
}
|
|
|
|
|
2022-04-14 02:12:38 +03:00
|
|
|
export function setItemIfNull(key, value) {
|
|
|
|
const provider = getProvider();
|
|
|
|
if (!provider.getItem(key)) {
|
|
|
|
provider.setItem(key, value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function initStorageDefaults() {
|
|
|
|
for (const [k, v] of Object.entries(defaults)) {
|
|
|
|
setItemIfNull(k, v);
|
|
|
|
}
|
|
|
|
}
|