Refactor network API calls to use centralized authentication context and improve cache handling

- Removed redundant getApiToken function and replaced its usage with getAuthContext in network-api.js, network-events.js, and network-init.js.
- Updated cache handling in network-events.js and network-init.js to use CACHE_KEYS constants for better maintainability.
- Introduced cache.js for centralized cache management functions and constants, including cache initialization and retrieval.
- Added app-init.js for application lifecycle management, including cache orchestration and initialization checks.
- Created app_config.php to securely fetch API token and GraphQL port from configuration.
- Improved error handling and logging throughout the codebase for better debugging and maintenance.
This commit is contained in:
Jokob @NetAlertX
2026-02-26 04:21:29 +00:00
parent 00042ab594
commit 63cef590d6
12 changed files with 915 additions and 682 deletions

View File

@@ -12,8 +12,8 @@ var showOffline = false;
*/
function initNetworkTopology() {
networkDeviceTypes = getSetting("NETWORK_DEVICE_TYPES").replace("[", "").replace("]", "");
showArchived = getCache('showArchived') === "true";
showOffline = getCache('showOffline') === "true";
showArchived = getCache(CACHE_KEYS.SHOW_ARCHIVED) === "true";
showOffline = getCache(CACHE_KEYS.SHOW_OFFLINE) === "true";
console.log('showArchived:', showArchived);
console.log('showOffline:', showOffline);
@@ -33,8 +33,7 @@ function initNetworkTopology() {
FROM Devices a
`;
const apiBase = getApiBase();
const apiToken = getApiToken();
const { token: apiToken, apiBase, authHeader } = getAuthContext();
// Verify token is available before making API call
if (!apiToken || apiToken.trim() === '') {
@@ -51,7 +50,7 @@ function initNetworkTopology() {
$.ajax({
url,
method: "POST",
headers: { "Authorization": `Bearer ${apiToken}` },
headers: { ...authHeader, "Content-Type": "application/json" },
data: JSON.stringify({ rawSql: btoa(unescape(encodeURIComponent(rawSql))) }),
contentType: "application/json",
success: function(data) {
@@ -121,7 +120,7 @@ function initNetworkTopology() {
}
});
setCache('devicesListNew', JSON.stringify(devicesSorted));
setCache(CACHE_KEYS.DEVICES_TOPOLOGY, JSON.stringify(devicesSorted));
deviceListGlobal = devicesSorted;
// Render filtered result