bitget-api
Version:
Complete Node.js & JavaScript SDK for Bitget V1-V3 REST APIs & WebSockets, with TypeScript & end-to-end tests.
118 lines • 3.68 kB
JavaScript
import { WS_KEY_MAP } from './util/websocket-util.js';
import { WebsocketClientV3 } from './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
*/
export class WebsocketAPIClient {
wsClient;
options;
constructor(options, logger) {
this.wsClient = new 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(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(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(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(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) => {
console.error(new Date(), 'ws exception: ', JSON.stringify(data));
});
}
}
}
//# sourceMappingURL=websocket-api-client.js.map