UNPKG

bitmart-api

Version:

Complete & robust Node.js SDK for BitMart's REST APIs and WebSockets, with TypeScript declarations.

109 lines (108 loc) 5.52 kB
import { BaseWebsocketClient, EmittableEvent } from './lib/BaseWSClient.js'; import { DefaultLogger } from './lib/logger.js'; import { MessageEventLike } from './lib/requestUtils.js'; import { WsKey, WsTopicRequest } from './lib/websocket/websocket-util.js'; import { WSConnectedResult } from './lib/websocket/WsStore.types.js'; import { MidflightWsRequestEvent, WSClientConfigurableOptions, WsMarket, WsTopic } from './types/websockets/client.js'; import { WsOperation, WsRequestOperation } from './types/websockets/requests.js'; /** Any WS keys in this list will ALWAYS skip the authentication process, even if credentials are available */ export declare const PUBLIC_WS_KEYS: WsKey[]; export interface WSAPIRequestFlags { /** If true, will skip auth requirement for WS API connection */ authIsOptional?: boolean | undefined; } export declare class WebsocketClient extends BaseWebsocketClient<WsMarket, WsKey, WsRequestOperation<WsTopic>> { constructor(options?: WSClientConfigurableOptions, logger?: DefaultLogger); /** * * Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library */ connectAll(): Promise<WSConnectedResult | undefined>[]; /** * Request subscription to one or more topics. * * - Subscriptions are automatically routed to the correct websocket connection. * - Authentication/connection is automatic. * - Resubscribe after network issues is automatic. * * Call `unsubscribeTopics(topics)` to remove topics */ subscribeTopics(topics: WsTopic[]): void; /** * Unsubscribe from one or more topics. * * - Requests are automatically routed to the correct websocket connection. * - These topics will be removed from the topic cache, so they won't be subscribed to again. */ unsubscribeTopics(topics: WsTopic[]): void; /** * Subscribe to topics & track/persist them. They will be automatically resubscribed to if the connection drops/reconnects. * @param wsTopics topic or list of topics * @param isPrivate optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) */ subscribe(wsTopics: WsTopic[] | WsTopic, market: WsMarket, isPrivate?: boolean): Promise<unknown>[]; /** * Unsubscribe from topics & remove them from memory. They won't be re-subscribed to if the connection reconnects. * @param wsTopics topic or list of topics * @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet) */ unsubscribe(wsTopics: WsTopic[] | WsTopic, market: WsMarket, isPrivate?: boolean): Promise<unknown>[]; /** * * * Internal methods - not intended for public use * * */ /** * Note: implementing this method will wipe the WsStore state for this WsKey, once this method returns */ protected isCustomReconnectionNeeded(): boolean; protected triggerCustomReconnectionWorkflow(): Promise<void>; /** * @returns The WS URL to connect to for this WS key */ protected getWsUrl(wsKey: WsKey): Promise<string>; private signMessage; protected getWsAuthRequestEvent(wsKey: WsKey): Promise<WsRequestOperation<string>>; private getWsAuthSignature; protected sendPingEvent(wsKey: WsKey): void; protected sendPongEvent(wsKey: WsKey): void; /** Force subscription requests to be sent in smaller batches, if a number is returned */ protected getMaxTopicsPerSubscribeEvent(wsKey: WsKey): number | null; protected authPrivateConnectionsOnConnect(_wsKey: WsKey): boolean; /** * @returns one or more correctly structured request events for performing a operations over WS. This can vary per exchange spec. */ protected getWsRequestEvents(market: WsMarket, operation: WsOperation, requests: WsTopicRequest<WsTopic>[], _wsKey: WsKey): Promise<MidflightWsRequestEvent<WsRequestOperation<WsTopic>>[]>; /** * Determines if a topic is for a private channel, using a hardcoded list of strings */ protected isPrivateTopicRequest(request: WsTopicRequest<WsTopic>): boolean; private isPrivateTopic; protected isWsPing(msg: any): boolean; protected isWsPong(msg: any): boolean; protected resolveEmittableEvents(wsKey: WsKey, event: MessageEventLike): EmittableEvent[]; protected getWsKeyForMarket(market: WsMarket, isPrivate: boolean): WsKey; protected getWsMarketForWsKey(key: WsKey): WsMarket; protected getWsKeyForTopic(topic: WsTopic): WsKey; protected getPrivateWSKeys(): WsKey[]; protected isAuthOnConnectWsKey(wsKey: WsKey): boolean; /** * Map one or more topics into fully prepared "unsubscribe request" events (already stringified and ready to send) */ protected getWsUnsubscribeEventsForTopics(topics: WsTopic[], wsKey: WsKey): string[]; /** * @returns a correctly structured events for performing an operation over WS. This can vary per exchange spec. */ private getWsRequestEvent; /** * This exchange API is split into "markets" that behave differently (different base URLs). * The market can easily be resolved using the topic name. */ private getMarketForTopic; /** * Used to split sub/unsub logic by websocket connection */ private arrangeTopicsIntoWsKeyGroups; }