This commit is contained in:
@kirill.komarov
2026-05-10 17:15:56 +05:00
parent e67d8bdc0a
commit 517cd23a55
17 changed files with 151 additions and 37 deletions
+9 -6
View File
@@ -1,19 +1,22 @@
import axios from 'axios'
import axios, { AxiosHeaders } from 'axios'
import { apiBaseURL } from '@/shared/config'
// Глобальный application/json ломает FormData: axios сериализует его в JSON. Тип для JSON задаёт transformRequest.
export const apiClient = axios.create({
baseURL: apiBaseURL,
headers: { 'Content-Type': 'application/json' },
})
apiClient.interceptors.request.use((config) => {
try {
config.headers = AxiosHeaders.from(config.headers ?? {})
const token = localStorage.getItem('craftshop_auth_token')
if (!token) return config
config.headers = config.headers ?? {}
config.headers.Authorization = `Bearer ${token}`
if (token) {
config.headers.set('Authorization', `Bearer ${token}`)
}
// FormData: нельзя задавать Content-Type вручную (нужен boundary). Иначе сервер не видит файлы → { urls: [] }.
if (config.data instanceof FormData) {
delete config.headers['Content-Type']
config.headers.delete('Content-Type')
config.headers.delete('content-type')
}
return config
} catch {