update commit message
This commit is contained in:
@@ -1,6 +1,20 @@
|
||||
(function () {
|
||||
const apiBaseUrlStorageKey = "tarot-time-api-base-url";
|
||||
const apiKeyStorageKey = "tarot-time-api-key";
|
||||
const defaultConnectionAccess = Object.freeze({
|
||||
connected: false,
|
||||
apiKeyRequired: false,
|
||||
authenticated: false,
|
||||
clientId: "",
|
||||
accountId: "",
|
||||
accessLevel: "",
|
||||
roles: Object.freeze([]),
|
||||
scopes: Object.freeze([]),
|
||||
capabilities: Object.freeze({
|
||||
tarot: false,
|
||||
adminApiManagement: false
|
||||
})
|
||||
});
|
||||
|
||||
function normalizeBaseUrl(value) {
|
||||
return String(value || "")
|
||||
@@ -12,6 +26,55 @@
|
||||
return String(value || "").trim();
|
||||
}
|
||||
|
||||
function normalizeStringList(values) {
|
||||
return Array.isArray(values)
|
||||
? values.map((value) => String(value || "").trim()).filter(Boolean)
|
||||
: [];
|
||||
}
|
||||
|
||||
function normalizeAccessLevel(value) {
|
||||
return String(value || "").trim().toLowerCase();
|
||||
}
|
||||
|
||||
function normalizeConnectionAccess(source = null) {
|
||||
const health = source?.health || {};
|
||||
const auth = health?.auth || source?.auth || {};
|
||||
const roles = normalizeStringList(auth?.roles);
|
||||
const scopes = normalizeStringList(auth?.scopes);
|
||||
const apiKeyRequired = health?.apiKeyRequired === true || source?.apiKeyRequired === true;
|
||||
const connected = source?.ok === true || source?.connected === true;
|
||||
const accessLevel = normalizeAccessLevel(
|
||||
auth?.accessLevel
|
||||
|| source?.accessLevel
|
||||
|| (connected && !apiKeyRequired ? "premium" : "")
|
||||
);
|
||||
const tarotCapability = source?.capabilities?.tarot === true
|
||||
|| (connected && !apiKeyRequired)
|
||||
|| accessLevel === "premium";
|
||||
const adminApiManagementCapability = source?.capabilities?.adminApiManagement === true
|
||||
|| roles.includes("admin")
|
||||
|| scopes.includes("api:admin");
|
||||
|
||||
return {
|
||||
connected,
|
||||
apiKeyRequired,
|
||||
authenticated: auth?.authenticated === true,
|
||||
clientId: String(auth?.clientId || source?.clientId || "").trim(),
|
||||
accountId: String(auth?.accountId || source?.accountId || "").trim(),
|
||||
accessLevel,
|
||||
roles,
|
||||
scopes,
|
||||
capabilities: {
|
||||
tarot: tarotCapability,
|
||||
adminApiManagement: adminApiManagementCapability
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function sameConnectionAccess(left, right) {
|
||||
return JSON.stringify(left) === JSON.stringify(right);
|
||||
}
|
||||
|
||||
function normalizeConnectionSettings(settings) {
|
||||
return {
|
||||
apiBaseUrl: normalizeBaseUrl(settings?.apiBaseUrl),
|
||||
@@ -90,11 +153,13 @@
|
||||
stripLegacyCredentialParams();
|
||||
|
||||
const initialConnectionSettings = readConfiguredConnectionSettings();
|
||||
const initialConnectionAccess = normalizeConnectionAccess(null);
|
||||
|
||||
window.TarotAppConfig = {
|
||||
...(window.TarotAppConfig || {}),
|
||||
apiBaseUrl: initialConnectionSettings.apiBaseUrl,
|
||||
apiKey: initialConnectionSettings.apiKey,
|
||||
connectionAccess: initialConnectionAccess,
|
||||
getApiBaseUrl() {
|
||||
return normalizeBaseUrl(this.apiBaseUrl);
|
||||
},
|
||||
@@ -110,6 +175,31 @@
|
||||
apiKey: this.getApiKey()
|
||||
};
|
||||
},
|
||||
getConnectionAccess() {
|
||||
return normalizeConnectionAccess(this.connectionAccess || defaultConnectionAccess);
|
||||
},
|
||||
hasTarotAccess() {
|
||||
return this.getConnectionAccess().capabilities.tarot === true;
|
||||
},
|
||||
hasAdminApiManagementAccess() {
|
||||
return this.getConnectionAccess().capabilities.adminApiManagement === true;
|
||||
},
|
||||
updateConnectionAccess(nextAccess = null) {
|
||||
const previous = this.getConnectionAccess();
|
||||
const current = normalizeConnectionAccess(nextAccess);
|
||||
this.connectionAccess = current;
|
||||
|
||||
if (!sameConnectionAccess(previous, current)) {
|
||||
document.dispatchEvent(new CustomEvent("connection:access-updated", {
|
||||
detail: {
|
||||
previous,
|
||||
current: { ...current, roles: [...current.roles], scopes: [...current.scopes], capabilities: { ...current.capabilities } }
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
return current;
|
||||
},
|
||||
updateConnectionSettings(nextSettings = {}) {
|
||||
const previous = this.getConnectionSettings();
|
||||
const current = normalizeConnectionSettings({
|
||||
|
||||
Reference in New Issue
Block a user