improve WS message typing

This commit is contained in:
Matthias 2022-12-05 07:06:57 +01:00
parent be90f855a7
commit e9478bfaab
2 changed files with 42 additions and 3 deletions

View File

@ -39,7 +39,7 @@ import axios, { AxiosResponse } from 'axios';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
import { showAlert } from './alerts'; import { showAlert } from './alerts';
import { useWebSocket } from '@vueuse/core'; import { useWebSocket } from '@vueuse/core';
import { FtWsMessageTypes } from '@/types/wsMessageTypes'; import { FTWsMessage, FtWsMessageTypes } from '@/types/wsMessageTypes';
export function createBotSubStore(botId: string, botName: string) { export function createBotSubStore(botId: string, botName: string) {
const userService = useUserService(botId); const userService = useUserService(botId);
@ -811,7 +811,7 @@ export function createBotSubStore(botId: string, botName: string) {
} }
}, },
_handleWebsocketMessage(ws, event: MessageEvent<any>) { _handleWebsocketMessage(ws, event: MessageEvent<any>) {
const msg = JSON.parse(event.data); const msg: FTWsMessage = JSON.parse(event.data);
switch (msg.type) { switch (msg.type) {
case FtWsMessageTypes.whitelist: case FtWsMessageTypes.whitelist:
this.whitelist = msg.data; this.whitelist = msg.data;
@ -824,9 +824,13 @@ export function createBotSubStore(botId: string, botName: string) {
console.log('exitFill', msg); console.log('exitFill', msg);
showAlert(`Exit fill for ${msg.pair} at ${msg.open_rate}`, 'success'); showAlert(`Exit fill for ${msg.pair} at ${msg.open_rate}`, 'success');
break; break;
case FtWsMessageTypes.newCandle:
console.log('exitFill', msg);
showAlert(`New Candle`, 'success');
break;
default: default:
// Unhandled events ... // Unhandled events ...
console.log(`Received event ${msg.type}`); console.log(`Received event ${(msg as any).type}`);
break; break;
} }
}, },

View File

@ -4,3 +4,38 @@ export enum FtWsMessageTypes {
exitFill = 'exit_fill', exitFill = 'exit_fill',
newCandle = 'newCandle', newCandle = 'newCandle',
} }
export interface FtBaseWsMessage {
type: FtWsMessageTypes;
}
export interface FtWhitelistMessage extends FtBaseWsMessage {
type: FtWsMessageTypes.whitelist;
data: string[];
}
export interface FtEntryFillMessage extends FtBaseWsMessage {
type: FtWsMessageTypes.entryFill;
pair: string;
open_rate: number;
amount: number;
// ...
}
export interface FtExitFillMessage extends FtBaseWsMessage {
type: FtWsMessageTypes.exitFill;
pair: string;
open_rate: number;
amount: number;
// ...
}
export interface FtNewCandleMessage extends FtBaseWsMessage {
type: FtWsMessageTypes.newCandle;
// ...
}
export type FTWsMessage =
| FtWhitelistMessage
| FtEntryFillMessage
| FtExitFillMessage
| FtNewCandleMessage;