feat(js): harden global HTTP and module import contracts
This commit is contained in:
@@ -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