v3.8.1: feat() add type guard and type for ws orderbook events
This commit is contained in:
@@ -13,6 +13,35 @@ import {
|
||||
} from './v5-shared';
|
||||
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 {
|
||||
qty: string;
|
||||
price: string;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './BaseRestClient';
|
||||
export * from './requestUtils';
|
||||
export * from './WsStore';
|
||||
export * from './logger';
|
||||
export * from './requestUtils';
|
||||
export * from './typeGuards';
|
||||
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