forked from verdnatura/salix-front
Reviewed-on: verdnatura/salix-front#928 Reviewed-by: Alex Moreno <alexm@verdnatura.es>
This commit is contained in:
commit
e57a253c6f
|
@ -4,9 +4,8 @@ import { Router } from 'src/router';
|
||||||
import useNotify from 'src/composables/useNotify.js';
|
import useNotify from 'src/composables/useNotify.js';
|
||||||
import { useStateQueryStore } from 'src/stores/useStateQueryStore';
|
import { useStateQueryStore } from 'src/stores/useStateQueryStore';
|
||||||
|
|
||||||
const session = useSession();
|
let session, notify, stateQuery;
|
||||||
const { notify } = useNotify();
|
|
||||||
const stateQuery = useStateQueryStore();
|
|
||||||
const baseUrl = '/api/';
|
const baseUrl = '/api/';
|
||||||
|
|
||||||
axios.defaults.baseURL = baseUrl;
|
axios.defaults.baseURL = baseUrl;
|
||||||
|
@ -51,9 +50,15 @@ const onResponseError = (error) => {
|
||||||
return Promise.reject(error);
|
return Promise.reject(error);
|
||||||
};
|
};
|
||||||
|
|
||||||
axios.interceptors.request.use(onRequest, onRequestError);
|
export function setupAxios() {
|
||||||
axios.interceptors.response.use(onResponse, onResponseError);
|
session = useSession();
|
||||||
axiosNoError.interceptors.request.use(onRequest);
|
notify = useNotify().notify;
|
||||||
axiosNoError.interceptors.response.use(onResponse);
|
stateQuery = useStateQueryStore();
|
||||||
|
|
||||||
|
axios.interceptors.request.use(onRequest, onRequestError);
|
||||||
|
axios.interceptors.response.use(onResponse, onResponseError);
|
||||||
|
axiosNoError.interceptors.request.use(onRequest);
|
||||||
|
axiosNoError.interceptors.response.use(onResponse);
|
||||||
|
}
|
||||||
|
|
||||||
export { onRequest, onResponseError, axiosNoError };
|
export { onRequest, onResponseError, axiosNoError };
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { boot } from 'quasar/wrappers';
|
import { boot } from 'quasar/wrappers';
|
||||||
import qFormMixin from './qformMixin';
|
import qFormMixin from './qformMixin';
|
||||||
import keyShortcut from './keyShortcut';
|
import keyShortcut from './keyShortcut';
|
||||||
|
import { setupAxios } from 'src/boot/axios';
|
||||||
import useNotify from 'src/composables/useNotify.js';
|
import useNotify from 'src/composables/useNotify.js';
|
||||||
import { CanceledError } from 'axios';
|
import { CanceledError } from 'axios';
|
||||||
|
|
||||||
|
@ -48,4 +49,5 @@ export default boot(({ app }) => {
|
||||||
|
|
||||||
notify(message ?? 'globals.error', 'negative', 'error');
|
notify(message ?? 'globals.error', 'negative', 'error');
|
||||||
};
|
};
|
||||||
|
setupAxios();
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,9 +8,13 @@ import useNotify from './useNotify';
|
||||||
import { useTokenConfig } from './useTokenConfig';
|
import { useTokenConfig } from './useTokenConfig';
|
||||||
const TOKEN_MULTIMEDIA = 'tokenMultimedia';
|
const TOKEN_MULTIMEDIA = 'tokenMultimedia';
|
||||||
const TOKEN = 'token';
|
const TOKEN = 'token';
|
||||||
|
let router;
|
||||||
|
export default {
|
||||||
|
setup() {
|
||||||
|
router = useRouter();
|
||||||
|
},
|
||||||
|
};
|
||||||
export function useSession() {
|
export function useSession() {
|
||||||
const router = useRouter();
|
|
||||||
const { notify } = useNotify();
|
const { notify } = useNotify();
|
||||||
let isCheckingToken = false;
|
let isCheckingToken = false;
|
||||||
let intervalId = null;
|
let intervalId = null;
|
||||||
|
|
|
@ -14,8 +14,8 @@ import { useUserConfig } from 'src/composables/useUserConfig';
|
||||||
import { useTokenConfig } from 'src/composables/useTokenConfig';
|
import { useTokenConfig } from 'src/composables/useTokenConfig';
|
||||||
import { useAcl } from 'src/composables/useAcl';
|
import { useAcl } from 'src/composables/useAcl';
|
||||||
|
|
||||||
const state = useState();
|
let state, session;
|
||||||
const session = useSession();
|
|
||||||
const { t, te } = i18n.global;
|
const { t, te } = i18n.global;
|
||||||
|
|
||||||
const createHistory = process.env.SERVER
|
const createHistory = process.env.SERVER
|
||||||
|
@ -43,8 +43,10 @@ const Router = createRouter({
|
||||||
* with the Router instance.
|
* with the Router instance.
|
||||||
*/
|
*/
|
||||||
export { Router };
|
export { Router };
|
||||||
export default route(function (/* { store, ssrContext } */) {
|
export default route((/* { store, ssrContext } */) => {
|
||||||
Router.beforeEach(async (to, from, next) => {
|
Router.beforeEach(async (to, from, next) => {
|
||||||
|
state = useState();
|
||||||
|
session = useSession();
|
||||||
const { isLoggedIn } = session;
|
const { isLoggedIn } = session;
|
||||||
const outLayout = Router.options.routes[0].children.map((r) => r.name);
|
const outLayout = Router.options.routes[0].children.map((r) => r.name);
|
||||||
if (!isLoggedIn() && !outLayout.includes(to.name)) {
|
if (!isLoggedIn() && !outLayout.includes(to.name)) {
|
||||||
|
|
|
@ -1,23 +1,38 @@
|
||||||
import { Notify } from 'quasar';
|
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||||
import { onRequest, onResponseError } from 'src/boot/axios';
|
import { setupAxios, onRequest, onResponseError } from 'src/boot/axios';
|
||||||
import { describe, expect, it, vi } from 'vitest';
|
import { useSession } from 'src/composables/useSession';
|
||||||
|
import useNotify from 'src/composables/useNotify';
|
||||||
|
import { useStateQueryStore } from 'src/stores/useStateQueryStore';
|
||||||
|
|
||||||
vi.mock('src/composables/useSession', () => ({
|
vi.mock('src/composables/useSession');
|
||||||
useSession: () => ({
|
vi.mock('src/composables/useNotify');
|
||||||
getToken: () => 'DEFAULT_TOKEN',
|
vi.mock('src/stores/useStateQueryStore');
|
||||||
isLoggedIn: () => vi.fn(),
|
|
||||||
destroy: () => vi.fn(),
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock('src/stores/useStateQueryStore', () => ({
|
|
||||||
useStateQueryStore: () => ({
|
|
||||||
add: () => vi.fn(),
|
|
||||||
remove: () => vi.fn(),
|
|
||||||
}),
|
|
||||||
}));
|
|
||||||
|
|
||||||
describe('Axios boot', () => {
|
describe('Axios boot', () => {
|
||||||
|
let sessionMock, notifyMock, stateQueryMock;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
sessionMock = {
|
||||||
|
getToken: vi.fn().mockReturnValue('DEFAULT_TOKEN'),
|
||||||
|
isLoggedIn: vi.fn().mockReturnValue(true),
|
||||||
|
destroy: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
notifyMock = {
|
||||||
|
notify: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
stateQueryMock = {
|
||||||
|
add: vi.fn(),
|
||||||
|
remove: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
useSession.mockReturnValue(sessionMock);
|
||||||
|
useNotify.mockReturnValue(notifyMock);
|
||||||
|
useStateQueryStore.mockReturnValue(stateQueryMock);
|
||||||
|
|
||||||
|
setupAxios();
|
||||||
|
});
|
||||||
describe('onRequest()', async () => {
|
describe('onRequest()', async () => {
|
||||||
it('should set the "Authorization" property on the headers', async () => {
|
it('should set the "Authorization" property on the headers', async () => {
|
||||||
const config = { headers: {} };
|
const config = { headers: {} };
|
||||||
|
|
Loading…
Reference in New Issue