2022-12-20 11:00:36 +00:00
|
|
|
import axios from 'axios';
|
2023-03-15 10:28:18 +00:00
|
|
|
import { Notify } from 'quasar';
|
2022-12-20 11:30:25 +00:00
|
|
|
import { useSession } from 'src/composables/useSession';
|
2023-06-15 09:46:15 +00:00
|
|
|
import { Router } from 'src/router';
|
2023-03-15 10:28:18 +00:00
|
|
|
import { i18n } from './i18n';
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
const session = useSession();
|
|
|
|
const { t } = i18n.global;
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
axios.defaults.baseURL = '/api/';
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
const onRequest = (config) => {
|
|
|
|
const token = session.getToken();
|
|
|
|
if (token.length && config.headers) {
|
|
|
|
config.headers.Authorization = token;
|
|
|
|
}
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
return config;
|
|
|
|
};
|
2022-12-20 11:00:36 +00:00
|
|
|
|
2023-03-15 10:28:18 +00:00
|
|
|
const onRequestError = (error) => {
|
|
|
|
return Promise.reject(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onResponse = (response) => {
|
|
|
|
const { method } = response.config;
|
|
|
|
|
|
|
|
const isSaveRequest = method === 'patch';
|
|
|
|
if (isSaveRequest) {
|
|
|
|
Notify.create({
|
|
|
|
message: t('globals.dataSaved'),
|
|
|
|
type: 'positive',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
|
|
|
const onResponseError = (error) => {
|
|
|
|
let message = '';
|
|
|
|
|
|
|
|
const response = error.response;
|
|
|
|
const responseData = response && response.data;
|
|
|
|
const responseError = responseData && response.data.error;
|
|
|
|
if (responseError) {
|
|
|
|
message = responseError.message;
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:59:41 +00:00
|
|
|
switch (response?.status) {
|
2023-03-15 10:28:18 +00:00
|
|
|
case 500:
|
|
|
|
message = 'errors.statusInternalServerError';
|
|
|
|
break;
|
|
|
|
case 502:
|
|
|
|
message = 'errors.statusBadGateway';
|
|
|
|
break;
|
|
|
|
case 504:
|
|
|
|
message = 'errors.statusGatewayTimeout';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-10-11 13:59:41 +00:00
|
|
|
if (session.isLoggedIn() && response?.status === 401) {
|
2023-03-15 10:28:18 +00:00
|
|
|
session.destroy();
|
2023-06-15 09:37:54 +00:00
|
|
|
const hash = window.location.hash;
|
|
|
|
const url = hash.slice(1);
|
2023-06-15 09:46:15 +00:00
|
|
|
Router.push({ path: url });
|
2023-06-15 09:37:54 +00:00
|
|
|
} else if (!session.isLoggedIn()) {
|
2023-08-04 11:29:31 +00:00
|
|
|
return Promise.reject(error);
|
2023-03-15 10:28:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Notify.create({
|
|
|
|
message: t(message),
|
|
|
|
type: 'negative',
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
axios.interceptors.request.use(onRequest, onRequestError);
|
|
|
|
axios.interceptors.response.use(onResponse, onResponseError);
|
2023-03-16 09:54:19 +00:00
|
|
|
|
2023-06-15 09:37:54 +00:00
|
|
|
export { onRequest, onResponseError };
|