@pipsend/sdk 0.2.0 → 0.3.1

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.
package/dist/index.mjs CHANGED
@@ -109,11 +109,6 @@ var AuthManager = class {
109
109
  }
110
110
  const data = await response.json();
111
111
  this.setTokenInfo(data);
112
- console.log("\u2705 Authentication successful");
113
- console.log(` User: ${data.user.first_name} ${data.user.last_name} (${data.user.email})`);
114
- console.log(` Login: ${data.user.login}`);
115
- console.log(` Balance: $${data.user.balance.toFixed(2)}`);
116
- console.log(` Logged with: ${data.logged_with}`);
117
112
  } catch (error) {
118
113
  if (error instanceof AuthenticationError) {
119
114
  throw error;
@@ -128,7 +123,6 @@ var AuthManager = class {
128
123
  */
129
124
  async refreshToken() {
130
125
  if (!this.tokenInfo?.refreshToken) {
131
- console.log("\u26A0\uFE0F No refresh token available, performing full authentication");
132
126
  this.tokenInfo = void 0;
133
127
  await this.authenticate();
134
128
  return;
@@ -142,7 +136,6 @@ var AuthManager = class {
142
136
  })
143
137
  });
144
138
  if (!response.ok) {
145
- console.log("\u26A0\uFE0F Refresh token failed, performing full authentication");
146
139
  this.tokenInfo = void 0;
147
140
  await this.authenticate();
148
141
  return;
@@ -157,9 +150,7 @@ var AuthManager = class {
157
150
  issuedAt: now,
158
151
  expiresAt
159
152
  };
160
- console.log("\u2705 Token refreshed successfully");
161
153
  } catch (error) {
162
- console.error("\u274C Error refreshing token:", error);
163
154
  this.tokenInfo = void 0;
164
155
  await this.authenticate();
165
156
  }
@@ -211,11 +202,9 @@ var WebSocketManager = class {
211
202
  */
212
203
  async connect() {
213
204
  if (this.isConnected) {
214
- console.log("\u26A0\uFE0F WebSocket already connected");
215
205
  return;
216
206
  }
217
207
  if (this.isReconnecting) {
218
- console.log("\u26A0\uFE0F WebSocket reconnection in progress");
219
208
  return;
220
209
  }
221
210
  const token = await this.authManager.ensureAuthenticated();
@@ -226,13 +215,11 @@ var WebSocketManager = class {
226
215
  const WS = this.getWebSocketConstructor();
227
216
  this.ws = new WS(wsUrl);
228
217
  this.ws.onopen = () => {
229
- console.log("\u2705 WebSocket connected");
230
218
  this.isConnected = true;
231
219
  this.isReconnecting = false;
232
220
  this.reconnectAttempts = 0;
233
221
  this.emit("connected", { timestamp: Date.now() });
234
222
  if (this.subscribedChannels.length > 0) {
235
- console.log("\u{1F504} Resubscribing to channels:", this.subscribedChannels);
236
223
  this.subscribe(this.subscribedChannels);
237
224
  }
238
225
  this.startHeartbeat();
@@ -254,7 +241,6 @@ var WebSocketManager = class {
254
241
  }
255
242
  };
256
243
  this.ws.onclose = (event) => {
257
- console.log("WebSocket disconnected", event.code, event.reason);
258
244
  this.isConnected = false;
259
245
  this.stopHeartbeat();
260
246
  this.emit("disconnected", {
@@ -284,14 +270,12 @@ var WebSocketManager = class {
284
270
  channels
285
271
  };
286
272
  this.sendAction(message);
287
- console.log("\u{1F4E1} Subscribed to channels:", channels);
288
273
  }
289
274
  /**
290
275
  * Unsubscribes from WebSocket channels
291
276
  */
292
277
  unsubscribe(channels) {
293
278
  if (!this.isConnected) {
294
- console.warn("WebSocket not connected, cannot unsubscribe");
295
279
  return;
296
280
  }
297
281
  this.subscribedChannels = this.subscribedChannels.filter(
@@ -302,7 +286,6 @@ var WebSocketManager = class {
302
286
  channels
303
287
  };
304
288
  this.sendAction(message);
305
- console.log("\u{1F4E1} Unsubscribed from channels:", channels);
306
289
  }
307
290
  /**
308
291
  * Registers an event listener
@@ -327,7 +310,6 @@ var WebSocketManager = class {
327
310
  * Disconnects from WebSocket
328
311
  */
329
312
  disconnect() {
330
- console.log("\u{1F44B} Disconnecting WebSocket...");
331
313
  this.stopHeartbeat();
332
314
  if (this.ws) {
333
315
  this.ws.close();
@@ -385,7 +367,6 @@ var WebSocketManager = class {
385
367
  */
386
368
  sendAction(message) {
387
369
  if (!this.ws || !this.isConnected) {
388
- console.warn("\u26A0\uFE0F WebSocket not ready, message not sent:", message.action);
389
370
  return;
390
371
  }
391
372
  try {
@@ -429,7 +410,7 @@ var WebSocketManager = class {
429
410
  this.handleError(message);
430
411
  break;
431
412
  default:
432
- console.warn(message.op);
413
+ break;
433
414
  }
434
415
  this.emit("*", message);
435
416
  }
@@ -437,22 +418,18 @@ var WebSocketManager = class {
437
418
  * Handles connected message from server
438
419
  */
439
420
  handleConnected(message) {
440
- console.log("\u2705 WebSocket connected:", message.data.message);
441
- console.log(" Available channels:", message.data.available_channels);
442
421
  this.emit("connected", message.data);
443
422
  }
444
423
  /**
445
424
  * Handles subscription success
446
425
  */
447
426
  handleSubscriptionSuccess(message) {
448
- console.log("\u2705 Subscription successful:", message.data.subscribed);
449
427
  this.emit("subscription_success", message.data);
450
428
  }
451
429
  /**
452
430
  * Handles unsubscription success
453
431
  */
454
432
  handleUnsubscriptionSuccess(message) {
455
- console.log("\u2705 Unsubscription successful:", message.data.unsubscribed);
456
433
  this.emit("unsubscription_success", message.data);
457
434
  }
458
435
  /**
@@ -472,11 +449,27 @@ var WebSocketManager = class {
472
449
  case "position.updated":
473
450
  this.emit("position:updated", eventData);
474
451
  break;
452
+ case "account_metrics":
453
+ const balanceEvent = eventData;
454
+ if (message.topic?.startsWith("balance:")) {
455
+ const loginMatch = message.topic.match(/balance:api:(\d+)/);
456
+ if (loginMatch) {
457
+ balanceEvent.login = parseInt(loginMatch[1], 10);
458
+ }
459
+ }
460
+ this.emit("account:metrics", balanceEvent);
461
+ this.emit("balance:updated", balanceEvent);
462
+ break;
475
463
  default:
476
464
  if (message.topic?.startsWith("balance:")) {
477
- this.emit("balance:updated", eventData);
478
- } else {
479
- console.warn("Unknown event type:", eventType);
465
+ const event = eventData;
466
+ const loginMatch = message.topic.match(/balance:api:(\d+)/);
467
+ if (loginMatch) {
468
+ event.login = parseInt(loginMatch[1], 10);
469
+ }
470
+ this.emit("balance:updated", event);
471
+ } else if (message.topic?.startsWith("account_metrics")) {
472
+ this.emit("account:metrics", eventData);
480
473
  }
481
474
  }
482
475
  }
@@ -512,18 +505,13 @@ var WebSocketManager = class {
512
505
  const autoReconnect = this.config.websocket?.autoReconnect ?? true;
513
506
  const maxAttempts = this.config.websocket?.maxReconnectAttempts ?? 5;
514
507
  if (!autoReconnect) {
515
- console.log("Auto-reconnect disabled");
516
508
  return;
517
509
  }
518
510
  if (this.reconnectAttempts >= maxAttempts) {
519
- console.log(`\u274C Max reconnection attempts (${maxAttempts}) reached. Giving up.`);
520
511
  this.emit("error", new WebSocketError("Max reconnection attempts reached"));
521
512
  return;
522
513
  }
523
514
  const delay = Math.min(1e3 * Math.pow(2, this.reconnectAttempts), 3e4);
524
- console.log(
525
- `\u{1F504} Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts + 1}/${maxAttempts})...`
526
- );
527
515
  this.isReconnecting = true;
528
516
  setTimeout(() => {
529
517
  this.reconnectAttempts++;
@@ -682,7 +670,10 @@ var AccountsAPI = class {
682
670
  * List all accounts with optional filters and pagination
683
671
  */
684
672
  async list(params) {
685
- return this.http.get("/api/v1/accounts", params);
673
+ return this.http.get(
674
+ "/api/v1/accounts",
675
+ params
676
+ );
686
677
  }
687
678
  /**
688
679
  * Get all account logins
@@ -694,25 +685,24 @@ var AccountsAPI = class {
694
685
  * Get account status/metrics (balance, equity, credit, margin)
695
686
  */
696
687
  async getStatus(login) {
697
- return this.http.get(`/api/v1/accounts/${login}/status`);
688
+ return this.http.get(
689
+ `/api/v1/accounts/${login}/status`
690
+ );
698
691
  }
699
692
  /**
700
693
  * Get account statistics with optional filters
701
694
  */
702
695
  async getStatistics(params) {
703
- return this.http.get("/api/v1/accounts/statistics", params);
704
- }
705
- /**
706
- * Change master password for an account
707
- */
708
- async changeMasterPassword(login, request) {
709
- return this.http.put(`/api/v1/accounts/${login}/password/master`, request);
696
+ return this.http.get(
697
+ "/api/v1/accounts/statistics",
698
+ params
699
+ );
710
700
  }
711
701
  /**
712
- * Change investor password for an account
702
+ * Change account passwords (master and/or investor)
713
703
  */
714
- async changeInvestorPassword(login, request) {
715
- return this.http.put(`/api/v1/accounts/${login}/password/investor`, request);
704
+ async changePassword(login, request) {
705
+ return this.http.put(`/api/v1/accounts/${login}/password`, request);
716
706
  }
717
707
  /**
718
708
  * Archive an account
@@ -731,14 +721,20 @@ var AccountsAPI = class {
731
721
  * All fields are optional, only send the ones you want to update
732
722
  */
733
723
  async update(login, request) {
734
- return this.http.put(`/api/v1/accounts/${login}`, request);
724
+ return this.http.put(
725
+ `/api/v1/accounts/${login}`,
726
+ request
727
+ );
735
728
  }
736
729
  /**
737
730
  * Adjust balance or credit for an account
738
731
  * The adjustment is relative: amount is added or subtracted from current value
739
732
  */
740
733
  async balance(login, request) {
741
- return this.http.put(`/api/v1/accounts/${login}/adjust`, request);
734
+ return this.http.put(
735
+ `/api/v1/accounts/${login}/adjust`,
736
+ request
737
+ );
742
738
  }
743
739
  /**
744
740
  * Get account permissions
@@ -1118,6 +1114,15 @@ var PipsendClient = class {
1118
1114
  }
1119
1115
  this.wsManager.on("balance:updated", callback);
1120
1116
  },
1117
+ /**
1118
+ * Listens to account metrics events (replaces balance updates)
1119
+ */
1120
+ onAccountMetrics: (callback) => {
1121
+ if (!this.wsManager) {
1122
+ throw new WebSocketError("WebSocket not enabled");
1123
+ }
1124
+ this.wsManager.on("account:metrics", callback);
1125
+ },
1121
1126
  // Legacy methods (deprecated but kept for compatibility)
1122
1127
  /**
1123
1128
  * @deprecated Use onPositionUpdated instead
@@ -1186,6 +1191,15 @@ var PipsendClient = class {
1186
1191
  }
1187
1192
  this.wsManager.on("error", callback);
1188
1193
  },
1194
+ /**
1195
+ * Registers a custom event listener
1196
+ */
1197
+ on: (event, callback) => {
1198
+ if (!this.wsManager) {
1199
+ throw new WebSocketError("WebSocket not enabled");
1200
+ }
1201
+ this.wsManager.on(event, callback);
1202
+ },
1189
1203
  /**
1190
1204
  * Removes event listener
1191
1205
  */