import axios from 'axios'; import Echo from 'laravel-echo'; import Pusher from 'pusher-js'; import {useAuthStore} from "./Stores/auth.js"; import {startGlobalLoading, stopGlobalLoading} from "./Composables/useGlobalLoading.js"; window.axios = axios; window.Pusher = Pusher; let reverbFailCount = 0; const MAX_REVERB_FAILS = 3; window.Echo = new Echo({ broadcaster: 'reverb', key: window.reverb.key, wsHost: window.reverb.host, wsPort: window.reverb.port ?? 8080, wssPort: window.reverb.port ?? 8080, forceTLS: (window.reverb.scheme ?? 'http') === 'https', enabledTransports: ['ws', 'wss'], withCredentials: true, }); window.Echo.connector.pusher.connection.bind('failed', () => { reverbFailCount++; if (reverbFailCount >= MAX_REVERB_FAILS) { console.warn('Reverb недоступен, отключаем WebSocket'); window.Echo.disconnect(); } }); window.Echo.connector.pusher.connection.bind('unavailable', () => { console.warn('Reverb недоступен'); window.Echo.disconnect(); }); window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; window.axios.defaults.withCredentials = true window.axios.defaults.withXSRFToken = true; // Добавляем токен авторизации к запросам window.axios.interceptors.request.use( (config) => { startGlobalLoading() const authStore = useAuthStore() // Если токен есть, добавляем его в заголовки if (authStore.token) { config.headers.Authorization = `Bearer ${authStore.token}` } // Для форм добавляем заголовок Content-Type if (config.data instanceof FormData) { config.headers['Content-Type'] = 'multipart/form-data' } else { config.headers['Content-Type'] = 'application/json' } return config }, (error) => { stopGlobalLoading() return Promise.reject(error) } ) // Интерцептор ответов для обработки ошибок авторизации window.axios.interceptors.response.use( (response) => { stopGlobalLoading() return response }, (error) => { stopGlobalLoading() const authStore = useAuthStore() if (error.response?.status === 401) { if (authStore.isAuthenticated) { authStore.logout() } } return Promise.reject(error) } ) // Вспомогательные методы для работы с API const api = { // GET запрос get: async (url, config = {}) => { try { const response = await axios.get(url, config) return response.data } catch (error) { throw error.response?.data || error } }, // POST запрос post: async (url, data = {}, config = {}) => { try { const response = await axios.post(url, data, config) return response.data } catch (error) { throw error.response?.data || error } }, // PUT запрос put: async (url, data = {}, config = {}) => { try { const response = await axios.put(url, data, config) return response.data } catch (error) { throw error.response?.data || error } }, // PATCH запрос patch: async (url, data = {}, config = {}) => { try { const response = await axios.patch(url, data, config) return response.data } catch (error) { throw error.response?.data || error } }, // DELETE запрос delete: async (url, config = {}) => { try { const response = await axios.delete(url, config) return response.data } catch (error) { throw error.response?.data || error } }, // Загрузка файлов upload: async (url, formData, config = {}) => { try { const response = await axios.post(url, formData, { ...config, headers: { 'Content-Type': 'multipart/form-data' } }) return response.data } catch (error) { throw error.response?.data || error } }, // Проверка токена checkToken: async () => { try { const response = await axios.get('/auth/check-token') return response.data.valid } catch { return false } } } export default api