okx-api
Version:
Complete & robust Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests
63 lines • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRawAPIResponse = isRawAPIResponse;
exports.isWsEvent = isWsEvent;
exports.isWsDataEvent = isWsDataEvent;
exports.isWsErrorEvent = isWsErrorEvent;
exports.isWsLoginEvent = isWsLoginEvent;
exports.isWsSubscribeEvent = isWsSubscribeEvent;
exports.isWsUnsubscribeEvent = isWsUnsubscribeEvent;
exports.isConnCountEvent = isConnCountEvent;
exports.neverGuard = neverGuard;
function isRawAPIResponse(response) {
if (typeof response !== 'object' || !response) {
return false;
}
if ('code' in response && 'msg' in response && 'data' in response) {
return true;
}
return false;
}
/** Simple type guard that a websocket event extends a known event schema */
function isWsEvent(evtData) {
if (typeof evtData !== 'object' || !evtData) {
return false;
}
if ('event' in evtData) {
return true;
}
return false;
}
function isWsDataEvent(evtData) {
if (typeof evtData !== 'object' || !evtData) {
return false;
}
if ('arg' in evtData && 'data' in evtData) {
return true;
}
return false;
}
function isWsErrorEvent(evt) {
return isWsEvent(evt) && evt.event === 'error';
}
/** Usually a response to authenticating over ws */
function isWsLoginEvent(evt) {
return isWsEvent(evt) && evt.event === 'login';
}
/** A response to subscribing to a channel */
function isWsSubscribeEvent(evtData) {
return isWsEvent(evtData) && evtData.event === 'subscribe';
}
/** A response to unsubscribing from a channel */
function isWsUnsubscribeEvent(evtData) {
return isWsEvent(evtData) && evtData.event === 'unsubscribe';
}
/** Information event */
function isConnCountEvent(evtData) {
return isWsEvent(evtData) && evtData.event === 'channel-conn-count';
}
/** Simple typescript guard never expecting code to reach it (will throw typescript error if called) */
function neverGuard(x, msg) {
return new Error(`Unhandled value exception "${x}", ${msg}`);
}
//# sourceMappingURL=typeGuards.js.map