import axios, {AxiosRequestConfig, AxiosResponse} from 'axios'; import {ElNotification} from 'element-plus'; import {getEmptyResponseWithListData, getRequestBaseUrl} from '@/utils/request'; import {Router} from 'vue-router'; import {translate} from '@/utils/i18n'; const t = translate; // initialize request export const initRequest = (router?: Router) => { // TODO: request interception // response interception let msgBoxVisible = false; axios.interceptors.response.use(res => { return res; }, async err => { // status code const status = err?.response?.status; if (status === 401) { // 401 error if (window.localStorage.getItem('token')) { // token expired, popup logout warning if (msgBoxVisible) return; msgBoxVisible = true; setTimeout(() => msgBoxVisible = false, 5000); router?.push('/login'); await ElNotification({ title: t('common.status.unauthorized'), message: t('common.notification.loggedOut'), type: 'warning', }); } else { // token not exists, redirect to login page router?.push('/login'); } } else { // other errors console.error(err); } }); }; const useRequest = () => { const getHeaders = (): any => { // headers const headers = {} as any; // add token to headers const token = localStorage.getItem('token'); if (token) { headers['Authorization'] = token; } return headers; }; const request = async (opts: AxiosRequestConfig): Promise => { // base url const baseURL = getRequestBaseUrl(); // headers const headers = getHeaders(); // axios response const res = await axios.request({ ...opts, baseURL, headers, }); // response data return res?.data; }; const get = async , PM = any>(url: string, params?: PM, opts?: AxiosRequestConfig): Promise => { opts = { ...opts, method: 'GET', url, params, }; return await request(opts); }; const post = async , PM = any>(url: string, data?: T, params?: PM, opts?: AxiosRequestConfig): Promise => { opts = { ...opts, method: 'POST', url, data, params, }; return await request(opts); }; const put = async , PM = any>(url: string, data?: T, params?: PM, opts?: AxiosRequestConfig): Promise => { opts = { ...opts, method: 'PUT', url, data, params, }; return await request(opts); }; const del = async , PM = any>(url: string, data?: T, params?: PM, opts?: AxiosRequestConfig): Promise => { opts = { ...opts, method: 'DELETE', url, data, params, }; return await request(opts); }; const getList = async (url: string, params?: ListRequestParams, opts?: AxiosRequestConfig) => { // normalize conditions if (params && Array.isArray(params.conditions)) { params.conditions = JSON.stringify(params.conditions); } // get request const res = await get, ListRequestParams>(url, params, opts); // if no response, return empty if (!res) { return getEmptyResponseWithListData(); } // normalize array data if (!res.data) { res.data = []; } return res; }; const getAll = async (url: string, opts?: AxiosRequestConfig) => { return await getList(url, {all: true}, opts); }; const postList = async (url: string, data?: T[], params?: PM, opts?: AxiosRequestConfig): Promise => { return await post(url, data, params, opts); }; const putList = async (url: string, data?: BatchRequestPayloadWithJsonStringData, params?: PM, opts?: AxiosRequestConfig): Promise => { return await put(url, data, params, opts); }; const delList = async (url: string, data?: BatchRequestPayload, params?: PM, opts?: AxiosRequestConfig): Promise => { return await del(url, data, params, opts); }; const requestRaw = async (opts: AxiosRequestConfig): Promise => { // base url const baseURL = getRequestBaseUrl(); // headers const headers = getHeaders(); // axios response return await axios.request({ ...opts, baseURL, headers, }); }; const getRaw = async (url: string, params?: PM, opts?: AxiosRequestConfig): Promise => { opts = { ...opts, method: 'GET', url, params, }; return await requestRaw(opts); }; return { // public variables and methods request, get, post, put, del, getList, getAll, postList, putList, delList, requestRaw, getRaw, }; }; export default useRequest;