From dc6b60ba5c3b493d161c7feb1f7e44158d0eea5b Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 28 Aug 2021 15:40:39 +0200 Subject: [PATCH] convert API to composable --- src/components/Login.vue | 5 ++-- src/shared/apiService.ts | 45 ++++++++++++++++---------------- src/store/modules/ftbot/index.ts | 5 +++- 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/components/Login.vue b/src/components/Login.vue index 96e68ce6..0771d912 100644 --- a/src/components/Login.vue +++ b/src/components/Login.vue @@ -62,7 +62,6 @@ import { Component, Vue, Emit, Prop } from 'vue-property-decorator'; import { Action } from 'vuex-class'; import userService from '@/shared/userService'; -import { setBaseUrl } from '@/shared/apiService'; import { AuthPayload } from '@/types'; @@ -132,7 +131,9 @@ export default class Login extends Vue { .login(this.auth) .then(() => { this.setLoggedIn(true); - setBaseUrl(userService.getAPIUrl()); + // TODO: Investigate how this needs to be done properly + // setBaseUrl(userService.getAPIUrl()); + this.emitLoginResult(true); if (this.inModal === false) { if (typeof this.$route.query.redirect === 'string') { diff --git a/src/shared/apiService.ts b/src/shared/apiService.ts index 078f2eb0..241e6f5a 100644 --- a/src/shared/apiService.ts +++ b/src/shared/apiService.ts @@ -1,17 +1,18 @@ import axios from 'axios'; -import userService from './userService'; - -export const api = axios.create({ - baseURL: userService.apiBase, - timeout: 10000, - withCredentials: true, -}); +import { UserService } from './userService'; /** - * Initialize api so store is accessible. - * @param store Vuex store + * Global store variable - keep a reference here to be able to emmit alerts */ -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( (config) => { const custconfig = config; @@ -59,7 +60,7 @@ export function initApi(store) { } if ((err.response && err.response.status === 500) || err.message === 'Network Error') { console.log('Bot not running...'); - store.dispatch('setIsBotOnline', false); + globalStore.dispatch('setIsBotOnline', false); } return new Promise((resolve, reject) => { @@ -67,17 +68,17 @@ export function initApi(store) { }); }, ); + + return { + api, + }; } -export function setBaseUrl(baseURL: string) { - if (baseURL === null) { - // Reset to "local" baseurl - api.defaults.baseURL = userService.apiBase; - } else if (!baseURL.endsWith(userService.apiBase)) { - api.defaults.baseURL = `${baseURL}${userService.apiBase}`; - } else { - api.defaults.baseURL = `${baseURL}${userService.apiBase}`; - } +/** + * Initialize api so store is accessible. + * @param store Vuex store + */ +export function initApi(store) { + globalStore = store; + // } - -setBaseUrl(userService.getAPIUrl()); diff --git a/src/store/modules/ftbot/index.ts b/src/store/modules/ftbot/index.ts index fc62d7ae..7fa8394f 100644 --- a/src/store/modules/ftbot/index.ts +++ b/src/store/modules/ftbot/index.ts @@ -1,4 +1,5 @@ -import { api } from '@/shared/apiService'; +import { useApi } from '@/shared/apiService'; +import userService from '@/shared/userService'; import { BacktestResult, @@ -85,6 +86,8 @@ export enum BotStoreGetters { } export function createBotSubStore() { + const { api } = useApi(userService); + return { namespaced: true, state,