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 { 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') {

View File

@ -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());

View File

@ -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,