- Read HF_BACKEND_BASE_URL from localStorage in api client - Refresh baseURL on each request interceptor - Persist backend_url from wizard config during app bootstrap - Persist backend_base_url after setup save
34 lines
740 B
TypeScript
34 lines
740 B
TypeScript
import axios from 'axios'
|
|
|
|
const getApiBase = () => {
|
|
return localStorage.getItem('HF_BACKEND_BASE_URL') || import.meta.env.VITE_API_BASE || 'http://127.0.0.1:8000'
|
|
}
|
|
|
|
const api = axios.create({
|
|
baseURL: getApiBase(),
|
|
})
|
|
|
|
api.interceptors.request.use((config) => {
|
|
config.baseURL = getApiBase()
|
|
const token = localStorage.getItem('token')
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`
|
|
}
|
|
return config
|
|
})
|
|
|
|
api.interceptors.response.use(
|
|
(res) => res,
|
|
(err) => {
|
|
if (err.response?.status === 401) {
|
|
localStorage.removeItem('token')
|
|
if (window.location.pathname !== '/login') {
|
|
window.location.href = '/login'
|
|
}
|
|
}
|
|
return Promise.reject(err)
|
|
}
|
|
)
|
|
|
|
export default api
|