feat(js): harden global HTTP and module import contracts

This commit is contained in:
2026-04-20 22:41:07 +02:00
parent f189ef9df6
commit 7c47e818f2
12 changed files with 181 additions and 266 deletions

View File

@@ -4,9 +4,10 @@
* - Reorder bookmarks inside groups (up/down)
* - Edit/Delete groups and bookmarks with confirm dialog
*/
import { showAsyncFlash } from '../../../../js/components/app-async-flash.js';
import { warnOnce, resolveHost } from '../../../../js/core/app-dom.js';
import { confirmDialog } from '../../../../js/core/app-confirm-dialog.js';
import { showAsyncFlash } from '/js/components/app-async-flash.js';
import { warnOnce, resolveHost } from '/js/core/app-dom.js';
import { confirmDialog } from '/js/core/app-confirm-dialog.js';
import { postForm } from '/js/core/app-http.js';
export function initBookmarkPanel(root = document, options = {}) {
const host = resolveHost(root);
@@ -84,14 +85,6 @@ export function initBookmarkPanel(root = document, options = {}) {
});
};
const parseJsonSafe = async (response) => {
try {
return await response.json();
} catch {
return null;
}
};
const reportError = (code, message, details = {}) => {
warnOnce(code, message, { ...moduleContext, ...details });
showAsyncFlash('error', message);
@@ -176,16 +169,7 @@ export function initBookmarkPanel(root = document, options = {}) {
setPending(true);
try {
const response = await fetch(reorderUrl, {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', messageReorderFailed, { status: response.status, type });
return;
}
const json = await postForm(reorderUrl, body);
if (!json.ok) {
reportError('FETCH_FAILED', messageReorderFailed, { error: json.error, type });
return;
@@ -193,7 +177,7 @@ export function initBookmarkPanel(root = document, options = {}) {
runReload();
} catch (error) {
reportError('FETCH_ERROR', messageReorderFailed, { error, type });
reportError('FETCH_ERROR', messageReorderFailed, { error, status: error?.status, type });
} finally {
setPending(false);
}
@@ -317,16 +301,7 @@ export function initBookmarkPanel(root = document, options = {}) {
setPending(true);
try {
const response = await fetch(groupDeleteUrl, {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', messageGroupDeleteFailed, { status: response.status, groupId });
return;
}
const json = await postForm(groupDeleteUrl, body);
if (!json.ok) {
reportError('FETCH_FAILED', messageGroupDeleteFailed, { error: json.error, groupId });
return;
@@ -334,7 +309,7 @@ export function initBookmarkPanel(root = document, options = {}) {
runReload(messageGroupDeleted);
} catch (error) {
reportError('FETCH_ERROR', messageGroupDeleteFailed, { error, groupId });
reportError('FETCH_ERROR', messageGroupDeleteFailed, { error, status: error?.status, groupId });
} finally {
setPending(false);
}
@@ -368,16 +343,7 @@ export function initBookmarkPanel(root = document, options = {}) {
setPending(true);
try {
const response = await fetch(bookmarkDeleteUrl, {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', messageBookmarkDeleteFailed, { status: response.status, bookmarkId });
return;
}
const json = await postForm(bookmarkDeleteUrl, body);
if (!json.ok) {
reportError('FETCH_FAILED', messageBookmarkDeleteFailed, { error: json.error, bookmarkId });
return;
@@ -385,7 +351,7 @@ export function initBookmarkPanel(root = document, options = {}) {
runReload(messageBookmarkDeleted);
} catch (error) {
reportError('FETCH_ERROR', messageBookmarkDeleteFailed, { error, bookmarkId });
reportError('FETCH_ERROR', messageBookmarkDeleteFailed, { error, status: error?.status, bookmarkId });
} finally {
setPending(false);
}

View File

@@ -3,9 +3,10 @@
* Bookmark dialog (create/update) + group dialog (create/update).
*/
import { showAsyncFlash } from '../../../../js/components/app-async-flash.js';
import { warnOnce, resolveHost } from '../../../../js/core/app-dom.js';
import { getAppBase } from '../../../../js/pages/app-list-utils.js';
import { showAsyncFlash } from '/js/components/app-async-flash.js';
import { warnOnce, resolveHost } from '/js/core/app-dom.js';
import { postForm } from '/js/core/app-http.js';
import { getAppBase } from '/js/pages/app-list-utils.js';
export function initBookmarkSave(root = document, options = {}) {
const host = resolveHost(root);
@@ -133,14 +134,6 @@ export function initBookmarkSave(root = document, options = {}) {
showAsyncFlash('error', message);
};
const parseJsonSafe = async (response) => {
try {
return await response.json();
} catch {
return null;
}
};
const resolveBookmarkApiErrorMessage = (errorCode) => {
if (!errorCode) return bookmarkMessageErrorGeneric;
return bookmarkApiErrorMessages[errorCode] || bookmarkMessageErrorGeneric;
@@ -348,16 +341,7 @@ export function initBookmarkSave(root = document, options = {}) {
if (editingBookmarkId) {
body.set('id', editingBookmarkId);
try {
const response = await fetch(getAppBase() + 'bookmarks/update-data', {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', bookmarkMessageErrorGeneric, { status: response.status });
return;
}
const json = await postForm(getAppBase() + 'bookmarks/update-data', body);
if (!json.ok) {
reportError('FETCH_FAILED', resolveBookmarkApiErrorMessage(json.error), { error: json.error });
return;
@@ -366,7 +350,7 @@ export function initBookmarkSave(root = document, options = {}) {
closeBookmarkDialog();
runReloadWithSuccess(bookmarkMessageUpdated);
} catch (error) {
reportError('FETCH_ERROR', bookmarkMessageErrorGeneric, { error });
reportError('FETCH_ERROR', bookmarkMessageErrorGeneric, { error, status: error?.status });
} finally {
setBookmarkSubmitPending(false);
}
@@ -375,16 +359,7 @@ export function initBookmarkSave(root = document, options = {}) {
body.set('url', currentRelativeUrl());
try {
const response = await fetch(getAppBase() + 'bookmarks/save-data', {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', bookmarkMessageErrorGeneric, { status: response.status });
return;
}
const json = await postForm(getAppBase() + 'bookmarks/save-data', body);
if (!json.ok) {
if (json.error === 'max_reached') {
showAsyncFlash('warning', maxBookmarkMessage || 'Maximum bookmarks reached');
@@ -397,7 +372,7 @@ export function initBookmarkSave(root = document, options = {}) {
closeBookmarkDialog();
runReloadWithSuccess(json.mode === 'updated' ? bookmarkMessageUpdated : bookmarkMessageSaved);
} catch (error) {
reportError('FETCH_ERROR', bookmarkMessageErrorGeneric, { error });
reportError('FETCH_ERROR', bookmarkMessageErrorGeneric, { error, status: error?.status });
} finally {
setBookmarkSubmitPending(false);
}
@@ -659,16 +634,7 @@ export function initBookmarkSave(root = document, options = {}) {
setGroupSubmitPending(true);
try {
const response = await fetch(getAppBase() + 'bookmarks/group-save-data', {
method: 'POST',
headers: { 'X-Requested-With': 'XMLHttpRequest' },
body
});
const json = await parseJsonSafe(response);
if (!response.ok || !json) {
reportError('FETCH_FAILED', groupMessageErrorGeneric, { status: response.status });
return;
}
const json = await postForm(getAppBase() + 'bookmarks/group-save-data', body);
if (!json.ok) {
if (json.error === 'max_reached') {
showAsyncFlash('warning', maxGroupMessage);
@@ -694,7 +660,7 @@ export function initBookmarkSave(root = document, options = {}) {
runReloadWithSuccess(successMessage);
}
} catch (error) {
reportError('FETCH_ERROR', groupMessageErrorGeneric, { error });
reportError('FETCH_ERROR', groupMessageErrorGeneric, { error, status: error?.status });
} finally {
setGroupSubmitPending(false);
}