Add centralized API client with axios interceptors, remove vue-axios
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,25 +1,17 @@
|
||||
import router from "@/router/index";
|
||||
import loadingStore from '@/stores/loading.js';
|
||||
import {useToast} from 'vue-toast-notification';
|
||||
import 'vue-toast-notification/dist/theme-sugar.css';
|
||||
import axios from "axios";
|
||||
import { deleteCookie } from "@/utils/cookieUtil.js";
|
||||
|
||||
// Delay loading and toast
|
||||
const delay = (s = 0) => new Promise((resolve, reject) => setTimeout(resolve, s));
|
||||
|
||||
/**
|
||||
* API catch error function
|
||||
* @param {object} Error 後端 ERROR
|
||||
* API catch error function.
|
||||
* 401 errors are handled by the axios response interceptor in api/client.js.
|
||||
* @param {object} error 後端 ERROR
|
||||
* @param {string} toastMessage Toast 的提示文字
|
||||
* @returns {string} Error HTTP Status
|
||||
*/
|
||||
export default async function apiError(error, toastMessage) {
|
||||
if(error.request?.status === 401) {
|
||||
delete axios.defaults.headers.common["Authorization"];
|
||||
deleteCookie("luciaToken");
|
||||
return router.push('/login');
|
||||
}
|
||||
const loading = loadingStore();
|
||||
const $toast = useToast();
|
||||
await delay();
|
||||
|
||||
Reference in New Issue
Block a user