@pipsend/sdk 0.2.0 → 0.3.0
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.d.mts +24 -10
- package/dist/index.d.ts +24 -10
- package/dist/index.js +38 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +38 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -18,7 +18,7 @@ interface WebSocketConfig {
|
|
|
18
18
|
/**
|
|
19
19
|
* Available WebSocket channels for subscription
|
|
20
20
|
*/
|
|
21
|
-
type WebSocketChannel = 'positions:new' | 'positions:closed' | 'positions:updated' | 'accounts:balance';
|
|
21
|
+
type WebSocketChannel = 'positions:new' | 'positions:closed' | 'positions:updated' | 'accounts:balance' | 'account_metrics';
|
|
22
22
|
/**
|
|
23
23
|
* WebSocket operation types from server
|
|
24
24
|
*/
|
|
@@ -79,21 +79,27 @@ interface PositionEvent {
|
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
* Balance update event
|
|
82
|
+
* Balance update event (account metrics)
|
|
83
83
|
*/
|
|
84
84
|
interface BalanceEvent {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
metrics: {
|
|
86
|
+
balance: number;
|
|
87
|
+
equity: number;
|
|
88
|
+
margin: number;
|
|
89
|
+
free_margin: number;
|
|
90
|
+
margin_level: number;
|
|
91
|
+
total_pnl: number;
|
|
92
|
+
leverage: number;
|
|
93
|
+
currency: string;
|
|
94
|
+
};
|
|
95
|
+
ts: number;
|
|
96
|
+
type: 'account_metrics';
|
|
97
|
+
login?: number;
|
|
92
98
|
}
|
|
93
99
|
/**
|
|
94
100
|
* WebSocket event types (internal)
|
|
95
101
|
*/
|
|
96
|
-
type WebSocketEventType = 'position:opened' | 'position:closed' | 'position:updated' | 'balance:updated' | 'connected' | 'disconnected' | 'error' | 'subscription_success' | 'unsubscription_success' | '*';
|
|
102
|
+
type WebSocketEventType = 'position:opened' | 'position:closed' | 'position:updated' | 'balance:updated' | 'account:metrics' | 'connected' | 'disconnected' | 'error' | 'subscription_success' | 'unsubscription_success' | '*';
|
|
97
103
|
/**
|
|
98
104
|
* @deprecated Use PositionEvent instead
|
|
99
105
|
*/
|
|
@@ -1465,6 +1471,10 @@ declare class PipsendClient {
|
|
|
1465
1471
|
* Listens to balance updated events
|
|
1466
1472
|
*/
|
|
1467
1473
|
onBalanceUpdated: (callback: WebSocketCallback<BalanceEvent>) => void;
|
|
1474
|
+
/**
|
|
1475
|
+
* Listens to account metrics events (replaces balance updates)
|
|
1476
|
+
*/
|
|
1477
|
+
onAccountMetrics: (callback: WebSocketCallback<BalanceEvent>) => void;
|
|
1468
1478
|
/**
|
|
1469
1479
|
* @deprecated Use onPositionUpdated instead
|
|
1470
1480
|
*/
|
|
@@ -1493,6 +1503,10 @@ declare class PipsendClient {
|
|
|
1493
1503
|
* Listens to error events
|
|
1494
1504
|
*/
|
|
1495
1505
|
onError: (callback: WebSocketCallback) => void;
|
|
1506
|
+
/**
|
|
1507
|
+
* Registers a custom event listener
|
|
1508
|
+
*/
|
|
1509
|
+
on: (event: string, callback: WebSocketCallback) => void;
|
|
1496
1510
|
/**
|
|
1497
1511
|
* Removes event listener
|
|
1498
1512
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ interface WebSocketConfig {
|
|
|
18
18
|
/**
|
|
19
19
|
* Available WebSocket channels for subscription
|
|
20
20
|
*/
|
|
21
|
-
type WebSocketChannel = 'positions:new' | 'positions:closed' | 'positions:updated' | 'accounts:balance';
|
|
21
|
+
type WebSocketChannel = 'positions:new' | 'positions:closed' | 'positions:updated' | 'accounts:balance' | 'account_metrics';
|
|
22
22
|
/**
|
|
23
23
|
* WebSocket operation types from server
|
|
24
24
|
*/
|
|
@@ -79,21 +79,27 @@ interface PositionEvent {
|
|
|
79
79
|
};
|
|
80
80
|
}
|
|
81
81
|
/**
|
|
82
|
-
* Balance update event
|
|
82
|
+
* Balance update event (account metrics)
|
|
83
83
|
*/
|
|
84
84
|
interface BalanceEvent {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
metrics: {
|
|
86
|
+
balance: number;
|
|
87
|
+
equity: number;
|
|
88
|
+
margin: number;
|
|
89
|
+
free_margin: number;
|
|
90
|
+
margin_level: number;
|
|
91
|
+
total_pnl: number;
|
|
92
|
+
leverage: number;
|
|
93
|
+
currency: string;
|
|
94
|
+
};
|
|
95
|
+
ts: number;
|
|
96
|
+
type: 'account_metrics';
|
|
97
|
+
login?: number;
|
|
92
98
|
}
|
|
93
99
|
/**
|
|
94
100
|
* WebSocket event types (internal)
|
|
95
101
|
*/
|
|
96
|
-
type WebSocketEventType = 'position:opened' | 'position:closed' | 'position:updated' | 'balance:updated' | 'connected' | 'disconnected' | 'error' | 'subscription_success' | 'unsubscription_success' | '*';
|
|
102
|
+
type WebSocketEventType = 'position:opened' | 'position:closed' | 'position:updated' | 'balance:updated' | 'account:metrics' | 'connected' | 'disconnected' | 'error' | 'subscription_success' | 'unsubscription_success' | '*';
|
|
97
103
|
/**
|
|
98
104
|
* @deprecated Use PositionEvent instead
|
|
99
105
|
*/
|
|
@@ -1465,6 +1471,10 @@ declare class PipsendClient {
|
|
|
1465
1471
|
* Listens to balance updated events
|
|
1466
1472
|
*/
|
|
1467
1473
|
onBalanceUpdated: (callback: WebSocketCallback<BalanceEvent>) => void;
|
|
1474
|
+
/**
|
|
1475
|
+
* Listens to account metrics events (replaces balance updates)
|
|
1476
|
+
*/
|
|
1477
|
+
onAccountMetrics: (callback: WebSocketCallback<BalanceEvent>) => void;
|
|
1468
1478
|
/**
|
|
1469
1479
|
* @deprecated Use onPositionUpdated instead
|
|
1470
1480
|
*/
|
|
@@ -1493,6 +1503,10 @@ declare class PipsendClient {
|
|
|
1493
1503
|
* Listens to error events
|
|
1494
1504
|
*/
|
|
1495
1505
|
onError: (callback: WebSocketCallback) => void;
|
|
1506
|
+
/**
|
|
1507
|
+
* Registers a custom event listener
|
|
1508
|
+
*/
|
|
1509
|
+
on: (event: string, callback: WebSocketCallback) => void;
|
|
1496
1510
|
/**
|
|
1497
1511
|
* Removes event listener
|
|
1498
1512
|
*/
|
package/dist/index.js
CHANGED
|
@@ -134,11 +134,6 @@ var AuthManager = class {
|
|
|
134
134
|
}
|
|
135
135
|
const data = await response.json();
|
|
136
136
|
this.setTokenInfo(data);
|
|
137
|
-
console.log("\u2705 Authentication successful");
|
|
138
|
-
console.log(` User: ${data.user.first_name} ${data.user.last_name} (${data.user.email})`);
|
|
139
|
-
console.log(` Login: ${data.user.login}`);
|
|
140
|
-
console.log(` Balance: $${data.user.balance.toFixed(2)}`);
|
|
141
|
-
console.log(` Logged with: ${data.logged_with}`);
|
|
142
137
|
} catch (error) {
|
|
143
138
|
if (error instanceof AuthenticationError) {
|
|
144
139
|
throw error;
|
|
@@ -153,7 +148,6 @@ var AuthManager = class {
|
|
|
153
148
|
*/
|
|
154
149
|
async refreshToken() {
|
|
155
150
|
if (!this.tokenInfo?.refreshToken) {
|
|
156
|
-
console.log("\u26A0\uFE0F No refresh token available, performing full authentication");
|
|
157
151
|
this.tokenInfo = void 0;
|
|
158
152
|
await this.authenticate();
|
|
159
153
|
return;
|
|
@@ -167,7 +161,6 @@ var AuthManager = class {
|
|
|
167
161
|
})
|
|
168
162
|
});
|
|
169
163
|
if (!response.ok) {
|
|
170
|
-
console.log("\u26A0\uFE0F Refresh token failed, performing full authentication");
|
|
171
164
|
this.tokenInfo = void 0;
|
|
172
165
|
await this.authenticate();
|
|
173
166
|
return;
|
|
@@ -182,9 +175,7 @@ var AuthManager = class {
|
|
|
182
175
|
issuedAt: now,
|
|
183
176
|
expiresAt
|
|
184
177
|
};
|
|
185
|
-
console.log("\u2705 Token refreshed successfully");
|
|
186
178
|
} catch (error) {
|
|
187
|
-
console.error("\u274C Error refreshing token:", error);
|
|
188
179
|
this.tokenInfo = void 0;
|
|
189
180
|
await this.authenticate();
|
|
190
181
|
}
|
|
@@ -236,11 +227,9 @@ var WebSocketManager = class {
|
|
|
236
227
|
*/
|
|
237
228
|
async connect() {
|
|
238
229
|
if (this.isConnected) {
|
|
239
|
-
console.log("\u26A0\uFE0F WebSocket already connected");
|
|
240
230
|
return;
|
|
241
231
|
}
|
|
242
232
|
if (this.isReconnecting) {
|
|
243
|
-
console.log("\u26A0\uFE0F WebSocket reconnection in progress");
|
|
244
233
|
return;
|
|
245
234
|
}
|
|
246
235
|
const token = await this.authManager.ensureAuthenticated();
|
|
@@ -251,13 +240,11 @@ var WebSocketManager = class {
|
|
|
251
240
|
const WS = this.getWebSocketConstructor();
|
|
252
241
|
this.ws = new WS(wsUrl);
|
|
253
242
|
this.ws.onopen = () => {
|
|
254
|
-
console.log("\u2705 WebSocket connected");
|
|
255
243
|
this.isConnected = true;
|
|
256
244
|
this.isReconnecting = false;
|
|
257
245
|
this.reconnectAttempts = 0;
|
|
258
246
|
this.emit("connected", { timestamp: Date.now() });
|
|
259
247
|
if (this.subscribedChannels.length > 0) {
|
|
260
|
-
console.log("\u{1F504} Resubscribing to channels:", this.subscribedChannels);
|
|
261
248
|
this.subscribe(this.subscribedChannels);
|
|
262
249
|
}
|
|
263
250
|
this.startHeartbeat();
|
|
@@ -279,7 +266,6 @@ var WebSocketManager = class {
|
|
|
279
266
|
}
|
|
280
267
|
};
|
|
281
268
|
this.ws.onclose = (event) => {
|
|
282
|
-
console.log("WebSocket disconnected", event.code, event.reason);
|
|
283
269
|
this.isConnected = false;
|
|
284
270
|
this.stopHeartbeat();
|
|
285
271
|
this.emit("disconnected", {
|
|
@@ -309,14 +295,12 @@ var WebSocketManager = class {
|
|
|
309
295
|
channels
|
|
310
296
|
};
|
|
311
297
|
this.sendAction(message);
|
|
312
|
-
console.log("\u{1F4E1} Subscribed to channels:", channels);
|
|
313
298
|
}
|
|
314
299
|
/**
|
|
315
300
|
* Unsubscribes from WebSocket channels
|
|
316
301
|
*/
|
|
317
302
|
unsubscribe(channels) {
|
|
318
303
|
if (!this.isConnected) {
|
|
319
|
-
console.warn("WebSocket not connected, cannot unsubscribe");
|
|
320
304
|
return;
|
|
321
305
|
}
|
|
322
306
|
this.subscribedChannels = this.subscribedChannels.filter(
|
|
@@ -327,7 +311,6 @@ var WebSocketManager = class {
|
|
|
327
311
|
channels
|
|
328
312
|
};
|
|
329
313
|
this.sendAction(message);
|
|
330
|
-
console.log("\u{1F4E1} Unsubscribed from channels:", channels);
|
|
331
314
|
}
|
|
332
315
|
/**
|
|
333
316
|
* Registers an event listener
|
|
@@ -352,7 +335,6 @@ var WebSocketManager = class {
|
|
|
352
335
|
* Disconnects from WebSocket
|
|
353
336
|
*/
|
|
354
337
|
disconnect() {
|
|
355
|
-
console.log("\u{1F44B} Disconnecting WebSocket...");
|
|
356
338
|
this.stopHeartbeat();
|
|
357
339
|
if (this.ws) {
|
|
358
340
|
this.ws.close();
|
|
@@ -410,7 +392,6 @@ var WebSocketManager = class {
|
|
|
410
392
|
*/
|
|
411
393
|
sendAction(message) {
|
|
412
394
|
if (!this.ws || !this.isConnected) {
|
|
413
|
-
console.warn("\u26A0\uFE0F WebSocket not ready, message not sent:", message.action);
|
|
414
395
|
return;
|
|
415
396
|
}
|
|
416
397
|
try {
|
|
@@ -454,7 +435,7 @@ var WebSocketManager = class {
|
|
|
454
435
|
this.handleError(message);
|
|
455
436
|
break;
|
|
456
437
|
default:
|
|
457
|
-
|
|
438
|
+
break;
|
|
458
439
|
}
|
|
459
440
|
this.emit("*", message);
|
|
460
441
|
}
|
|
@@ -462,22 +443,18 @@ var WebSocketManager = class {
|
|
|
462
443
|
* Handles connected message from server
|
|
463
444
|
*/
|
|
464
445
|
handleConnected(message) {
|
|
465
|
-
console.log("\u2705 WebSocket connected:", message.data.message);
|
|
466
|
-
console.log(" Available channels:", message.data.available_channels);
|
|
467
446
|
this.emit("connected", message.data);
|
|
468
447
|
}
|
|
469
448
|
/**
|
|
470
449
|
* Handles subscription success
|
|
471
450
|
*/
|
|
472
451
|
handleSubscriptionSuccess(message) {
|
|
473
|
-
console.log("\u2705 Subscription successful:", message.data.subscribed);
|
|
474
452
|
this.emit("subscription_success", message.data);
|
|
475
453
|
}
|
|
476
454
|
/**
|
|
477
455
|
* Handles unsubscription success
|
|
478
456
|
*/
|
|
479
457
|
handleUnsubscriptionSuccess(message) {
|
|
480
|
-
console.log("\u2705 Unsubscription successful:", message.data.unsubscribed);
|
|
481
458
|
this.emit("unsubscription_success", message.data);
|
|
482
459
|
}
|
|
483
460
|
/**
|
|
@@ -497,11 +474,27 @@ var WebSocketManager = class {
|
|
|
497
474
|
case "position.updated":
|
|
498
475
|
this.emit("position:updated", eventData);
|
|
499
476
|
break;
|
|
477
|
+
case "account_metrics":
|
|
478
|
+
const balanceEvent = eventData;
|
|
479
|
+
if (message.topic?.startsWith("balance:")) {
|
|
480
|
+
const loginMatch = message.topic.match(/balance:api:(\d+)/);
|
|
481
|
+
if (loginMatch) {
|
|
482
|
+
balanceEvent.login = parseInt(loginMatch[1], 10);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
this.emit("account:metrics", balanceEvent);
|
|
486
|
+
this.emit("balance:updated", balanceEvent);
|
|
487
|
+
break;
|
|
500
488
|
default:
|
|
501
489
|
if (message.topic?.startsWith("balance:")) {
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
490
|
+
const event = eventData;
|
|
491
|
+
const loginMatch = message.topic.match(/balance:api:(\d+)/);
|
|
492
|
+
if (loginMatch) {
|
|
493
|
+
event.login = parseInt(loginMatch[1], 10);
|
|
494
|
+
}
|
|
495
|
+
this.emit("balance:updated", event);
|
|
496
|
+
} else if (message.topic?.startsWith("account_metrics")) {
|
|
497
|
+
this.emit("account:metrics", eventData);
|
|
505
498
|
}
|
|
506
499
|
}
|
|
507
500
|
}
|
|
@@ -537,18 +530,13 @@ var WebSocketManager = class {
|
|
|
537
530
|
const autoReconnect = this.config.websocket?.autoReconnect ?? true;
|
|
538
531
|
const maxAttempts = this.config.websocket?.maxReconnectAttempts ?? 5;
|
|
539
532
|
if (!autoReconnect) {
|
|
540
|
-
console.log("Auto-reconnect disabled");
|
|
541
533
|
return;
|
|
542
534
|
}
|
|
543
535
|
if (this.reconnectAttempts >= maxAttempts) {
|
|
544
|
-
console.log(`\u274C Max reconnection attempts (${maxAttempts}) reached. Giving up.`);
|
|
545
536
|
this.emit("error", new WebSocketError("Max reconnection attempts reached"));
|
|
546
537
|
return;
|
|
547
538
|
}
|
|
548
539
|
const delay = Math.min(1e3 * Math.pow(2, this.reconnectAttempts), 3e4);
|
|
549
|
-
console.log(
|
|
550
|
-
`\u{1F504} Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts + 1}/${maxAttempts})...`
|
|
551
|
-
);
|
|
552
540
|
this.isReconnecting = true;
|
|
553
541
|
setTimeout(() => {
|
|
554
542
|
this.reconnectAttempts++;
|
|
@@ -1143,6 +1131,15 @@ var PipsendClient = class {
|
|
|
1143
1131
|
}
|
|
1144
1132
|
this.wsManager.on("balance:updated", callback);
|
|
1145
1133
|
},
|
|
1134
|
+
/**
|
|
1135
|
+
* Listens to account metrics events (replaces balance updates)
|
|
1136
|
+
*/
|
|
1137
|
+
onAccountMetrics: (callback) => {
|
|
1138
|
+
if (!this.wsManager) {
|
|
1139
|
+
throw new WebSocketError("WebSocket not enabled");
|
|
1140
|
+
}
|
|
1141
|
+
this.wsManager.on("account:metrics", callback);
|
|
1142
|
+
},
|
|
1146
1143
|
// Legacy methods (deprecated but kept for compatibility)
|
|
1147
1144
|
/**
|
|
1148
1145
|
* @deprecated Use onPositionUpdated instead
|
|
@@ -1211,6 +1208,15 @@ var PipsendClient = class {
|
|
|
1211
1208
|
}
|
|
1212
1209
|
this.wsManager.on("error", callback);
|
|
1213
1210
|
},
|
|
1211
|
+
/**
|
|
1212
|
+
* Registers a custom event listener
|
|
1213
|
+
*/
|
|
1214
|
+
on: (event, callback) => {
|
|
1215
|
+
if (!this.wsManager) {
|
|
1216
|
+
throw new WebSocketError("WebSocket not enabled");
|
|
1217
|
+
}
|
|
1218
|
+
this.wsManager.on(event, callback);
|
|
1219
|
+
},
|
|
1214
1220
|
/**
|
|
1215
1221
|
* Removes event listener
|
|
1216
1222
|
*/
|