laplace-api 2.1.0 → 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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "laplace-api",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Client library for Laplace API for the US stock market and BIST (Istanbul stock market) fundamental financial data.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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
 
@@ -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