feat(js): harden global HTTP and module import contracts
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import { initMultiSelect } from '../../../../js/components/app-multiselect-init.js';
|
||||
import { initMultiSelectCascade } from '../../../../js/components/app-multiselect-cascade.js';
|
||||
import { initStandardListPage } from '../../../../js/core/app-grid-factory.js';
|
||||
import { readPageConfig } from '../../../../js/core/app-page-config.js';
|
||||
import { warnOnce } from '../../../../js/core/app-dom.js';
|
||||
import { getAppBase, escapeHtml, buildBadgeList, gridFilterMultiCsv, gridFilterText, withCurrentListReturn } from '../../../../js/pages/app-list-utils.js';
|
||||
import { initMultiSelect } from '/js/components/app-multiselect-init.js';
|
||||
import { initMultiSelectCascade } from '/js/components/app-multiselect-cascade.js';
|
||||
import { initStandardListPage } from '/js/core/app-grid-factory.js';
|
||||
import { readPageConfig } from '/js/core/app-page-config.js';
|
||||
import { warnOnce } from '/js/core/app-dom.js';
|
||||
import { getAppBase, escapeHtml, buildBadgeList, gridFilterMultiCsv, gridFilterText, withCurrentListReturn } from '/js/pages/app-list-utils.js';
|
||||
|
||||
const config = readPageConfig('address-book-index');
|
||||
if (config) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,9 @@
|
||||
* notification list rendering, mark-read and dismiss actions.
|
||||
*/
|
||||
|
||||
import { telemetry } from '../../../../js/core/app-telemetry.js';
|
||||
import { getAppBase } from '../../../../js/pages/app-list-utils.js';
|
||||
import { telemetry } from '/js/core/app-telemetry.js';
|
||||
import { getJson, postForm } from '/js/core/app-http.js';
|
||||
import { getAppBase } from '/js/pages/app-list-utils.js';
|
||||
|
||||
const POLL_INTERVAL_MS = 60_000;
|
||||
const TYPE_ICONS = {
|
||||
@@ -43,12 +44,6 @@ function escapeHtml(str) {
|
||||
return d.innerHTML;
|
||||
}
|
||||
|
||||
async function fetchJson(url, options = {}) {
|
||||
const resp = await fetch(url, options);
|
||||
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
|
||||
return resp.json();
|
||||
}
|
||||
|
||||
export function initNotificationBell(root = document, config = {}) {
|
||||
const details = root.querySelector('[data-notification-bell]');
|
||||
const dropdown = root.querySelector('[data-notification-dropdown]');
|
||||
@@ -173,7 +168,7 @@ export function initNotificationBell(root = document, config = {}) {
|
||||
showState('loading');
|
||||
list?.querySelectorAll('.app-notification-item').forEach(el => el.remove());
|
||||
try {
|
||||
const data = await fetchJson(endpoint('notifications/list-data'), { signal });
|
||||
const data = await getJson(endpoint('notifications/list-data'), { signal });
|
||||
if (data.ok) {
|
||||
renderNotifications(data.notifications || []);
|
||||
updateBadge(data.unread_count ?? 0);
|
||||
@@ -192,7 +187,7 @@ export function initNotificationBell(root = document, config = {}) {
|
||||
|
||||
async function pollUnreadCount() {
|
||||
try {
|
||||
const data = await fetchJson(endpoint('notifications/unread-count-data'), { signal });
|
||||
const data = await getJson(endpoint('notifications/unread-count-data'), { signal });
|
||||
if (data.ok) {
|
||||
updateBadge(data.unread_count ?? 0);
|
||||
}
|
||||
@@ -212,11 +207,7 @@ export function initNotificationBell(root = document, config = {}) {
|
||||
body.set('id', String(id));
|
||||
if (csrf.key) body.set(csrf.key, csrf.token);
|
||||
|
||||
const data = await fetchJson(endpoint('notifications/mark-read-data'), {
|
||||
method: 'POST',
|
||||
body,
|
||||
signal,
|
||||
});
|
||||
const data = await postForm(endpoint('notifications/mark-read-data'), body, { signal });
|
||||
if (data.ok) {
|
||||
updateBadge(data.unread_count ?? 0);
|
||||
if (details.open) loadNotifications();
|
||||
@@ -237,11 +228,7 @@ export function initNotificationBell(root = document, config = {}) {
|
||||
body.set('all', '1');
|
||||
if (csrf.key) body.set(csrf.key, csrf.token);
|
||||
|
||||
const data = await fetchJson(endpoint('notifications/mark-read-data'), {
|
||||
method: 'POST',
|
||||
body,
|
||||
signal,
|
||||
});
|
||||
const data = await postForm(endpoint('notifications/mark-read-data'), body, { signal });
|
||||
if (data.ok) {
|
||||
updateBadge(data.unread_count ?? 0);
|
||||
if (details.open) loadNotifications();
|
||||
@@ -262,11 +249,7 @@ export function initNotificationBell(root = document, config = {}) {
|
||||
body.set('id', String(id));
|
||||
if (csrf.key) body.set(csrf.key, csrf.token);
|
||||
|
||||
const data = await fetchJson(endpoint('notifications/delete-data'), {
|
||||
method: 'POST',
|
||||
body,
|
||||
signal,
|
||||
});
|
||||
const data = await postForm(endpoint('notifications/delete-data'), body, { signal });
|
||||
if (data.ok) {
|
||||
updateBadge(data.unread_count ?? 0);
|
||||
const el = list?.querySelector(`[data-notification-id="${id}"]`);
|
||||
|
||||
Reference in New Issue
Block a user