UNPKG

okx-api

Version:

Complete Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests

82 lines (81 loc) 5.17 kB
import { WsAPIOperationResponseMap, WSAPIRequestFlags, WsAPITopicRequestParamMap, WsAPIWsKeyTopicMap, WSOperation, WsRequestOperationOKX } from './types/websockets/ws-api.js'; import { MessageEventLike, WsDataEvent } from './types/websockets/ws-events.js'; import { WSClientConfigurableOptions } from './types/websockets/ws-general.js'; import { WsAuthRequestArg, WsChannelSubUnSubRequestArg } from './types/websockets/ws-request.js'; import { BaseWebsocketClient, EmittableEvent, MidflightWsRequestEvent, WSClientEventMap } from './util/BaseWSClient.js'; import { DefaultLogger } from './util/logger.js'; import { WsKey, WsTopicRequest } from './util/websocket-util.js'; import { WSConnectedResult } from './util/WsStore.types.js'; export declare interface WebsocketClient { on<U extends keyof WSClientEventMap<WsKey, WsDataEvent>>(event: U, listener: WSClientEventMap<WsKey>[U]): this; emit<U extends keyof WSClientEventMap<WsKey, WsDataEvent>>(event: U, ...args: Parameters<WSClientEventMap<WsKey>[U]>): boolean; } export declare class WebsocketClient extends BaseWebsocketClient<WsKey, WsRequestOperationOKX<object>> { 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>[]; connectPublic(businessEndpoint?: boolean): Promise<WSConnectedResult | undefined>; connectPrivate(businessEndpoint?: boolean): Promise<WSConnectedResult | undefined>; /** * Ensures the WS API connection is active and ready. * * You do not need to call this, but if you call this before making any WS API requests, * it can accelerate the first request (by preparing the connection in advance). */ connectWSAPI(): Promise<unknown[]>; /** * Subscribe to topics & track/persist them. They will be automatically resubscribed to if the connection drops/reconnects. * @param wsEvents 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) */ subscribe(wsEvents: WsChannelSubUnSubRequestArg[] | WsChannelSubUnSubRequestArg, isPrivateTopic?: 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(wsEvents: WsChannelSubUnSubRequestArg[] | WsChannelSubUnSubRequestArg, isPrivateTopic?: boolean): Promise<unknown>[]; /** * * * Internal methods required to integrate with the BaseWSClient * * */ getMarketWsKey(type: 'private' | 'business'): WsKey; protected sendPingEvent(wsKey: WsKey): void; protected sendPongEvent(wsKey: WsKey): void; protected isWsPing(data: any): boolean; protected isWsPong(data: any): boolean; protected isPrivateTopicRequest(_request: WsTopicRequest<string>, wsKey: WsKey): boolean; protected getPrivateWSKeys(): WsKey[]; protected isAuthOnConnectWsKey(wsKey: WsKey): boolean; protected getWsUrl(wsKey: WsKey): Promise<string>; protected getMaxTopicsPerSubscribeEvent(): number | null; /** * @returns one or more correctly structured request events for performing a operations over WS. This can vary per exchange spec. */ protected getWsRequestEvents(operation: WSOperation, requests: WsTopicRequest<string, object>[]): Promise<MidflightWsRequestEvent<WsRequestOperationOKX<object>>[]>; private signMessage; protected getWsAuthRequestEvent(wsKey: WsKey, skipIsPublicWsKeyCheck: boolean): Promise<WsRequestOperationOKX<WsAuthRequestArg> | null>; private getWsAuthSignature; /** * Abstraction called to sort ws events into emittable event types (response to a request, data update, etc) */ protected resolveEmittableEvents(wsKey: WsKey, event: MessageEventLike): EmittableEvent[]; /** * OKX supports order placement via WebSockets. This is the WS API: * https://www.okx.com/docs-v5/en/#order-book-trading-trade-ws-place-order * * For convenient promise-wrapped usage of the WS API, instance the WebsocketAPIClient class exported by this SDK. * * For demo trading, set demoTrading:true in the WS Client config. * * @returns a promise that resolves/rejects when a matching response arrives */ sendWSAPIRequest<TWSKey extends keyof WsAPIWsKeyTopicMap, TWSOperation extends WsAPIWsKeyTopicMap[TWSKey], TWSParams extends WsAPITopicRequestParamMap[TWSOperation], TWSAPIResponse extends WsAPIOperationResponseMap[TWSOperation] = WsAPIOperationResponseMap[TWSOperation]>(rawWsKey: WsKey, operation: TWSOperation, params: TWSParams & { signRequest?: boolean; }, requestFlags?: WSAPIRequestFlags): Promise<TWSAPIResponse>; }