BIG_UPDATE deleted vps, added s3 infrastructure.
This commit is contained in:
71
ospabhost/frontend/src/utils/apiClient.ts
Normal file
71
ospabhost/frontend/src/utils/apiClient.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import axios, { AxiosError } from 'axios';
|
||||
import type { InternalAxiosRequestConfig } from 'axios';
|
||||
import { API_URL } from '../config/api';
|
||||
|
||||
// Создаём экземпляр axios с базовым URL
|
||||
export const apiClient = axios.create({
|
||||
baseURL: API_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Добавляем токен к каждому запросу
|
||||
apiClient.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token && config.headers) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// Обработка ответов и ошибок
|
||||
apiClient.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error: AxiosError) => {
|
||||
if (error.response) {
|
||||
const status = error.response.status;
|
||||
|
||||
// Обрабатываем ошибки без автоматического перенаправления
|
||||
// Компоненты сами решают, как обрабатывать ошибки
|
||||
switch (status) {
|
||||
case 401:
|
||||
// Unauthorized - очищаем токен
|
||||
localStorage.removeItem('access_token');
|
||||
sessionStorage.clear();
|
||||
// Вызываем событие для реакции на изменение авторизации
|
||||
window.dispatchEvent(new Event('unauthorized'));
|
||||
break;
|
||||
case 403:
|
||||
// Forbidden - нет прав доступа
|
||||
console.warn('Access denied (403)');
|
||||
break;
|
||||
case 404:
|
||||
// Not Found - ресурс не найден
|
||||
console.warn('Resource not found (404)');
|
||||
break;
|
||||
case 500:
|
||||
case 502:
|
||||
case 503:
|
||||
case 504:
|
||||
// Server errors
|
||||
console.error(`Server error (${status})`);
|
||||
break;
|
||||
}
|
||||
} else if (error.request) {
|
||||
// Ошибка сети - сервер недоступен
|
||||
console.error('Network error:', error.message);
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
export default apiClient;
|
||||
49
ospabhost/frontend/src/utils/logger.ts
Normal file
49
ospabhost/frontend/src/utils/logger.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Logger utility для frontend - логирование только в development режиме
|
||||
*/
|
||||
|
||||
const isDebug = import.meta.env.MODE === 'development';
|
||||
|
||||
export const logger = {
|
||||
log: (...args: unknown[]) => {
|
||||
if (isDebug) {
|
||||
console.log(...args);
|
||||
}
|
||||
},
|
||||
|
||||
error: (...args: unknown[]) => {
|
||||
// Ошибки логируем всегда
|
||||
console.error(...args);
|
||||
},
|
||||
|
||||
warn: (...args: unknown[]) => {
|
||||
if (isDebug) {
|
||||
console.warn(...args);
|
||||
}
|
||||
},
|
||||
|
||||
info: (...args: unknown[]) => {
|
||||
if (isDebug) {
|
||||
console.info(...args);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// WebSocket специфичные логи
|
||||
export const wsLogger = {
|
||||
log: (message: string, ...args: unknown[]) => {
|
||||
if (isDebug) {
|
||||
console.log(`[WS] ${message}`, ...args);
|
||||
}
|
||||
},
|
||||
|
||||
error: (message: string, ...args: unknown[]) => {
|
||||
console.error(`[WS] ${message}`, ...args);
|
||||
},
|
||||
|
||||
warn: (message: string, ...args: unknown[]) => {
|
||||
if (isDebug) {
|
||||
console.warn(`[WS] ${message}`, ...args);
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user