UNPKG

bitget-api

Version:

Complete Node.js & JavaScript SDK for Bitget V1-V3 REST APIs & WebSockets, with TypeScript & end-to-end tests.

128 lines 4.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebsocketAPIClient = void 0; const websocket_util_js_1 = require("./util/websocket-util.js"); const websocket_client_v3_js_1 = require("./websocket-client-v3.js"); /** * This is a minimal Websocket API wrapper around the WebsocketClient. * * Note: You can also directly use the sendWSAPIRequest() method to make WS API calls, but some * may find the below methods slightly more intuitive. * * Refer to the WS API promises example for a more detailed example on using sendWSAPIRequest() directly: * https://github.com/tiagosiebler/bitget-api/blob/master/examples/V3/ws-api-trade-raw.ts */ class WebsocketAPIClient { wsClient; options; constructor(options, logger) { this.wsClient = new websocket_client_v3_js_1.WebsocketClientV3(options, logger); this.options = { attachEventListeners: true, ...options, }; this.setupDefaultEventListeners(); } getWSClient() { return this.wsClient; } setTimeOffsetMs(newOffset) { return this.getWSClient().setTimeOffsetMs(newOffset); } /* * Bitget WebSocket API Methods * https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel */ /** * Submit a new order * * https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel * * @returns */ submitNewOrder(category, params) { return this.wsClient.sendWSAPIRequest(websocket_util_js_1.WS_KEY_MAP.v3Private, 'place-order', category, params); } /** * Submit a new order * * https://www.bitget.com/api-doc/uta/websocket/private/Batch-Place-Order-Channel * * @returns */ placeBatchOrders(category, params) { return this.wsClient.sendWSAPIRequest(websocket_util_js_1.WS_KEY_MAP.v3Private, 'batch-place', category, params); } /* * Bitget WebSocket API Methods * https://www.bitget.com/api-doc/uta/websocket/private/Place-Order-Channel */ /** * Cancel Order * * https://www.bitget.com/api-doc/uta/websocket/private/Cancel-Order-Channel * * @returns */ cancelOrder(category, params) { return this.wsClient.sendWSAPIRequest(websocket_util_js_1.WS_KEY_MAP.v3Private, 'cancel-order', category, params); } /** * Batch Cancel Order * * https://www.bitget.com/api-doc/uta/websocket/private/Batch-Cancel-Order-Channel * * @returns */ cancelBatchOrders(category, params) { return this.wsClient.sendWSAPIRequest(websocket_util_js_1.WS_KEY_MAP.v3Private, 'batch-cancel', category, params); } /** * * * * * * * * Private methods for handling some of the convenience/automation provided by the WS API Client * * * * * * * */ setupDefaultEventListeners() { if (this.options.attachEventListeners) { /** * General event handlers for monitoring the WebsocketClient */ this.wsClient .on('open', (data) => { console.log(new Date(), 'ws connected', data.wsKey); }) .on('reconnect', ({ wsKey }) => { console.log(new Date(), 'ws automatically reconnecting.... ', wsKey); }) .on('reconnected', (data) => { console.log(new Date(), 'ws has reconnected ', data?.wsKey); }) .on('authenticated', (data) => { console.info(new Date(), 'ws has authenticated ', data?.wsKey); }) .on('exception', (data) => { try { // Blind JSON.stringify can fail on circular references console.error(new Date(), 'ws exception: ', JSON.stringify(data)); } catch { console.error(new Date(), 'ws exception: ', data); } }); } } } exports.WebsocketAPIClient = WebsocketAPIClient; //# sourceMappingURL=websocket-api-client.js.map