42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import axios, { type AxiosError, type AxiosResponse } from 'axios';
|
|
// import { useAuthStore } from '~/stores/login';
|
|
// import { useDefaultStore, useLoadingStore } from '~/stores';
|
|
|
|
const baseURL = import.meta.env.VITE_API_URL as string;
|
|
|
|
export const useAxios = () => {
|
|
// const loadingStore = useLoadingStore();
|
|
// const defaultStore = useDefaultStore();
|
|
// const { siteInfo } = storeToRefs(defaultStore);
|
|
// const authStore = useAuthStore();
|
|
const router = useRouter();
|
|
|
|
const instance = axios.create({
|
|
baseURL,
|
|
withCredentials: true
|
|
});
|
|
|
|
instance.interceptors.request.use(
|
|
(config) => {
|
|
return Promise.resolve(config);
|
|
},
|
|
(error: AxiosError) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
instance.interceptors.response.use(
|
|
(response: AxiosResponse<any, any>) => {
|
|
return Promise.resolve(response);
|
|
},
|
|
(error: AxiosError) => {
|
|
if (error.status === 403) {
|
|
return router.push('/');
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
return instance;
|
|
};
|