@redonvn/event-ws-cliproxyapi-sdk 0.2.2 → 0.2.3

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
@@ -280,8 +280,16 @@ await provider.connect({
280
280
  }
281
281
  });
282
282
 
283
- provider.close();
284
- ```
283
+ provider.close();
284
+ ```
285
+
286
+ Update credentials at runtime:
287
+ ```ts
288
+ provider.setCredentials('api_new', 'mgmt_new');
289
+ await provider.reconnect();
290
+ // or:
291
+ await provider.setCredentialsAndReconnect('api_new', 'mgmt_new');
292
+ ```
285
293
 
286
294
  ## Usage (HTTP Client)
287
295
 
@@ -7,12 +7,15 @@ export declare class CliproxyWSProvider {
7
7
  private ws?;
8
8
  private options;
9
9
  private handlers?;
10
+ private lastHandlers?;
10
11
  constructor(options: ProviderOptions);
11
12
  connect(handlers: ProviderHandlers): Promise<void>;
12
13
  close(): void;
14
+ reconnect(): Promise<void>;
13
15
  setAccessKey(accessKey?: string): void;
14
16
  setManagementKey(managementKey?: string): void;
15
17
  setCredentials(accessKey?: string, managementKey?: string): void;
18
+ setCredentialsAndReconnect(accessKey?: string, managementKey?: string): Promise<void>;
16
19
  private handleMessage;
17
20
  private handleClose;
18
21
  private emitError;
package/dist/ws/client.js CHANGED
@@ -10,6 +10,7 @@ export class CliproxyWSProvider {
10
10
  // It resolves once the socket is open.
11
11
  connect(handlers) {
12
12
  this.handlers = handlers;
13
+ this.lastHandlers = handlers;
13
14
  if (this.ws && this.ws.readyState === WebSocket.OPEN)
14
15
  return Promise.resolve();
15
16
  const base = this.options.baseUrl.replace(/\/+$/, '');
@@ -57,6 +58,13 @@ export class CliproxyWSProvider {
57
58
  close() {
58
59
  this.ws?.close();
59
60
  }
61
+ async reconnect() {
62
+ this.close();
63
+ if (!this.lastHandlers) {
64
+ throw new Error('wsrelay: reconnect requires handlers (call connect at least once)');
65
+ }
66
+ await this.connect(this.lastHandlers);
67
+ }
60
68
  setAccessKey(accessKey) {
61
69
  this.options.accessKey = accessKey;
62
70
  }
@@ -67,6 +75,10 @@ export class CliproxyWSProvider {
67
75
  this.options.accessKey = accessKey;
68
76
  this.options.managementKey = managementKey;
69
77
  }
78
+ async setCredentialsAndReconnect(accessKey, managementKey) {
79
+ this.setCredentials(accessKey, managementKey);
80
+ await this.reconnect();
81
+ }
70
82
  handleMessage(raw) {
71
83
  let msg;
72
84
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redonvn/event-ws-cliproxyapi-sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "TypeScript SDK for CLIProxyAPI WebSocket relay (provider-side).",
5
5
  "license": "MIT",
6
6
  "type": "module",