whio-api-sdk 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -50,7 +50,7 @@ export class WebSocketModule extends BaseClient {
50
50
  const token = this.getAccessToken();
51
51
  if (!token) {
52
52
  this.emit('connection-error', new Error('No access token available for WebSocket connection'));
53
- return;
53
+ throw new Error('No access token available for WebSocket connection');
54
54
  }
55
55
  this.socket = io(wsUrl, {
56
56
  auth: {
@@ -60,9 +60,30 @@ export class WebSocketModule extends BaseClient {
60
60
  timeout: 10000
61
61
  });
62
62
  this.setupEventHandlers();
63
+ // Wait for the socket to actually connect
64
+ yield new Promise((resolve, reject) => {
65
+ const timeout = setTimeout(() => {
66
+ reject(new Error('WebSocket connection timeout after 10 seconds'));
67
+ }, 10000);
68
+ const onConnect = () => {
69
+ var _a;
70
+ clearTimeout(timeout);
71
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.off('connect_error', onError);
72
+ resolve();
73
+ };
74
+ const onError = (error) => {
75
+ var _a;
76
+ clearTimeout(timeout);
77
+ (_a = this.socket) === null || _a === void 0 ? void 0 : _a.off('connect', onConnect);
78
+ reject(error);
79
+ };
80
+ this.socket.once('connect', onConnect);
81
+ this.socket.once('connect_error', onError);
82
+ });
63
83
  }
64
84
  catch (error) {
65
85
  this.emit('connection-error', error);
86
+ throw error;
66
87
  }
67
88
  });
68
89
  }
@@ -99,6 +120,7 @@ export class WebSocketModule extends BaseClient {
99
120
  * Reconnects with fresh token if current token is expired
100
121
  */
101
122
  ensureValidConnection() {
123
+ var _a;
102
124
  return __awaiter(this, void 0, void 0, function* () {
103
125
  const currentToken = this.getAccessToken();
104
126
  // Check if we have a token and if it's expired
@@ -117,6 +139,11 @@ export class WebSocketModule extends BaseClient {
117
139
  throw new Error('Authentication failed: Unable to refresh token for WebSocket streaming');
118
140
  }
119
141
  }
142
+ else if (!((_a = this.socket) === null || _a === void 0 ? void 0 : _a.connected)) {
143
+ // Token is valid but not connected - establish connection
144
+ console.log('[WebSocket] Not connected, connecting...');
145
+ yield this.connect();
146
+ }
120
147
  });
121
148
  }
122
149
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "whio-api-sdk",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -60,7 +60,7 @@ export class WebSocketModule extends BaseClient {
60
60
  }
61
61
 
62
62
  const wsUrl = this.getWebSocketUrl();
63
-
63
+
64
64
  try {
65
65
  // Ensure we have a fresh token before connecting
66
66
  await this.getToken();
@@ -68,7 +68,7 @@ export class WebSocketModule extends BaseClient {
68
68
 
69
69
  if (!token) {
70
70
  this.emit('connection-error', new Error('No access token available for WebSocket connection'));
71
- return;
71
+ throw new Error('No access token available for WebSocket connection');
72
72
  }
73
73
 
74
74
  this.socket = io(wsUrl, {
@@ -80,8 +80,31 @@ export class WebSocketModule extends BaseClient {
80
80
  });
81
81
 
82
82
  this.setupEventHandlers();
83
+
84
+ // Wait for the socket to actually connect
85
+ await new Promise<void>((resolve, reject) => {
86
+ const timeout = setTimeout(() => {
87
+ reject(new Error('WebSocket connection timeout after 10 seconds'));
88
+ }, 10000);
89
+
90
+ const onConnect = () => {
91
+ clearTimeout(timeout);
92
+ this.socket?.off('connect_error', onError);
93
+ resolve();
94
+ };
95
+
96
+ const onError = (error: Error) => {
97
+ clearTimeout(timeout);
98
+ this.socket?.off('connect', onConnect);
99
+ reject(error);
100
+ };
101
+
102
+ this.socket!.once('connect', onConnect);
103
+ this.socket!.once('connect_error', onError);
104
+ });
83
105
  } catch (error) {
84
106
  this.emit('connection-error', error as Error);
107
+ throw error;
85
108
  }
86
109
  }
87
110
 
@@ -123,24 +146,28 @@ export class WebSocketModule extends BaseClient {
123
146
  */
124
147
  private async ensureValidConnection(): Promise<void> {
125
148
  const currentToken = this.getAccessToken();
126
-
149
+
127
150
  // Check if we have a token and if it's expired
128
151
  if (!currentToken || this.isTokenExpired(currentToken)) {
129
152
  console.log('[WebSocket] Token expired or missing, refreshing and reconnecting...');
130
-
153
+
131
154
  try {
132
155
  // Refresh token
133
156
  await this.getToken();
134
-
157
+
135
158
  // Disconnect current connection and reconnect with fresh token
136
159
  this.disconnect();
137
160
  await this.connect();
138
-
161
+
139
162
  console.log('[WebSocket] Successfully reconnected with fresh token');
140
163
  } catch (error) {
141
164
  console.error('[WebSocket] Failed to refresh token and reconnect:', error);
142
165
  throw new Error('Authentication failed: Unable to refresh token for WebSocket streaming');
143
166
  }
167
+ } else if (!this.socket?.connected) {
168
+ // Token is valid but not connected - establish connection
169
+ console.log('[WebSocket] Not connected, connecting...');
170
+ await this.connect();
144
171
  }
145
172
  }
146
173