This commit is contained in:
Tomas Dvorak
2025-11-11 10:29:30 +01:00
parent d5b4faea61
commit 8762bde4bf
139 changed files with 7240 additions and 2870 deletions
+43
View File
@@ -1,5 +1,7 @@
import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { reportError } from './errorReporter';
import { getToken } from '../utils/auth';
import { logAction } from './actionLog';
function readStored(key: string): string | null {
try { return localStorage.getItem(key); } catch { return null; }
@@ -63,11 +65,22 @@ async function getCsrfToken(): Promise<string | null> {
// Request interceptor - attach bearer token when available
api.interceptors.request.use(
async (config: InternalAxiosRequestConfig) => {
(config as any).metadata = { ...(config as any).metadata, start: Date.now() };
const token = getToken();
config.headers = config.headers || {};
if (token) {
(config.headers as any).Authorization = `Bearer ${token}`;
}
// Dev helper: attach X-Admin-Token from localStorage if present (allows admin calls without rebuild)
if (process.env.NODE_ENV !== 'production') {
try {
const devAdmin = readStored('fc_admin_token');
if (devAdmin && !(config.headers as any)['X-Admin-Token']) {
(config.headers as any)['X-Admin-Token'] = devAdmin;
(config.headers as any)['X-Dev-Admin'] = 'true';
}
} catch {}
}
// For cookie-based flows (no Bearer header), attach X-CSRF-Token on mutating methods
const method = (config.method || 'get').toLowerCase();
const isMutating = method === 'post' || method === 'put' || method === 'patch' || method === 'delete';
@@ -89,6 +102,23 @@ api.interceptors.request.use(
api.interceptors.response.use(
(response: AxiosResponse) => response,
(error) => {
try {
const status = error?.response?.status;
try {
const cfg = error?.config || {};
const method: string = (cfg?.method || 'get').toUpperCase();
const url: string = cfg?.url || '';
const start: number | undefined = (cfg as any)?.metadata?.start;
const ms = typeof start === 'number' ? Date.now() - start : undefined;
logAction({ type: 'request', at: Date.now(), method, url, status, ms, ok: false });
} catch {}
if (typeof status === 'number' && status >= 500) {
const reqUrl: string = error.config?.url || '';
const method: string = (error.config?.method || 'get').toUpperCase();
const requestId: string | undefined = error.response?.headers?.['x-request-id'] || error.response?.headers?.['X-Request-ID'];
reportError({ message: `HTTP ${status} ${method} ${reqUrl}`, status, method, url: reqUrl, request_id: requestId });
}
} catch {}
if (error.response?.status === 401) {
// Avoid redirect loop on the login call itself
const reqUrl: string = error.config?.url || '';
@@ -108,6 +138,19 @@ api.interceptors.response.use(
}
);
api.interceptors.response.use((response: AxiosResponse) => {
try {
const cfg = response.config || {} as any;
const method: string = (cfg?.method || 'get').toUpperCase();
const url: string = cfg?.url || '';
const start: number | undefined = (cfg as any)?.metadata?.start;
const ms = typeof start === 'number' ? Date.now() - start : undefined;
const status = response.status;
logAction({ type: 'request', at: Date.now(), method, url, status, ms, ok: true });
} catch {}
return response;
});
// Upload image helper
export const uploadImage = async (formData: FormData): Promise<{ url: string }> => {
const res = await api.post('/upload', formData, {