From 10b2af1c37e13ffe5f373baeaa375234c963b8a0 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Fri, 17 Jan 2025 13:46:49 +0000 Subject: [PATCH] fix(): wsapi response mapping --- src/types/websockets/ws-api.ts | 16 ++++++---------- src/websocket-client.ts | 13 +++++-------- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/types/websockets/ws-api.ts b/src/types/websockets/ws-api.ts index ac52177b..b27f9dcc 100644 --- a/src/types/websockets/ws-api.ts +++ b/src/types/websockets/ws-api.ts @@ -4,6 +4,7 @@ import { CancelOrderParamsV5, OrderParamsV5, } from '../request'; +import { OrderResultV5 } from '../response'; import { WsKey } from './ws-general'; export type WSAPIOperation = 'order.create' | 'order.amend' | 'order.cancel'; @@ -58,9 +59,6 @@ export interface WsAPITopicRequestParamMap { // ping: undefined; } -export type WsAPITopicRequestParams = - WsAPITopicRequestParamMap[keyof WsAPITopicRequestParamMap]; - export interface WSAPIResponse< TResponseData extends object = object, TOperation extends WSAPIOperation = WSAPIOperation, @@ -71,7 +69,7 @@ export interface WSAPIResponse< retCode: 0 | number; retMsg: 'OK' | string; op: TOperation; - data: [TResponseData]; + data: TResponseData; header?: { 'X-Bapi-Limit': string; 'X-Bapi-Limit-Status': string; @@ -88,12 +86,10 @@ export interface WSAPIResponse< // string: object; // } -export interface WsAPIOperationResponseMap< - TResponseType extends object = object, -> { - 'order.create': WSAPIResponse; - 'order.amend': WSAPIResponse; - 'order.cancel': WSAPIResponse; +export interface WsAPIOperationResponseMap { + 'order.create': WSAPIResponse; + 'order.amend': WSAPIResponse; + 'order.cancel': WSAPIResponse; ping: { retCode: 0 | number; retMsg: 'OK' | string; diff --git a/src/websocket-client.ts b/src/websocket-client.ts index 827d267c..830178f2 100644 --- a/src/websocket-client.ts +++ b/src/websocket-client.ts @@ -696,6 +696,7 @@ export class WebsocketClient extends BaseWebsocketClient { // WS API Exception if (isError) { + console.log('wsAPI error: ', parsed); try { this.getWsStore().rejectDeferredPromise( wsKey, @@ -908,19 +909,16 @@ export class WebsocketClient extends BaseWebsocketClient { // This overload allows the caller to omit the 3rd param, if it isn't required (e.g. for the login call) async sendWSAPIRequest< - TWSKey extends keyof WsAPIWsKeyTopicMap, + TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap, TWSOperation extends WsAPIWsKeyTopicMap[TWSKey] = WsAPIWsKeyTopicMap[TWSKey], TWSParams extends WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation], - TWSAPIResponse extends - | WsAPIOperationResponseMap[TWSOperation] - | object = WsAPIOperationResponseMap[TWSOperation], >( wsKey: TWSKey, operation: TWSOperation, ...params: TWSParams extends undefined ? [] : [TWSParams] - ): Promise; + ): Promise; async sendWSAPIRequest< TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap, @@ -929,13 +927,12 @@ export class WebsocketClient extends BaseWebsocketClient { TWSParams extends WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation], TWSAPIResponse extends - | WsAPIOperationResponseMap[TWSOperation] - | object = WsAPIOperationResponseMap[TWSOperation], + WsAPIOperationResponseMap[TWSOperation] = WsAPIOperationResponseMap[TWSOperation], >( wsKey: WsKey = WS_KEY_MAP.v5PrivateTrade, operation: TWSOperation, params: TWSParams, - ): Promise { + ): Promise { this.logger.trace(`sendWSAPIRequest(): assert "${wsKey}" is connected`); await this.assertIsConnected(wsKey); this.logger.trace('sendWSAPIRequest()->assertIsConnected() ok');