Spaces:
Runtime error
Runtime error
Download frontend/src/api/client.ts from SII-gelingli/echem-agent: direct link, hf CLI and curl.
- Browser
- Download file 1.45 kB
-
https://huggingface.co/spaces/SII-gelingli/echem-agent/resolve/main/frontend/src/api/client.ts
- Command line
-
hf download hf://spaces/SII-gelingli/echem-agent/frontend/src/api/client.ts
-
curl -L -o client.ts https://huggingface.co/spaces/SII-gelingli/echem-agent/resolve/main/frontend/src/api/client.ts
1.45 kB
| import axios from 'axios'; | |
| const apiClient = axios.create({ | |
| baseURL: '/api', | |
| headers: { 'Content-Type': 'application/json' }, | |
| }); | |
| // Request interceptor: attach access token | |
| apiClient.interceptors.request.use((config) => { | |
| const token = localStorage.getItem('access_token'); | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }); | |
| // Response interceptor: auto-refresh on 401 | |
| apiClient.interceptors.response.use( | |
| (res) => res, | |
| async (error) => { | |
| const original = error.config; | |
| if (error.response?.status === 401 && !original._retry) { | |
| original._retry = true; | |
| const refreshToken = localStorage.getItem('refresh_token'); | |
| if (refreshToken) { | |
| try { | |
| const res = await axios.post('/api/auth/refresh', { | |
| refresh_token: refreshToken, | |
| }); | |
| const { access_token, refresh_token } = res.data; | |
| localStorage.setItem('access_token', access_token); | |
| localStorage.setItem('refresh_token', refresh_token); | |
| original.headers.Authorization = `Bearer ${access_token}`; | |
| return apiClient(original); | |
| } catch { | |
| localStorage.removeItem('access_token'); | |
| localStorage.removeItem('refresh_token'); | |
| window.location.href = '/login'; | |
| } | |
| } else { | |
| window.location.href = '/login'; | |
| } | |
| } | |
| return Promise.reject(error); | |
| }, | |
| ); | |
| export default apiClient; | |