bitget-api
Version:
Complete Node.js & JavaScript SDK for Bitget V1-V3 REST APIs & WebSockets, with TypeScript & end-to-end tests.
62 lines • 2.14 kB
JavaScript
/** TypeGuard: event has a string "action" property */
function isWsEvent(event) {
return (typeof event === 'object' &&
event &&
(typeof event)['action'] === 'string' &&
(typeof event)['data']);
}
/** TypeGuard: event has "action === snapshot" */
function isWsSnapshotEvent(event) {
return isWsEvent(event) && event.action === 'snapshot';
}
/** TypeGuard: event has a string channel name */
function isWsChannelEvent(event) {
if (typeof event['arg'] === 'object' &&
event['arg'] &&
typeof (typeof event['arg'])['channel'] === 'string') {
return true;
}
return false;
}
/** TypeGuard: event is an account update (balance) */
export function isWsAccountSnapshotEvent(event) {
return (isWsSnapshotEvent(event) &&
isWsChannelEvent(event) &&
event.arg.channel === 'account' &&
Array.isArray(event.data));
}
/** TypeGuard: event is a positions update */
export function isWsPositionsSnapshotEvent(event) {
return (isWsSnapshotEvent(event) &&
isWsChannelEvent(event) &&
event.arg.channel === 'positions' &&
Array.isArray(event.data));
}
/** TypeGuard: event is a UMCBL account update (balance) */
export function isWsFuturesAccountSnapshotEvent(event) {
return isWsAccountSnapshotEvent(event) && event.arg.instType === 'umcbl';
}
/** TypeGuard: event is a UMCBL positions update */
export function isWsFuturesPositionsSnapshotEvent(event) {
return isWsPositionsSnapshotEvent(event) && event.arg.instType === 'umcbl';
}
/**
* Simple guard for non-TypeScript users, throw a runtime error if value doesn't match type
*/
export function assertMarginType(marginType) {
if (marginType !== 'isolated' && marginType !== 'crossed') {
throw new Error('MarginType should be one of: crossed | isolated');
}
return true;
}
export function isWSAPIResponse(msg) {
if (typeof msg !== 'object' || !msg) {
return false;
}
if (typeof msg['event'] !== 'string' ||
typeof msg['id'] !== 'string') {
return false;
}
return true;
}
//# sourceMappingURL=type-guards.js.map