deploy
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user