Fixes 401 Unauthorized API responses being incorrectly cached

This commit is contained in:
BinaryAlley committed 2024-12-25 18:34:49 +02:00
1 parent f246f0bf75
commit d6af519e17
37 files changed
+298 -174

No files matched your search

@@ -233,11 +233,11 @@
*/
async function getFileSystemPropertiesAsync() {
// call the API to get the file system type (platform type)
const fileSystemTypeResponse = await callApiGetAsync(`${clientBasePath}file-system/get-type`);
const fileSystemTypeResponse = await callApiGetAsync(`${clientBasePath}file-system/api-get-type`);
if (fileSystemTypeResponse !== undefined)
platformType = fileSystemTypeResponse.platformType;
// call the API to get the path separator for the file system
const pathSeparatorResponse = await callApiGetAsync(`${clientBasePath}path/get-path-separator`);
const pathSeparatorResponse = await callApiGetAsync(`${clientBasePath}path/api-get-path-separator`);
if (pathSeparatorResponse !== undefined)
pathSeparator = pathSeparatorResponse.pathSeparator;
}
@@ -248,7 +248,7 @@
* @returns {Promise<boolean>} A promise that resolves to whether the path is valid and exists, or not.
*/
async function checkPathAsync(path) {
const pathValidResponse = await callApiGetAsync(`${clientBasePath}path/validate?path=${encodeURIComponent(path)}`);
const pathValidResponse = await callApiGetAsync(`${clientBasePath}path/api-validate?path=${encodeURIComponent(path)}`);
if (pathValidResponse !== undefined) {
if (!pathValidResponse.isValid) {
notificationService.show("Specified path is not valid!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented
@@ -256,7 +256,7 @@
if (addressBarInput.value !== null && !addressBarInput.value.endsWith(pathSeparator))
addressBarInput.value += pathSeparator;
} else {
const pathExistsResponse = await callApiGetAsync(`${clientBasePath}path/check-path-exists?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`);
const pathExistsResponse = await callApiGetAsync(`${clientBasePath}path/api-check-path-exists?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`);
if (pathExistsResponse !== undefined && !pathExistsResponse.exists) {
notificationService.show("Specified path does not exist!", NotificationType.ERROR); // TODO: should ask error message from server when translation is implemented
return false;
@@ -338,7 +338,7 @@
async function navigateUpAsync() {
if (addressBarInput.value !== null) {
// ask the API for the parent path
const parentLocationResponse = await callApiGetAsync(`${clientBasePath}path/get-path-parent?path=${encodeURIComponent(addressBarInput.value)}`);
const parentLocationResponse = await callApiGetAsync(`${clientBasePath}path/api-get-path-parent?path=${encodeURIComponent(addressBarInput.value)}`);
if (parentLocationResponse !== undefined && parentLocationResponse.pathSegments !== null) {
// if the answer was valid, and we can navigate up, reconstruct the provided path from its segments, and navigate to it
platformType = parentLocationResponse.pathSegments;
@@ -362,7 +362,7 @@
* @param {any} path The path for which to get the navigator path segments.
*/
async function getNavigatorPathAsync(path) {
const pathSplitResponse = await callApiGetAsync(`${clientBasePath}path/split?path=${encodeURIComponent(path)}`);
const pathSplitResponse = await callApiGetAsync(`${clientBasePath}path/api-split?path=${encodeURIComponent(path)}`);
if (pathSplitResponse !== undefined)
renderAddressBar(pathSplitResponse.pathSegments);
}
@@ -589,7 +589,7 @@
* @returns {Promise<Array<Object>|undefined>} A promise that resolves to an array of drive objects, or undefined if the API call fails.
*/
async function getFileSystemDrivesAsync() {
const getDrivesResponse = await callApiGetAsync(`${clientBasePath}drives/get-drives`);
const getDrivesResponse = await callApiGetAsync(`${clientBasePath}drives/api-get-drives`);
if (getDrivesResponse !== undefined)
return getDrivesResponse.drives;
}
@@ -716,7 +716,7 @@
* @param {Function} callback - Function to call for each directory received.
*/
async function fetchFileSystemDirectoriesAsync(path, callback) {
await fetchJsonDataAsync(`${clientBasePath}directories/get-directories?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`, callback, 'Directory');
await fetchJsonDataAsync(`${clientBasePath}directories/api-get-directories?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`, callback, 'Directory');
}
/**
@@ -725,7 +725,7 @@
* @param {Function} callback - Function to call for each file received.
*/
async function fetchFileSystemFilesAsync(path, callback) {
await fetchJsonDataAsync(`${clientBasePath}files/get-tree-files?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`, callback, 'File');
await fetchJsonDataAsync(`${clientBasePath}files/api-get-tree-files?path=${encodeURIComponent(path)}&includeHiddenElements=${showHiddenElements}`, callback, 'File');
}
/**
+92 -84
View File
@@ -23,8 +23,12 @@ var ajaxCallCounter = 0;
*/
async function callApiGetAsync(url) {
try {
// create the URL for the API endpoint
const response = await fetch(url); // call the API
// call the API
const response = await fetch(url, {
headers: {
'Accept': 'application/json'
}
});
if (response.redirected) // if a redirect is requested, perform it
window.location.href = response.url;
else {
@@ -279,85 +283,88 @@ async function updateContentAsync(url, addToHistory) {
}
const response = await fetch(url);
const html = await response.text();
if (response.redirected) // if a redirect is requested, perform it
window.location.href = response.url;
else {
const html = await response.text();
// parse the new content
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const content = doc.querySelector('main').innerHTML;
const title = doc.querySelector('title')?.textContent || document.title;
// parse the new content
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const content = doc.querySelector('main').innerHTML;
const title = doc.querySelector('title')?.textContent || document.title;
// update the page content
document.querySelector('main').innerHTML = content;
document.title = title;
// update the page content
document.querySelector('main').innerHTML = content;
document.title = title;
// process all scripts in order: component scripts first, then view scripts
const componentScripts = doc.querySelectorAll('script[data-component-script]');
for (const script of componentScripts) {
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
if (script.src) {
// wait for external script to fully load before continuing
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject;
document.body.appendChild(newScript);
// process all scripts in order: component scripts first, then view scripts
const componentScripts = doc.querySelectorAll('script[data-component-script]');
for (const script of componentScripts) {
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
if (script.src) {
// wait for external script to fully load before continuing
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject;
document.body.appendChild(newScript);
});
}
}
}
// execute component initialization scripts
const componentInits = doc.querySelectorAll('script[data-component-init]');
for (const script of componentInits) {
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
newScript.textContent = script.textContent;
document.body.appendChild(newScript);
}
// process view scripts
const viewScripts = doc.querySelector('[data-section="scripts"]')?.getElementsByTagName('script') || [];
// get or create scripts container
let scriptsContainer = document.querySelector('[data-section="scripts"]');
if (!scriptsContainer) {
scriptsContainer = document.createElement('div');
scriptsContainer.setAttribute('data-section', 'scripts');
document.body.appendChild(scriptsContainer);
}
// clear existing scripts in container
scriptsContainer.innerHTML = '';
// process view scripts
for (const script of viewScripts) {
const scriptId = script.getAttribute('data-form');
// remove existing script with same Id if it exists
const existingScript = document.querySelector(`script[data-form="${scriptId}"]`);
if (existingScript)
existingScript.remove();
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
// handle both inline and external scripts
if (script.src) {
newScript.src = script.src;
// wait for external script to load
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject;
// execute component initialization scripts
const componentInits = doc.querySelectorAll('script[data-component-init]');
for (const script of componentInits) {
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
} else {
// wrap inline scripts in a safety check
newScript.textContent = `
newScript.textContent = script.textContent;
document.body.appendChild(newScript);
}
// process view scripts
const viewScripts = doc.querySelector('[data-section="scripts"]')?.getElementsByTagName('script') || [];
// get or create scripts container
let scriptsContainer = document.querySelector('[data-section="scripts"]');
if (!scriptsContainer) {
scriptsContainer = document.createElement('div');
scriptsContainer.setAttribute('data-section', 'scripts');
document.body.appendChild(scriptsContainer);
}
// clear existing scripts in container
scriptsContainer.innerHTML = '';
// process view scripts
for (const script of viewScripts) {
const scriptId = script.getAttribute('data-form');
// remove existing script with same Id if it exists
const existingScript = document.querySelector(`script[data-form="${scriptId}"]`);
if (existingScript)
existingScript.remove();
const newScript = document.createElement('script');
// copy script attributes
Array.from(script.attributes).forEach(attr => {
newScript.setAttribute(attr.name, attr.value);
});
// handle both inline and external scripts
if (script.src) {
newScript.src = script.src;
// wait for external script to load
await new Promise((resolve, reject) => {
newScript.onload = resolve;
newScript.onerror = reject;
});
} else {
// wrap inline scripts in a safety check
newScript.textContent = `
(function() {
const safeGetElement = (id) => document.getElementById(id);
const safeAddEventListener = (element, e, handler) => {
@@ -367,17 +374,18 @@ async function updateContentAsync(url, addToHistory) {
${script.textContent}
})();
`;
}
// add script to scripts container
scriptsContainer.appendChild(newScript);
}
// add script to scripts container
scriptsContainer.appendChild(newScript);
}
// bind navigation links in the new content
bindNavigationLinks();
// bind navigation links in the new content
bindNavigationLinks();
// save to history if needed
if (addToHistory) {
const state = { url, content, title };
history.pushState(state, title, url);
// save to history if needed
if (addToHistory) {
const state = { url, content, title };
history.pushState(state, title, url);
}
}
} catch (error) {
notificationService.show(jsLocalizedMessages.navigationFailed, NotificationType.ERROR);