From 12b5b0a41812add4087fcdcdc3228dfcc9dd8927 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 25 Apr 2024 17:28:28 +0200 Subject: [PATCH] Add type guards for ws position events --- src/util/typeGuards.ts | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/util/typeGuards.ts b/src/util/typeGuards.ts index c8b42a0..44308f8 100644 --- a/src/util/typeGuards.ts +++ b/src/util/typeGuards.ts @@ -2,7 +2,7 @@ * Use type guards to narrow down types with minimal efforts. */ -import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5 } from '../types/websocket.events'; +import { WSAccountOrderEventV5, WSExecutionEventV5, WSOrderbookEventV5, WSPositionEventV5 } from '../types/websocket.events'; /** * Type guard to detect a V5 orderbook event (delta & snapshots) @@ -28,6 +28,26 @@ export function isWsOrderbookEventV5( ); } +/** + * Type guard to detect a V5 position event. + * + * @param event + * @returns + */ +export function isWsPositionEventV5( + event: unknown, +): event is WSPositionEventV5 { + if ( + typeof event !== 'object' || + !event || + typeof event['topic'] !== 'string' + ) { + return false; + } + + return event['topic'] === 'position'; +} + /** * Type guard to detect a V5 order event. *