laplace-api 2.0.3 → 2.2.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/package.json
CHANGED
|
@@ -91,12 +91,13 @@ export class FinancialFundamentalsClient extends Client {
|
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
async getTopMovers(region: Region, page: number, pageSize: number, direction: TopMoverDirection): Promise<TopMover[]> {
|
|
94
|
+
async getTopMovers(region: Region, page: number, pageSize: number, direction: TopMoverDirection, assetType?: AssetType): Promise<TopMover[]> {
|
|
95
95
|
const url = new URL(`${this['baseUrl']}/api/v2/stock/top-movers`);
|
|
96
96
|
url.searchParams.append('region', region);
|
|
97
97
|
url.searchParams.append('page', page.toString());
|
|
98
98
|
url.searchParams.append('pageSize', pageSize.toString());
|
|
99
99
|
url.searchParams.append('direction', direction);
|
|
100
|
+
if (assetType) url.searchParams.append('assetType', assetType);
|
|
100
101
|
|
|
101
102
|
return this.sendRequest<TopMover[]>({
|
|
102
103
|
method: 'GET',
|
|
@@ -107,6 +107,9 @@ export class LivePriceWebSocketClient {
|
|
|
107
107
|
private wsUrl: string | null = null;
|
|
108
108
|
private readonly options: Required<WebSocketOptions>;
|
|
109
109
|
private connectPromise: Promise<void> | null = null;
|
|
110
|
+
private lastMessageTimestamp: number = 0;
|
|
111
|
+
private inactivityCheckInterval: NodeJS.Timeout | null = null;
|
|
112
|
+
private readonly INACTIVITY_TIMEOUT = 15000;
|
|
110
113
|
|
|
111
114
|
constructor(options: WebSocketOptions = {}) {
|
|
112
115
|
this.options = {
|
|
@@ -119,6 +122,32 @@ export class LivePriceWebSocketClient {
|
|
|
119
122
|
};
|
|
120
123
|
}
|
|
121
124
|
|
|
125
|
+
private startInactivityInterval() {
|
|
126
|
+
this.lastMessageTimestamp = Date.now();
|
|
127
|
+
if (this.inactivityCheckInterval) {
|
|
128
|
+
clearInterval(this.inactivityCheckInterval);
|
|
129
|
+
this.inactivityCheckInterval = null;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.inactivityCheckInterval = setInterval(async ()=> {
|
|
133
|
+
if (Date.now() - this.lastMessageTimestamp >= this.INACTIVITY_TIMEOUT) {
|
|
134
|
+
this.stopInactivityInterval();
|
|
135
|
+
try {
|
|
136
|
+
this.attemptReconnect();
|
|
137
|
+
} catch(error) {
|
|
138
|
+
this.log(`Failed to reconnect: ${error}`, "error");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}, this.INACTIVITY_TIMEOUT)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private stopInactivityInterval() {
|
|
145
|
+
if (this.inactivityCheckInterval) {
|
|
146
|
+
clearInterval(this.inactivityCheckInterval);
|
|
147
|
+
this.inactivityCheckInterval = null;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
122
151
|
private log(message: string, level: "info" | "error" | "warn" = "info") {
|
|
123
152
|
if (!this.options.enableLogging) return;
|
|
124
153
|
|
|
@@ -183,6 +212,7 @@ export class LivePriceWebSocketClient {
|
|
|
183
212
|
this.ws.onopen = () => {
|
|
184
213
|
this.reconnectAttempts = 0;
|
|
185
214
|
this.log("WebSocket connected");
|
|
215
|
+
this.startInactivityInterval();
|
|
186
216
|
resolve();
|
|
187
217
|
};
|
|
188
218
|
|
|
@@ -198,6 +228,8 @@ export class LivePriceWebSocketClient {
|
|
|
198
228
|
this.ws.onclose = () => {
|
|
199
229
|
this.isClosed = true;
|
|
200
230
|
this.log("WebSocket closed");
|
|
231
|
+
|
|
232
|
+
this.stopInactivityInterval();
|
|
201
233
|
if (this.closedReason !== WebSocketCloseReason.NORMAL_CLOSURE) {
|
|
202
234
|
try {
|
|
203
235
|
this.attemptReconnect();
|
|
@@ -225,6 +257,7 @@ export class LivePriceWebSocketClient {
|
|
|
225
257
|
};
|
|
226
258
|
|
|
227
259
|
this.ws.onmessage = (event) => {
|
|
260
|
+
this.lastMessageTimestamp = Date.now();
|
|
228
261
|
try {
|
|
229
262
|
const rawData = JSON.parse(event.data.toString());
|
|
230
263
|
|
|
@@ -4,6 +4,7 @@ import { Client } from '../client/client';
|
|
|
4
4
|
import { FinancialFundamentalsClient, TopMoverDirection } from '../client/financial_fundamentals';
|
|
5
5
|
import { Region } from '../client/collections';
|
|
6
6
|
import './client_test_suite';
|
|
7
|
+
import { AssetType } from '../client/stocks';
|
|
7
8
|
|
|
8
9
|
describe('FinancialFundamentals', () => {
|
|
9
10
|
let client: Client;
|
|
@@ -64,7 +65,7 @@ describe('FinancialFundamentals', () => {
|
|
|
64
65
|
const pageSize = 20;
|
|
65
66
|
|
|
66
67
|
async function testTopMovers(direction: TopMoverDirection, shouldBePositive: boolean) {
|
|
67
|
-
const result = await stockClient.getTopMovers(region, page, pageSize, direction);
|
|
68
|
+
const result = await stockClient.getTopMovers(region, page, pageSize, direction, AssetType.Stock);
|
|
68
69
|
|
|
69
70
|
expect(Array.isArray(result)).toBe(true);
|
|
70
71
|
expect(result.length).toBeGreaterThan(0);
|
|
@@ -80,6 +81,10 @@ describe('FinancialFundamentals', () => {
|
|
|
80
81
|
shouldBePositive ? mover.change > 0 : mover.change < 0
|
|
81
82
|
);
|
|
82
83
|
expect(directionCheck).toBe(true);
|
|
84
|
+
|
|
85
|
+
const assetTypeCheck = result.every(mover => mover.assetType === AssetType.Stock)
|
|
86
|
+
|
|
87
|
+
expect(assetTypeCheck).toBe(true);
|
|
83
88
|
|
|
84
89
|
expect(result.length).toBeLessThanOrEqual(pageSize);
|
|
85
90
|
}
|
package/src/utilities/test.env
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
BASE_URL=
|
|
2
|
-
API_KEY=
|
|
1
|
+
BASE_URL=https://api.finfree.app
|
|
2
|
+
API_KEY=api-6fa6cefb-16df-4a19-8351-54f83c6bbe2f
|