Files
sencho/frontend/src/lib/api.ts
T
SaelixCode f7e8e40915 feat(settings): scope split — developer settings always target local node
- Add localOnly option to apiFetch — omits x-node-id header so the
  request bypasses the proxy and always hits the local Sencho instance
- fetchSettings now performs two fetches when a remote node is active:
  active node fetch for per-node settings (CPU/RAM/disk limits, janitor,
  crash detection), and a localOnly fetch for UI preferences
  (developer_mode, global_logs_refresh, metrics_retention_hours,
  log_retention_days)
- saveDeveloperSettings passes localOnly: true — developer preferences
  can no longer be written into a remote node's database
- Update scope badges: System Limits shows "Configuring: [node]",
  Developer shows "Always Local" when a remote node is active
2026-03-20 20:12:00 -04:00

51 lines
1.5 KiB
TypeScript

const API_BASE = '/api';
export interface ApiFetchOptions extends RequestInit {
/** When true, omits the x-node-id header so the request always targets
* the local node regardless of which node is currently active in the UI. */
localOnly?: boolean;
}
export async function apiFetch(
endpoint: string,
options: ApiFetchOptions = {}
): Promise<Response> {
const { localOnly, ...fetchOptions } = options;
const url = `${API_BASE}${endpoint}`;
const activeNodeId = localOnly ? null : localStorage.getItem('sencho-active-node');
const defaultOptions: RequestInit = {
credentials: 'include',
headers: {
'Content-Type': 'application/json',
...(activeNodeId ? { 'x-node-id': activeNodeId } : {}),
...fetchOptions.headers,
},
};
const response = await fetch(url, { ...defaultOptions, ...fetchOptions });
if (response.status === 401) {
// Signal auth failure to AuthContext without a hard page reload
window.dispatchEvent(new Event('sencho-unauthorized'));
throw new Error('Unauthorized');
}
// Intercept 404 Node Not Found responses and force context refresh
if (response.status === 404) {
try {
const clone = response.clone();
const errData = await clone.json();
if (errData.error && errData.error.includes('not found') && errData.error.includes('Node')) {
window.dispatchEvent(new Event('node-not-found'));
}
} catch (e) {
// Ignore JSON parse errors, caller handles standard 404s
}
}
return response;
}
export { API_BASE };