fix(): wsapi response mapping

This commit is contained in:
tiagosiebler
2025-01-17 13:46:49 +00:00
parent 56e945f591
commit 10b2af1c37
2 changed files with 11 additions and 18 deletions

View File

@@ -4,6 +4,7 @@ import {
CancelOrderParamsV5, CancelOrderParamsV5,
OrderParamsV5, OrderParamsV5,
} from '../request'; } from '../request';
import { OrderResultV5 } from '../response';
import { WsKey } from './ws-general'; import { WsKey } from './ws-general';
export type WSAPIOperation = 'order.create' | 'order.amend' | 'order.cancel'; export type WSAPIOperation = 'order.create' | 'order.amend' | 'order.cancel';
@@ -58,9 +59,6 @@ export interface WsAPITopicRequestParamMap {
// ping: undefined; // ping: undefined;
} }
export type WsAPITopicRequestParams =
WsAPITopicRequestParamMap[keyof WsAPITopicRequestParamMap];
export interface WSAPIResponse< export interface WSAPIResponse<
TResponseData extends object = object, TResponseData extends object = object,
TOperation extends WSAPIOperation = WSAPIOperation, TOperation extends WSAPIOperation = WSAPIOperation,
@@ -71,7 +69,7 @@ export interface WSAPIResponse<
retCode: 0 | number; retCode: 0 | number;
retMsg: 'OK' | string; retMsg: 'OK' | string;
op: TOperation; op: TOperation;
data: [TResponseData]; data: TResponseData;
header?: { header?: {
'X-Bapi-Limit': string; 'X-Bapi-Limit': string;
'X-Bapi-Limit-Status': string; 'X-Bapi-Limit-Status': string;
@@ -88,12 +86,10 @@ export interface WSAPIResponse<
// string: object; // string: object;
// } // }
export interface WsAPIOperationResponseMap< export interface WsAPIOperationResponseMap {
TResponseType extends object = object, 'order.create': WSAPIResponse<OrderResultV5, 'order.create'>;
> { 'order.amend': WSAPIResponse<OrderResultV5, 'order.amend'>;
'order.create': WSAPIResponse<TResponseType, 'order.cancel'>; 'order.cancel': WSAPIResponse<OrderResultV5, 'order.cancel'>;
'order.amend': WSAPIResponse<TResponseType, 'order.amend'>;
'order.cancel': WSAPIResponse<TResponseType, 'order.cancel'>;
ping: { ping: {
retCode: 0 | number; retCode: 0 | number;
retMsg: 'OK' | string; retMsg: 'OK' | string;

View File

@@ -696,6 +696,7 @@ export class WebsocketClient extends BaseWebsocketClient<WsKey> {
// WS API Exception // WS API Exception
if (isError) { if (isError) {
console.log('wsAPI error: ', parsed);
try { try {
this.getWsStore().rejectDeferredPromise( this.getWsStore().rejectDeferredPromise(
wsKey, wsKey,
@@ -908,19 +909,16 @@ export class WebsocketClient extends BaseWebsocketClient<WsKey> {
// This overload allows the caller to omit the 3rd param, if it isn't required (e.g. for the login call) // This overload allows the caller to omit the 3rd param, if it isn't required (e.g. for the login call)
async sendWSAPIRequest< async sendWSAPIRequest<
TWSKey extends keyof WsAPIWsKeyTopicMap, TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap,
TWSOperation extends TWSOperation extends
WsAPIWsKeyTopicMap[TWSKey] = WsAPIWsKeyTopicMap[TWSKey], WsAPIWsKeyTopicMap[TWSKey] = WsAPIWsKeyTopicMap[TWSKey],
TWSParams extends TWSParams extends
WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation], WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation],
TWSAPIResponse extends
| WsAPIOperationResponseMap[TWSOperation]
| object = WsAPIOperationResponseMap[TWSOperation],
>( >(
wsKey: TWSKey, wsKey: TWSKey,
operation: TWSOperation, operation: TWSOperation,
...params: TWSParams extends undefined ? [] : [TWSParams] ...params: TWSParams extends undefined ? [] : [TWSParams]
): Promise<TWSAPIResponse>; ): Promise<WsAPIOperationResponseMap[TWSOperation]>;
async sendWSAPIRequest< async sendWSAPIRequest<
TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap, TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap,
@@ -929,13 +927,12 @@ export class WebsocketClient extends BaseWebsocketClient<WsKey> {
TWSParams extends TWSParams extends
WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation], WsAPITopicRequestParamMap[TWSOperation] = WsAPITopicRequestParamMap[TWSOperation],
TWSAPIResponse extends TWSAPIResponse extends
| WsAPIOperationResponseMap[TWSOperation] WsAPIOperationResponseMap[TWSOperation] = WsAPIOperationResponseMap[TWSOperation],
| object = WsAPIOperationResponseMap[TWSOperation],
>( >(
wsKey: WsKey = WS_KEY_MAP.v5PrivateTrade, wsKey: WsKey = WS_KEY_MAP.v5PrivateTrade,
operation: TWSOperation, operation: TWSOperation,
params: TWSParams, params: TWSParams,
): Promise<any> { ): Promise<WsAPIOperationResponseMap[TWSOperation]> {
this.logger.trace(`sendWSAPIRequest(): assert "${wsKey}" is connected`); this.logger.trace(`sendWSAPIRequest(): assert "${wsKey}" is connected`);
await this.assertIsConnected(wsKey); await this.assertIsConnected(wsKey);
this.logger.trace('sendWSAPIRequest()->assertIsConnected() ok'); this.logger.trace('sendWSAPIRequest()->assertIsConnected() ok');