moved to API
This commit is contained in:
132
app/app-config.js
Normal file
132
app/app-config.js
Normal file
@@ -0,0 +1,132 @@
|
||||
(function () {
|
||||
const apiBaseUrlStorageKey = "tarot-time-api-base-url";
|
||||
const apiKeyStorageKey = "tarot-time-api-key";
|
||||
const defaultApiBaseUrl = "";
|
||||
|
||||
function normalizeBaseUrl(value) {
|
||||
return String(value || "")
|
||||
.trim()
|
||||
.replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
function normalizeApiKey(value) {
|
||||
return String(value || "").trim();
|
||||
}
|
||||
|
||||
function normalizeConnectionSettings(settings) {
|
||||
return {
|
||||
apiBaseUrl: normalizeBaseUrl(settings?.apiBaseUrl),
|
||||
apiKey: normalizeApiKey(settings?.apiKey)
|
||||
};
|
||||
}
|
||||
|
||||
function persistConnectionSettings(settings) {
|
||||
let didPersist = true;
|
||||
|
||||
try {
|
||||
const params = new URLSearchParams(window.location.search || "");
|
||||
const queryValue = String(params.get("apiBaseUrl") || "").trim();
|
||||
const queryApiKey = String(params.get("apiKey") || params.get("api_key") || "").trim();
|
||||
|
||||
if (queryValue) {
|
||||
window.localStorage.setItem(apiBaseUrlStorageKey, normalizeBaseUrl(queryValue));
|
||||
}
|
||||
|
||||
if (queryApiKey) {
|
||||
window.localStorage.setItem(apiKeyStorageKey, normalizeApiKey(queryApiKey));
|
||||
}
|
||||
|
||||
if (settings.apiBaseUrl) {
|
||||
window.localStorage.setItem(apiBaseUrlStorageKey, settings.apiBaseUrl);
|
||||
} else {
|
||||
window.localStorage.removeItem(apiBaseUrlStorageKey);
|
||||
}
|
||||
|
||||
if (settings.apiKey) {
|
||||
window.localStorage.setItem(apiKeyStorageKey, settings.apiKey);
|
||||
} else {
|
||||
window.localStorage.removeItem(apiKeyStorageKey);
|
||||
}
|
||||
} catch (_error) {
|
||||
didPersist = false;
|
||||
}
|
||||
|
||||
return didPersist;
|
||||
}
|
||||
|
||||
function readConfiguredConnectionSettings() {
|
||||
try {
|
||||
const params = new URLSearchParams(window.location.search || "");
|
||||
const queryValue = String(params.get("apiBaseUrl") || "").trim();
|
||||
const queryApiKey = String(params.get("apiKey") || params.get("api_key") || "").trim();
|
||||
|
||||
if (queryValue) {
|
||||
window.localStorage.setItem(apiBaseUrlStorageKey, normalizeBaseUrl(queryValue));
|
||||
}
|
||||
|
||||
if (queryApiKey) {
|
||||
window.localStorage.setItem(apiKeyStorageKey, normalizeApiKey(queryApiKey));
|
||||
}
|
||||
|
||||
const storedBaseUrl = String(window.localStorage.getItem(apiBaseUrlStorageKey) || "").trim();
|
||||
const storedApiKey = String(window.localStorage.getItem(apiKeyStorageKey) || "").trim();
|
||||
return normalizeConnectionSettings({
|
||||
apiBaseUrl: storedBaseUrl,
|
||||
apiKey: storedApiKey
|
||||
});
|
||||
} catch (_error) {
|
||||
return normalizeConnectionSettings({
|
||||
apiBaseUrl: "",
|
||||
apiKey: ""
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const initialConnectionSettings = readConfiguredConnectionSettings();
|
||||
|
||||
window.TarotAppConfig = {
|
||||
...(window.TarotAppConfig || {}),
|
||||
apiBaseUrl: initialConnectionSettings.apiBaseUrl,
|
||||
apiKey: initialConnectionSettings.apiKey,
|
||||
getApiBaseUrl() {
|
||||
return normalizeBaseUrl(this.apiBaseUrl);
|
||||
},
|
||||
getApiKey() {
|
||||
return normalizeApiKey(this.apiKey);
|
||||
},
|
||||
isConnectionConfigured() {
|
||||
return Boolean(this.getApiBaseUrl());
|
||||
},
|
||||
getConnectionSettings() {
|
||||
return {
|
||||
apiBaseUrl: this.getApiBaseUrl(),
|
||||
apiKey: this.getApiKey()
|
||||
};
|
||||
},
|
||||
updateConnectionSettings(nextSettings = {}) {
|
||||
const previous = this.getConnectionSettings();
|
||||
const current = normalizeConnectionSettings({
|
||||
...previous,
|
||||
...nextSettings
|
||||
});
|
||||
const didPersist = persistConnectionSettings(current);
|
||||
|
||||
this.apiBaseUrl = current.apiBaseUrl;
|
||||
this.apiKey = current.apiKey;
|
||||
|
||||
if (previous.apiBaseUrl !== current.apiBaseUrl || previous.apiKey !== current.apiKey) {
|
||||
document.dispatchEvent(new CustomEvent("connection:updated", {
|
||||
detail: {
|
||||
previous,
|
||||
current: { ...current }
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
return {
|
||||
...current,
|
||||
didPersist
|
||||
};
|
||||
}
|
||||
};
|
||||
})();
|
||||
Reference in New Issue
Block a user