Merge pull request #309 from tiagosiebler/obguard
v3.8.1: feat() add type guard and type for ws orderbook events
This commit is contained in:
28
package-lock.json
generated
28
package-lock.json
generated
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "3.8.0",
|
"version": "3.8.1",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "3.8.0",
|
"version": "3.8.1",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^0.21.0",
|
"axios": "^0.21.0",
|
||||||
@@ -2027,9 +2027,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/axios": {
|
"node_modules/axios": {
|
||||||
"version": "0.21.2",
|
"version": "0.21.4",
|
||||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz",
|
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
|
||||||
"integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==",
|
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"follow-redirects": "^1.14.0"
|
"follow-redirects": "^1.14.0"
|
||||||
}
|
}
|
||||||
@@ -3350,9 +3350,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/follow-redirects": {
|
"node_modules/follow-redirects": {
|
||||||
"version": "1.14.9",
|
"version": "1.15.5",
|
||||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz",
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
||||||
"integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==",
|
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==",
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"type": "individual",
|
"type": "individual",
|
||||||
@@ -8885,9 +8885,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"axios": {
|
"axios": {
|
||||||
"version": "0.21.2",
|
"version": "0.21.4",
|
||||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.2.tgz",
|
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
|
||||||
"integrity": "sha512-87otirqUw3e8CzHTMO+/9kh/FSgXt/eVDvipijwDtEuwbkySWZ9SBm6VEubmJ/kLKEoLQV/POhxXFb66bfekfg==",
|
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"follow-redirects": "^1.14.0"
|
"follow-redirects": "^1.14.0"
|
||||||
}
|
}
|
||||||
@@ -9926,9 +9926,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"follow-redirects": {
|
"follow-redirects": {
|
||||||
"version": "1.14.9",
|
"version": "1.15.5",
|
||||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz",
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz",
|
||||||
"integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w=="
|
"integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw=="
|
||||||
},
|
},
|
||||||
"form-data": {
|
"form-data": {
|
||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "bybit-api",
|
"name": "bybit-api",
|
||||||
"version": "3.8.0",
|
"version": "3.8.1",
|
||||||
"description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
|
"description": "Complete & robust Node.js SDK for Bybit's REST APIs and WebSockets, with TypeScript & strong end to end tests.",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"types": "lib/index.d.ts",
|
"types": "lib/index.d.ts",
|
||||||
|
|||||||
@@ -13,6 +13,35 @@ import {
|
|||||||
} from './v5-shared';
|
} from './v5-shared';
|
||||||
import { WsKey } from './websockets';
|
import { WsKey } from './websockets';
|
||||||
|
|
||||||
|
export interface WSOrderbookEventV5 {
|
||||||
|
topic: string;
|
||||||
|
/** Event timestamp */
|
||||||
|
ts: number;
|
||||||
|
type: 'delta' | 'snapshot';
|
||||||
|
data: {
|
||||||
|
/** Symbol */
|
||||||
|
s: string;
|
||||||
|
/** [price, qty][] */
|
||||||
|
b: [string, string][];
|
||||||
|
/** [price, qty][] */
|
||||||
|
a: [string, string][];
|
||||||
|
/** Update ID */
|
||||||
|
u: number;
|
||||||
|
/**
|
||||||
|
* Cross sequence
|
||||||
|
*/
|
||||||
|
seq: number;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* matching engine timestamp (correlated with T from public trade channel)
|
||||||
|
*/
|
||||||
|
cts: number;
|
||||||
|
/**
|
||||||
|
* Internal reference, can be used to determine if this is spot/linear/inverse/etc
|
||||||
|
*/
|
||||||
|
wsKey: WsKey;
|
||||||
|
}
|
||||||
|
|
||||||
export interface WSAccountOrderV5 {
|
export interface WSAccountOrderV5 {
|
||||||
qty: string;
|
qty: string;
|
||||||
price: string;
|
price: string;
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export * from './BaseRestClient';
|
export * from './BaseRestClient';
|
||||||
export * from './requestUtils';
|
|
||||||
export * from './WsStore';
|
|
||||||
export * from './logger';
|
export * from './logger';
|
||||||
|
export * from './requestUtils';
|
||||||
|
export * from './typeGuards';
|
||||||
export * from './websocket-util';
|
export * from './websocket-util';
|
||||||
|
export * from './WsStore';
|
||||||
|
|||||||
29
src/util/typeGuards.ts
Normal file
29
src/util/typeGuards.ts
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
/**
|
||||||
|
* Use type guards to narrow down types with minimal efforts.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { WSOrderbookEventV5 } from '../types/websocket.events';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Type guard to detect a V5 orderbook event (delta & snapshots)
|
||||||
|
*
|
||||||
|
* @param event
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
export function isWsOrderbookEventV5(
|
||||||
|
event: unknown,
|
||||||
|
): event is WSOrderbookEventV5 {
|
||||||
|
if (
|
||||||
|
typeof event !== 'object' ||
|
||||||
|
!event ||
|
||||||
|
typeof event['topic'] !== 'string' ||
|
||||||
|
typeof event['type'] !== 'string'
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
['delta', 'snapshot'].includes(event['type']) &&
|
||||||
|
event['topic'].startsWith('orderbook')
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user