From 1226a01d48fe645ea6c86cf328560159cb247c61 Mon Sep 17 00:00:00 2001 From: Tiago Siebler Date: Mon, 25 Nov 2024 12:39:02 +0000 Subject: [PATCH] chore(v1.4.12): bump dependencies, add example for #83. --- examples/ws-private-handle-auth-fail.ts | 66 +++++++++++++++++++++++++ package-lock.json | 16 +++--- package.json | 2 +- 3 files changed, 75 insertions(+), 9 deletions(-) create mode 100644 examples/ws-private-handle-auth-fail.ts diff --git a/examples/ws-private-handle-auth-fail.ts b/examples/ws-private-handle-auth-fail.ts new file mode 100644 index 0000000..58ba981 --- /dev/null +++ b/examples/ws-private-handle-auth-fail.ts @@ -0,0 +1,66 @@ +// If you cloned the repo and are using typescript, you can import from src directly: +import { WebsocketClient } from '../src'; + +/** + * + * This simple example demonstrates one way to handle failed authentication, stopping the + * websocket client from running into a reconnect-loop when authentication fails (e.g. bad API keys). + * + * However, keep in mind it might be safer to make a test REST API call (e.g. fetch account balance) before + * trying to make a private WS connection to the account. + * + */ + +const wsClient = new WebsocketClient({ + accounts: [ + // For private topics, include one or more accounts in an array. Otherwise only public topics will work + { + apiKey: 'INCORRECT_API_KEY', + apiSecret: 'INCORRECT_API_SECRET', + apiPass: 'INCORRECT_API_PASSPHRASE', + }, + ], +}); + +// Raw data will arrive on the 'update' event +wsClient.on('update', (data) => { + // console.log('ws update (raw data received)', JSON.stringify(data, null, 2)); + console.log('ws update (raw data received)', JSON.stringify(data)); +}); + +wsClient.on('open', (data) => { + console.log('connection opened open:', data.wsKey); +}); + +// Replies (e.g. authenticating or subscribing to channels) will arrive on the 'response' event +wsClient.on('response', (data) => { + // console.log('ws response: ', JSON.stringify(data, null, 2)); + console.log('ws response: ', JSON.stringify(data)); +}); + +wsClient.on('reconnect', ({ wsKey }) => { + console.log('ws automatically reconnecting.... ', wsKey); +}); +wsClient.on('reconnected', (data) => { + console.log('ws has reconnected ', data?.wsKey); +}); + +wsClient.on('error', (data) => { + console.error('ws exception: ', data); + + const INVALID_API_KEY_ERROR = '60005'; + if (data.event === 'error' && data.code === INVALID_API_KEY_ERROR) { + console.error(`Detected auth failure - closing websocket`); + wsClient.close(data.wsKey); + } +}); + +// Optional, connect before subscribing: +wsClient.connectPrivate(); + +// This is optional though. The wsclient will automatically open and subscribe if the connection doesn't exist yet. + +// Subscribe one event at a time: +wsClient.subscribe({ + channel: 'account', +}); diff --git a/package-lock.json b/package-lock.json index 003ad68..7e80d11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "okx-api", - "version": "1.4.11", + "version": "1.4.12", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "okx-api", - "version": "1.4.11", + "version": "1.4.12", "license": "MIT", "dependencies": { "axios": "^1.6.7", @@ -2070,9 +2070,9 @@ } }, "node_modules/cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "devOptional": true, "dependencies": { "path-key": "^3.1.0", @@ -6986,9 +6986,9 @@ } }, "cross-spawn": { - "version": "7.0.3", - "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz", - "integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==", + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz", + "integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==", "devOptional": true, "requires": { "path-key": "^3.1.0", diff --git a/package.json b/package.json index 2bfd7d5..5307ae5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "okx-api", - "version": "1.4.11", + "version": "1.4.12", "description": "Complete & robust Node.js SDK for OKX's REST APIs and WebSockets, with TypeScript & end-to-end tests", "main": "lib/index.js", "types": "lib/index.d.ts",