2021-01-30 04:45:24 +00:00
|
|
|
import config from 'src/config';
|
2020-10-02 07:13:14 +00:00
|
|
|
import { boot } from 'quasar/wrappers';
|
2021-01-30 04:45:24 +00:00
|
|
|
import { LocalStorage, Notify } from 'quasar';
|
|
|
|
import axios, { AxiosError, AxiosInstance } from 'axios';
|
|
|
|
import { UserSessionState } from 'src/plugins/user/store';
|
2020-10-02 07:13:14 +00:00
|
|
|
|
2021-01-30 04:45:24 +00:00
|
|
|
declare module '@vue/runtime-core' {
|
|
|
|
interface ComponentCustomProperties {
|
2020-10-02 07:13:14 +00:00
|
|
|
$axios: AxiosInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 04:45:24 +00:00
|
|
|
const api = axios.create({
|
|
|
|
baseURL: <string | undefined>LocalStorage.getItem('baseURL') || config.baseURL,
|
|
|
|
});
|
2021-01-29 10:51:56 +00:00
|
|
|
|
2021-01-30 04:45:24 +00:00
|
|
|
export default boot<UserSessionState>(({ app, store, router }) => {
|
2020-11-06 00:15:40 +00:00
|
|
|
/***
|
|
|
|
* Intercept requests and insert Token if available
|
|
|
|
*/
|
2021-01-30 04:45:24 +00:00
|
|
|
api.interceptors.request.use((config) => {
|
|
|
|
const session = store.state.sessions.currentSession;
|
2020-11-04 22:53:10 +00:00
|
|
|
if (session?.token) {
|
|
|
|
config.headers = { Authorization: 'Bearer ' + session.token };
|
2020-10-16 07:38:14 +00:00
|
|
|
}
|
|
|
|
return config;
|
|
|
|
});
|
2020-11-06 00:15:40 +00:00
|
|
|
|
|
|
|
/***
|
2020-11-06 09:52:51 +00:00
|
|
|
* Intercept responses
|
|
|
|
* - filter 401 --> logout
|
2020-11-15 17:51:30 +00:00
|
|
|
* - filter timeout or 502-504 --> backendOffline
|
2020-11-06 00:15:40 +00:00
|
|
|
*/
|
|
|
|
axios.interceptors.response.use(
|
2021-03-18 16:23:57 +00:00
|
|
|
(response) => response,
|
|
|
|
(error) => {
|
2020-11-06 09:52:51 +00:00
|
|
|
if (error) {
|
2020-11-06 00:15:40 +00:00
|
|
|
const e = <AxiosError>error;
|
2020-11-09 02:40:51 +00:00
|
|
|
if (
|
|
|
|
e.code === 'ECONNABORTED' ||
|
2020-11-15 17:51:30 +00:00
|
|
|
(e.response && e.response.status >= 502 && e.response.status <= 504)
|
2020-11-09 02:40:51 +00:00
|
|
|
) {
|
2021-02-04 01:43:53 +00:00
|
|
|
const current = router.currentRoute.value;
|
|
|
|
let next = current.path;
|
|
|
|
if ((current.name == 'login' || current.name == 'offline') && current.query.redirect)
|
|
|
|
next = <string>current.query.redirect;
|
2020-11-15 17:51:30 +00:00
|
|
|
return router.push({
|
|
|
|
name: 'offline',
|
2021-02-04 01:43:53 +00:00
|
|
|
query: { redirect: next },
|
2020-11-14 14:04:42 +00:00
|
|
|
});
|
2020-11-15 17:51:30 +00:00
|
|
|
} else if (e.response && e.response.status == 401) {
|
2021-01-30 04:45:24 +00:00
|
|
|
if (router.currentRoute.value.name !== 'login')
|
|
|
|
return store.dispatch('session/clearCurrent');
|
2020-11-06 00:15:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
);
|
2021-01-30 04:45:24 +00:00
|
|
|
// for use inside Vue files (Options API) through this.$axios and this.$api
|
|
|
|
|
|
|
|
app.config.globalProperties.$axios = axios;
|
|
|
|
// ^ ^ ^ this will allow you to use this.$axios (for Vue Options API form)
|
|
|
|
// so you won't necessarily have to import axios in each vue file
|
|
|
|
|
|
|
|
app.config.globalProperties.$api = api;
|
|
|
|
// ^ ^ ^ this will allow you to use this.$api (for Vue Options API form)
|
|
|
|
// so you can easily perform requests against your app's API
|
2020-10-02 07:13:14 +00:00
|
|
|
});
|
2020-10-15 09:23:41 +00:00
|
|
|
|
2021-01-30 04:45:24 +00:00
|
|
|
export { axios, api };
|
|
|
|
|
|
|
|
export const setBaseUrl = (url: string) => {
|
|
|
|
LocalStorage.set('baseURL', url);
|
|
|
|
axios.defaults.baseURL = url;
|
|
|
|
Notify.create({
|
|
|
|
message: 'Serveraddresse gespeichert',
|
|
|
|
position: 'bottom',
|
|
|
|
caption: `${url}`,
|
|
|
|
color: 'positive',
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.reload();
|
|
|
|
}, 5000);
|
|
|
|
};
|