convert API to composable

This commit is contained in:
Matthias 2021-08-28 15:40:39 +02:00
parent 49059e5b83
commit dc6b60ba5c
3 changed files with 30 additions and 25 deletions

View File

@ -62,7 +62,6 @@
import { Component, Vue, Emit, Prop } from 'vue-property-decorator'; import { Component, Vue, Emit, Prop } from 'vue-property-decorator';
import { Action } from 'vuex-class'; import { Action } from 'vuex-class';
import userService from '@/shared/userService'; import userService from '@/shared/userService';
import { setBaseUrl } from '@/shared/apiService';
import { AuthPayload } from '@/types'; import { AuthPayload } from '@/types';
@ -132,7 +131,9 @@ export default class Login extends Vue {
.login(this.auth) .login(this.auth)
.then(() => { .then(() => {
this.setLoggedIn(true); this.setLoggedIn(true);
setBaseUrl(userService.getAPIUrl()); // TODO: Investigate how this needs to be done properly
// setBaseUrl(userService.getAPIUrl());
this.emitLoginResult(true); this.emitLoginResult(true);
if (this.inModal === false) { if (this.inModal === false) {
if (typeof this.$route.query.redirect === 'string') { if (typeof this.$route.query.redirect === 'string') {

View File

@ -1,17 +1,18 @@
import axios from 'axios'; import axios from 'axios';
import userService from './userService'; import { UserService } from './userService';
export const api = axios.create({
baseURL: userService.apiBase,
timeout: 10000,
withCredentials: true,
});
/** /**
* Initialize api so store is accessible. * Global store variable - keep a reference here to be able to emmit alerts
* @param store Vuex store
*/ */
export function initApi(store) { let globalStore;
export function useApi(userService: UserService) {
const api = axios.create({
baseURL: userService.getBaseUrl(),
timeout: 10000,
withCredentials: true,
});
// Sent auth headers interceptor
api.interceptors.request.use( api.interceptors.request.use(
(config) => { (config) => {
const custconfig = config; const custconfig = config;
@ -59,7 +60,7 @@ export function initApi(store) {
} }
if ((err.response && err.response.status === 500) || err.message === 'Network Error') { if ((err.response && err.response.status === 500) || err.message === 'Network Error') {
console.log('Bot not running...'); console.log('Bot not running...');
store.dispatch('setIsBotOnline', false); globalStore.dispatch('setIsBotOnline', false);
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -67,17 +68,17 @@ export function initApi(store) {
}); });
}, },
); );
return {
api,
};
} }
export function setBaseUrl(baseURL: string) { /**
if (baseURL === null) { * Initialize api so store is accessible.
// Reset to "local" baseurl * @param store Vuex store
api.defaults.baseURL = userService.apiBase; */
} else if (!baseURL.endsWith(userService.apiBase)) { export function initApi(store) {
api.defaults.baseURL = `${baseURL}${userService.apiBase}`; globalStore = store;
} else { //
api.defaults.baseURL = `${baseURL}${userService.apiBase}`;
}
} }
setBaseUrl(userService.getAPIUrl());

View File

@ -1,4 +1,5 @@
import { api } from '@/shared/apiService'; import { useApi } from '@/shared/apiService';
import userService from '@/shared/userService';
import { import {
BacktestResult, BacktestResult,
@ -85,6 +86,8 @@ export enum BotStoreGetters {
} }
export function createBotSubStore() { export function createBotSubStore() {
const { api } = useApi(userService);
return { return {
namespaced: true, namespaced: true,
state, state,