configurapi-handler-ws 1.1.2 → 1.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/README.md CHANGED
@@ -35,8 +35,10 @@ Configurapi request handlers for websocket
35
35
 
36
36
  * Use WebSocketClient to connect to a WebSocket runner, send data over the connection, and disconnect from the runner when finished.
37
37
  * To pass data (such as a protocol or authentication token) when establishing the WebSocket connection, provide it as the second parameter to the constructor.
38
+ * If a connection could not be made (e.g. the on_connect route returns 400+ status code), an error will be sent to onPush().
39
+ * The client has a default timeout of 27 seconds. To increase the wait time for results, set the `replyTimeoutMs` parameter in the constructor's second argument.
38
40
 
39
41
  ```
40
42
  const { WebSocketClient } = require("configurapi-handler-ws");
41
- let client = new WebSocketClient(WEBSOCKET_BASE_URL, {onPush: (data:IResponse)=>console.log(data)});
43
+ let client = new WebSocketClient(WEBSOCKET_BASE_URL, {replyTimeoutMs: 300000, onPush: (data:IResponse)=>console.log(data)});
42
44
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "configurapi-handler-ws",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "main": "src/index",
5
5
  "files": [
6
6
  "src/*",
@@ -23,5 +23,8 @@
23
23
  "description": "",
24
24
  "devDependencies": {
25
25
  "@types/node": "^25.2.3"
26
+ },
27
+ "dependencies": {
28
+ "uuid": "^13.0.0"
26
29
  }
27
30
  }
@@ -1,4 +1,4 @@
1
- const { randomUUID } = require("node:crypto");
1
+ const { v4: uuidv4 } = require('uuid');
2
2
  const StreamResponse = require("./streamResponse");
3
3
 
4
4
  // tiny sleep for retry/backoff
@@ -50,7 +50,7 @@ module.exports = async function postbackFunction(postBackFunction, response)
50
50
  };
51
51
 
52
52
  const stream = response.body;
53
- const streamId = baseHeaders?.['message-id'] || randomUUID();
53
+ const streamId = baseHeaders?.['message-id'] || uuidv4();
54
54
 
55
55
  // Guard: need a readable stream
56
56
  if(!stream || typeof stream.on !== 'function')
@@ -1,6 +1,4 @@
1
- const Response = require('configurapi').Response;
2
-
3
- module.exports = class PostbackResponse extends Response
1
+ module.exports = class PostbackResponse
4
2
  {
5
3
  body;
6
4
  headers;
@@ -9,7 +7,5 @@ module.exports = class PostbackResponse extends Response
9
7
  {
10
8
  this.body = body;
11
9
  this.headers = headers;
12
-
13
- super(body, 200, headers);
14
10
  }
15
11
  };
@@ -1,9 +1,13 @@
1
- const Response = require('configurapi').Response;
2
-
3
- module.exports = class StreamResponse extends Response
1
+ module.exports = class StreamResponse
4
2
  {
5
- constructor(stream, statusCode = 200, headers = {})
3
+ statusCode = 200;
4
+ body;
5
+ headers;
6
+
7
+ constructor(stream, statusCode, headers = {})
6
8
  {
7
- super(stream, statusCode, headers);
9
+ this.body = stream;
10
+ this.statusCode = statusCode;
11
+ this.headers = headers;
8
12
  }
9
13
  };
@@ -9,6 +9,7 @@ export interface ClientOptions
9
9
  /** If a response arrives without an id, deliver it here (or ignore if omitted) */
10
10
  onPush?: (data: {
11
11
  body: any;
12
+ error: string;
12
13
  statusCode: number;
13
14
  headers: Record<string, string>;
14
15
  }) => Promise<void>;
@@ -199,7 +199,7 @@ module.exports = class WebSocketClient
199
199
  }
200
200
 
201
201
  const id = this.getHeaderCaseInsensitive(msg.headers, MessageIdHeaderName);
202
-
202
+
203
203
  if (msg?.statusCode && msg.statusCode !== 202 && id && this.inflight.has(id))
204
204
  {
205
205
  const waiter = this.inflight.get(id);
@@ -222,14 +222,25 @@ module.exports = class WebSocketClient
222
222
  }
223
223
  else
224
224
  {
225
+ //no inflight ws connections.
225
226
  if(msg?.statusCode > 299)
226
227
  {
227
- const error = new Error(`${msg.message} - (${msg.statusCode})`)
228
- throw error;
228
+ const error = new Error(`${msg.message} - (${msg.statusCode})`);
229
+
230
+ if(this.onPush)
231
+ {
232
+ this.onPush(msg)
233
+ }
234
+ else
235
+ {
236
+ throw error;
237
+ }
238
+ }
239
+ else
240
+ {
241
+ // No id or unknown id. Treat as push/unsolicited.
242
+ if (this.onPush) this.onPush(msg);
229
243
  }
230
-
231
- // No id or unknown id. Treat as push/unsolicited.
232
- if (this.onPush) this.onPush(msg);
233
244
  }
234
245
  }
235
246