From e9478bfaab9d61244ea90e8afac91610d403b11b Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 5 Dec 2022 07:06:57 +0100 Subject: [PATCH] improve WS message typing --- src/stores/ftbot.ts | 10 +++++++--- src/types/wsMessageTypes.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/stores/ftbot.ts b/src/stores/ftbot.ts index 3e52f803..2cde0d35 100644 --- a/src/stores/ftbot.ts +++ b/src/stores/ftbot.ts @@ -39,7 +39,7 @@ import axios, { AxiosResponse } from 'axios'; import { defineStore } from 'pinia'; import { showAlert } from './alerts'; import { useWebSocket } from '@vueuse/core'; -import { FtWsMessageTypes } from '@/types/wsMessageTypes'; +import { FTWsMessage, FtWsMessageTypes } from '@/types/wsMessageTypes'; export function createBotSubStore(botId: string, botName: string) { const userService = useUserService(botId); @@ -811,7 +811,7 @@ export function createBotSubStore(botId: string, botName: string) { } }, _handleWebsocketMessage(ws, event: MessageEvent) { - const msg = JSON.parse(event.data); + const msg: FTWsMessage = JSON.parse(event.data); switch (msg.type) { case FtWsMessageTypes.whitelist: this.whitelist = msg.data; @@ -824,9 +824,13 @@ export function createBotSubStore(botId: string, botName: string) { console.log('exitFill', msg); showAlert(`Exit fill for ${msg.pair} at ${msg.open_rate}`, 'success'); break; + case FtWsMessageTypes.newCandle: + console.log('exitFill', msg); + showAlert(`New Candle`, 'success'); + break; default: // Unhandled events ... - console.log(`Received event ${msg.type}`); + console.log(`Received event ${(msg as any).type}`); break; } }, diff --git a/src/types/wsMessageTypes.ts b/src/types/wsMessageTypes.ts index 09d9539d..ea1735e9 100644 --- a/src/types/wsMessageTypes.ts +++ b/src/types/wsMessageTypes.ts @@ -4,3 +4,38 @@ export enum FtWsMessageTypes { exitFill = 'exit_fill', 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;