feat(client): setup axios client
Signed-off-by: Noa Virellia <noa@requiem.garden>
This commit is contained in:
24
client/src/lib/axios.ts
Normal file
24
client/src/lib/axios.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import type { AxiosError } from 'axios';
|
||||
import axios from 'axios';
|
||||
|
||||
export const axiosClient = axios.create({
|
||||
baseURL: '/api/',
|
||||
});
|
||||
|
||||
axiosClient.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token !== null) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
axiosClient.interceptors.response.use(undefined, async (error: AxiosError) => {
|
||||
if (error.response && error?.response.status === 401) {
|
||||
// TODO: refresh token
|
||||
if (error.config) {
|
||||
return axiosClient(error.config);
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
});
|
||||
Reference in New Issue
Block a user