Add a few axios response types

This commit is contained in:
Matthias 2021-10-14 21:31:25 +02:00
parent 09dbfb9257
commit 576aca8011
3 changed files with 56 additions and 24 deletions

View File

@ -31,6 +31,10 @@ import {
LogLine, LogLine,
BacktestSteps, BacktestSteps,
SysInfoResponse, SysInfoResponse,
AvailablePairResult,
StatusResponse,
DeleteTradeResponse,
BlacklistResponse,
} from '@/types'; } from '@/types';
import { import {
@ -38,7 +42,7 @@ import {
getAllPlotConfigNames, getAllPlotConfigNames,
storePlotConfigName, storePlotConfigName,
} from '@/shared/storage'; } from '@/shared/storage';
import axios, { AxiosInstance } from 'axios'; import axios, { AxiosInstance, AxiosResponse } from 'axios';
import state, { FtbotStateType } from './state'; import state, { FtbotStateType } from './state';
import { showAlert } from '../alerts'; import { showAlert } from '../alerts';
@ -517,7 +521,9 @@ export function createBotSubStore(botId: string, botName: string) {
let totalTrades = 0; let totalTrades = 0;
const pageLength = 500; const pageLength = 500;
const fetchTrades = async (limit: number, offset: number) => { const fetchTrades = async (limit: number, offset: number) => {
return api.get('/trades', { params: { limit, offset } }); return api.get<unknown, AxiosResponse<TradeResponse>>('/trades', {
params: { limit, offset },
});
}; };
const res = await fetchTrades(pageLength, 0); const res = await fetchTrades(pageLength, 0);
const result: TradeResponse = res.data; const result: TradeResponse = res.data;
@ -652,7 +658,7 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.getStrategyPlotConfig]({ commit }) { async [BotStoreActions.getStrategyPlotConfig]({ commit }) {
try { try {
const result = await api.get('/plot_config'); const result = await api.get<never, AxiosResponse<PlotConfig>>('/plot_config');
const plotConfig = result.data; const plotConfig = result.data;
if (plotConfig.subplots === null) { if (plotConfig.subplots === null) {
// Subplots should not be null but an empty object // Subplots should not be null but an empty object
@ -687,9 +693,12 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.getAvailablePairs]({ commit }, payload: AvailablePairPayload) { async [BotStoreActions.getAvailablePairs]({ commit }, payload: AvailablePairPayload) {
try { try {
const result = await api.get('/available_pairs', { const result = await api.get<AvailablePairPayload, AxiosResponse<AvailablePairResult>>(
params: { ...payload }, '/available_pairs',
}); {
params: { ...payload },
},
);
// result is of type AvailablePairResult // result is of type AvailablePairResult
const { pairs } = result.data; const { pairs } = result.data;
commit('updatePairs', pairs); commit('updatePairs', pairs);
@ -770,7 +779,7 @@ export function createBotSubStore(botId: string, botName: string) {
// TODO: Migrate calls to API to a seperate module unrelated to vuex? // TODO: Migrate calls to API to a seperate module unrelated to vuex?
async [BotStoreActions.startBot]({ dispatch }) { async [BotStoreActions.startBot]({ dispatch }) {
try { try {
const res = await api.post('/start', {}); const res = await api.post<{}, AxiosResponse<StatusResponse>>('/start', {});
console.log(res.data); console.log(res.data);
showAlert(dispatch, res.data.status); showAlert(dispatch, res.data.status);
return Promise.resolve(res); return Promise.resolve(res);
@ -784,7 +793,7 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.stopBot]({ dispatch }) { async [BotStoreActions.stopBot]({ dispatch }) {
try { try {
const res = await api.post('/stop', {}); const res = await api.post<{}, AxiosResponse<StatusResponse>>('/stop', {});
showAlert(dispatch, res.data.status); showAlert(dispatch, res.data.status);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
@ -797,7 +806,7 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.stopBuy]({ dispatch }) { async [BotStoreActions.stopBuy]({ dispatch }) {
try { try {
const res = await api.post('/stopbuy', {}); const res = await api.post<{}, AxiosResponse<StatusResponse>>('/stopbuy', {});
showAlert(dispatch, res.data.status); showAlert(dispatch, res.data.status);
return Promise.resolve(res); return Promise.resolve(res);
} catch (error) { } catch (error) {
@ -810,7 +819,7 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.reloadConfig]({ dispatch }) { async [BotStoreActions.reloadConfig]({ dispatch }) {
try { try {
const res = await api.post('/reload_config', {}); const res = await api.post<{}, AxiosResponse<StatusResponse>>('/reload_config', {});
console.log(res.data); console.log(res.data);
showAlert(dispatch, res.data.status); showAlert(dispatch, res.data.status);
return Promise.resolve(res); return Promise.resolve(res);
@ -824,7 +833,9 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.deleteTrade]({ dispatch }, tradeid: string) { async [BotStoreActions.deleteTrade]({ dispatch }, tradeid: string) {
try { try {
const res = await api.delete(`/trades/${tradeid}`); const res = await api.delete<never, AxiosResponse<DeleteTradeResponse>>(
`/trades/${tradeid}`,
);
showAlert( showAlert(
dispatch, dispatch,
res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}`, res.data.result_msg ? res.data.result_msg : `Deleted Trade ${tradeid}`,
@ -869,7 +880,10 @@ export function createBotSubStore(botId: string, botName: string) {
async [BotStoreActions.forcebuy]({ dispatch }, payload: ForcebuyPayload) { async [BotStoreActions.forcebuy]({ dispatch }, payload: ForcebuyPayload) {
if (payload && payload.pair) { if (payload && payload.pair) {
try { try {
const res = await api.post('/forcebuy', payload); const res = await api.post<
ForcebuyPayload,
AxiosResponse<StatusResponse | TradeResponse>
>('/forcebuy', payload);
showAlert(dispatch, `Buy order for ${payload.pair} created.`); showAlert(dispatch, `Buy order for ${payload.pair} created.`);
return Promise.resolve(res); return Promise.resolve(res);
@ -894,7 +908,10 @@ export function createBotSubStore(botId: string, botName: string) {
console.log(`Adding ${payload} to blacklist`); console.log(`Adding ${payload} to blacklist`);
if (payload && payload.blacklist) { if (payload && payload.blacklist) {
try { try {
const result = await api.post('/blacklist', payload); const result = await api.post<BlacklistPayload, AxiosResponse<BlacklistResponse>>(
'/blacklist',
payload,
);
commit('updateBlacklist', result.data); commit('updateBlacklist', result.data);
if (result.data.errors && Object.keys(result.data.errors).length !== 0) { if (result.data.errors && Object.keys(result.data.errors).length !== 0) {
const { errors } = result.data; const { errors } = result.data;
@ -935,7 +952,7 @@ export function createBotSubStore(botId: string, botName: string) {
} }
}, },
async [BotStoreActions.pollBacktest]({ commit }) { async [BotStoreActions.pollBacktest]({ commit }) {
const result = await api.get('/backtest'); const result = await api.get<never, AxiosResponse<BacktestStatus>>('/backtest');
commit('updateBacktestRunning', result.data); commit('updateBacktestRunning', result.data);
if (result.data.running === false && result.data.backtest_result) { if (result.data.running === false && result.data.backtest_result) {
commit('updateBacktestResult', result.data.backtest_result); commit('updateBacktestResult', result.data.backtest_result);
@ -944,18 +961,18 @@ export function createBotSubStore(botId: string, botName: string) {
async [BotStoreActions.removeBacktest]({ commit }) { async [BotStoreActions.removeBacktest]({ commit }) {
commit('resetBacktestHistory'); commit('resetBacktestHistory');
try { try {
const result = await api.delete('/backtest'); const { data } = await api.delete<never, AxiosResponse<BacktestStatus>>('/backtest');
commit('updateBacktestRunning', result.data); commit('updateBacktestRunning', data);
return Promise.resolve(result.data); return Promise.resolve(data);
} catch (err) { } catch (err) {
return Promise.reject(err); return Promise.reject(err);
} }
}, },
async [BotStoreActions.stopBacktest]({ commit }) { async [BotStoreActions.stopBacktest]({ commit }) {
try { try {
const result = await api.get('/backtest/abort'); const { data } = await api.get<never, AxiosResponse<BacktestStatus>>('/backtest/abort');
commit('updateBacktestRunning', result.data); commit('updateBacktestRunning', data);
return Promise.resolve(result.data); return Promise.resolve(data);
} catch (err) { } catch (err) {
return Promise.reject(err); return Promise.reject(err);
} }
@ -965,9 +982,9 @@ export function createBotSubStore(botId: string, botName: string) {
}, },
async [BotStoreActions.sysInfo]({ commit }) { async [BotStoreActions.sysInfo]({ commit }) {
try { try {
const result = await api.get('/sysinfo'); const { data } = await api.get('/sysinfo');
commit('updateSysInfo', result.data); commit('updateSysInfo', data);
return Promise.resolve(result.data); return Promise.resolve(data);
} catch (err) { } catch (err) {
return Promise.reject(err); return Promise.reject(err);
} }

View File

@ -2,11 +2,15 @@ export interface BlacklistPayload {
blacklist: Array<string>; blacklist: Array<string>;
} }
export interface BlacklistErrMsg {
error_msg: string;
}
export interface BlacklistResponse { export interface BlacklistResponse {
method: Array<string>; method: Array<string>;
length: number; length: number;
blacklist: Array<string>; blacklist: Array<string>;
errors: Record<string, string>; errors: Record<string, BlacklistErrMsg>;
} }
export interface WhitelistResponse { export interface WhitelistResponse {

View File

@ -139,3 +139,14 @@ export interface SysInfoResponse {
cpu_pct: number[]; cpu_pct: number[];
ram_pct: number; ram_pct: number;
} }
export interface StatusResponse {
status: string;
}
export interface DeleteTradeResponse {
cancel_order_count: number;
result: string;
result_msg: string;
trade_id: number;
}