using typeguards and removed useless logging
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
import { WebsocketTopicSubscriptionConfirmationEvent } from './topic-subscription-confirmation';
|
||||||
|
|
||||||
|
export interface WebsocketFailedTopicSubscriptionConfirmationEvent
|
||||||
|
extends WebsocketTopicSubscriptionConfirmationEvent {
|
||||||
|
success: false;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
import { WebsocketTopicSubscriptionConfirmationEvent } from './topic-subscription-confirmation';
|
||||||
|
|
||||||
|
export interface WebsocketSucceededTopicSubscriptionConfirmationEvent
|
||||||
|
extends WebsocketTopicSubscriptionConfirmationEvent {
|
||||||
|
success: true;
|
||||||
|
}
|
||||||
7
src/types/ws-events/topic-subscription-confirmation.ts
Normal file
7
src/types/ws-events/topic-subscription-confirmation.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export interface WebsocketTopicSubscriptionConfirmationEvent {
|
||||||
|
op: 'subscribe';
|
||||||
|
req_id: string;
|
||||||
|
conn_id: string;
|
||||||
|
ret_msg: string;
|
||||||
|
success: boolean;
|
||||||
|
}
|
||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import { WebsocketSucceededTopicSubscriptionConfirmationEvent } from '../types/ws-events/succeeded-topic-subscription-confirmation';
|
||||||
|
import { WebsocketTopicSubscriptionConfirmationEvent } from '../types/ws-events/topic-subscription-confirmation';
|
||||||
|
|
||||||
export interface RestClientOptions {
|
export interface RestClientOptions {
|
||||||
/** Your API key */
|
/** Your API key */
|
||||||
key?: string;
|
key?: string;
|
||||||
@@ -57,7 +60,7 @@ export function serializeParams(
|
|||||||
params: object = {},
|
params: object = {},
|
||||||
strict_validation = false,
|
strict_validation = false,
|
||||||
sortProperties = true,
|
sortProperties = true,
|
||||||
encodeSerialisedValues = true
|
encodeSerialisedValues = true,
|
||||||
): string {
|
): string {
|
||||||
const properties = sortProperties
|
const properties = sortProperties
|
||||||
? Object.keys(params).sort()
|
? Object.keys(params).sort()
|
||||||
@@ -71,7 +74,7 @@ export function serializeParams(
|
|||||||
|
|
||||||
if (strict_validation === true && typeof value === 'undefined') {
|
if (strict_validation === true && typeof value === 'undefined') {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
'Failed to sign API request due to undefined parameter'
|
'Failed to sign API request due to undefined parameter',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return `${key}=${value}`;
|
return `${key}=${value}`;
|
||||||
@@ -81,7 +84,7 @@ export function serializeParams(
|
|||||||
|
|
||||||
export function getRestBaseUrl(
|
export function getRestBaseUrl(
|
||||||
useTestnet: boolean,
|
useTestnet: boolean,
|
||||||
restInverseOptions: RestClientOptions
|
restInverseOptions: RestClientOptions,
|
||||||
): string {
|
): string {
|
||||||
const exchangeBaseUrls = {
|
const exchangeBaseUrls = {
|
||||||
livenet: 'https://api.bybit.com',
|
livenet: 'https://api.bybit.com',
|
||||||
@@ -124,30 +127,30 @@ export function isWsPong(msg: any): boolean {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
export function isTopicSubscriptionConfirmation(
|
||||||
export function isTopicSubscriptionConfirmation(msg: any): boolean {
|
msg: unknown,
|
||||||
|
): msg is WebsocketTopicSubscriptionConfirmationEvent {
|
||||||
|
if (typeof msg !== 'object') {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (!msg) {
|
if (!msg) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (typeof msg['op'] !== 'string') {
|
||||||
if (!msg['op'] || msg['op'] !== 'subscribe') {
|
return false;
|
||||||
|
}
|
||||||
|
if (msg['op'] !== 'subscribe') {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
export function isTopicSubscriptionSuccess(
|
||||||
export function isTopicSubscriptionSuccess(msg: any): boolean {
|
msg: unknown,
|
||||||
if (!msg) {
|
): msg is WebsocketSucceededTopicSubscriptionConfirmationEvent {
|
||||||
return false;
|
if (!isTopicSubscriptionConfirmation(msg)) return false;
|
||||||
}
|
return msg.success === true;
|
||||||
|
|
||||||
if (!msg['op'] || msg['op'] !== 'subscribe') {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return msg['success'] === true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const APIID = 'bybitapinode';
|
export const APIID = 'bybitapinode';
|
||||||
@@ -165,4 +168,4 @@ export const REST_CLIENT_TYPE_ENUM = {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type RestClientType =
|
export type RestClientType =
|
||||||
typeof REST_CLIENT_TYPE_ENUM[keyof typeof REST_CLIENT_TYPE_ENUM];
|
(typeof REST_CLIENT_TYPE_ENUM)[keyof typeof REST_CLIENT_TYPE_ENUM];
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ import {
|
|||||||
serializeParams,
|
serializeParams,
|
||||||
} from './util';
|
} from './util';
|
||||||
import { RestClientV5 } from './rest-client-v5';
|
import { RestClientV5 } from './rest-client-v5';
|
||||||
|
import { WebsocketTopicSubscriptionConfirmationEvent } from './types/ws-events/topic-subscription-confirmation';
|
||||||
|
|
||||||
const loggerCategory = { category: 'bybit-ws' };
|
const loggerCategory = { category: 'bybit-ws' };
|
||||||
|
|
||||||
@@ -1069,20 +1070,19 @@ export class WebsocketClient extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private updatePendingTopicSubscriptionStatus(wsKey: string, msg: any) {
|
private updatePendingTopicSubscriptionStatus(
|
||||||
const requestsIds = msg['req_id'] as string;
|
wsKey: string,
|
||||||
|
msg: WebsocketTopicSubscriptionConfirmationEvent,
|
||||||
|
) {
|
||||||
|
const requestsIds = msg.req_id as string;
|
||||||
const pendingTopicsSubscriptions = this.pendingTopicsSubscriptions.find(
|
const pendingTopicsSubscriptions = this.pendingTopicsSubscriptions.find(
|
||||||
(s) => s.wsKey === wsKey,
|
(s) => s.wsKey === wsKey,
|
||||||
);
|
);
|
||||||
if (!pendingTopicsSubscriptions) {
|
|
||||||
throw new Error(
|
|
||||||
`Could not find "${wsKey}" within pending topics subscriptions.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const subscriptionSuccess = isTopicSubscriptionSuccess(msg);
|
if (!pendingTopicsSubscriptions) return;
|
||||||
|
|
||||||
const splitRequestsIds = requestsIds.split(',');
|
const splitRequestsIds = requestsIds.split(',');
|
||||||
if (!subscriptionSuccess) {
|
if (!isTopicSubscriptionSuccess(msg)) {
|
||||||
splitRequestsIds.forEach((req_id) =>
|
splitRequestsIds.forEach((req_id) =>
|
||||||
pendingTopicsSubscriptions.failedTopicsSubscriptions.add(req_id),
|
pendingTopicsSubscriptions.failedTopicsSubscriptions.add(req_id),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ describe('Public V5 Websocket client', () => {
|
|||||||
await api.subscribeV5(`publicTrade.${linearSymbol}X`, linearCategory);
|
await api.subscribeV5(`publicTrade.${linearSymbol}X`, linearCategory);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
expect(e).toBeDefined();
|
expect(e).toBeDefined();
|
||||||
expect(e).toMatch('(publicTrade.BTCUSDTX) failed to subscribe');
|
expect(e).toMatch('(publicTrade.BTCUSDT) failed to subscribe');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user