@redonvn/event-ws-cliproxyapi-sdk 0.2.1 → 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
@@ -259,7 +259,8 @@ import { CliproxyWSProvider } from 'event-ws-cliproxyapi-sdk';
259
259
  const provider = new CliproxyWSProvider({
260
260
  baseUrl: 'http://127.0.0.1:8317',
261
261
  accessKey: 'andev',
262
- managementKey: 'mgmt_xxx'
262
+ managementKey: 'mgmt_xxx',
263
+ authViaQuery: false
263
264
  });
264
265
 
265
266
  await provider.connect({
@@ -279,8 +280,16 @@ await provider.connect({
279
280
  }
280
281
  });
281
282
 
282
- provider.close();
283
- ```
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
+ ```
284
293
 
285
294
  ## Usage (HTTP Client)
286
295
 
@@ -429,5 +438,8 @@ await openai.postChatCompletions({
429
438
  - `Authorization: Bearer <accessKey>` (backward compatible)
430
439
  - `X-Access-Key: <accessKey>`
431
440
  - `X-Management-Key: <managementKey>` (if provided)
441
+ - Optional query fallback (`authViaQuery: true`):
442
+ - `key=<accessKey>` + `auth_token=<accessKey>`
443
+ - `management_key=<managementKey>` + `mgmt_key=<managementKey>`
432
444
  - Management APIs require `managementKey` (or local password for `/keep-alive` if enabled).
433
445
  - HTTP client returns `Response` for streaming endpoints; parse SSE or chunks based on your client.
@@ -7,9 +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>;
15
+ setAccessKey(accessKey?: string): void;
16
+ setManagementKey(managementKey?: string): void;
17
+ setCredentials(accessKey?: string, managementKey?: string): void;
18
+ setCredentialsAndReconnect(accessKey?: string, managementKey?: string): Promise<void>;
13
19
  private handleMessage;
14
20
  private handleClose;
15
21
  private emitError;
package/dist/ws/client.js CHANGED
@@ -10,10 +10,25 @@ 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(/\/+$/, '');
16
- const url = `${base.replace(/^http/, 'ws')}/v1/ws`;
17
+ const wsPath = (this.options.wsPath || '/v1/ws').startsWith('/')
18
+ ? (this.options.wsPath || '/v1/ws')
19
+ : `/${this.options.wsPath || '/v1/ws'}`;
20
+ const urlObj = new URL(`${base.replace(/^http/, 'ws')}${wsPath}`);
21
+ if (this.options.authViaQuery) {
22
+ if (this.options.accessKey) {
23
+ urlObj.searchParams.set('key', this.options.accessKey);
24
+ urlObj.searchParams.set('auth_token', this.options.accessKey);
25
+ }
26
+ if (this.options.managementKey) {
27
+ urlObj.searchParams.set('management_key', this.options.managementKey);
28
+ urlObj.searchParams.set('mgmt_key', this.options.managementKey);
29
+ }
30
+ }
31
+ const url = urlObj.toString();
17
32
  const headers = {};
18
33
  if (this.options.accessKey) {
19
34
  headers['Authorization'] = `Bearer ${this.options.accessKey}`;
@@ -43,6 +58,27 @@ export class CliproxyWSProvider {
43
58
  close() {
44
59
  this.ws?.close();
45
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
+ }
68
+ setAccessKey(accessKey) {
69
+ this.options.accessKey = accessKey;
70
+ }
71
+ setManagementKey(managementKey) {
72
+ this.options.managementKey = managementKey;
73
+ }
74
+ setCredentials(accessKey, managementKey) {
75
+ this.options.accessKey = accessKey;
76
+ this.options.managementKey = managementKey;
77
+ }
78
+ async setCredentialsAndReconnect(accessKey, managementKey) {
79
+ this.setCredentials(accessKey, managementKey);
80
+ await this.reconnect();
81
+ }
46
82
  handleMessage(raw) {
47
83
  let msg;
48
84
  try {
@@ -76,6 +76,8 @@ export interface ProviderOptions {
76
76
  baseUrl: string;
77
77
  accessKey?: string;
78
78
  managementKey?: string;
79
+ wsPath?: string;
80
+ authViaQuery?: boolean;
79
81
  }
80
82
  export type ProviderEvent = {
81
83
  type: 'error';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@redonvn/event-ws-cliproxyapi-sdk",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "TypeScript SDK for CLIProxyAPI WebSocket relay (provider-side).",
5
5
  "license": "MIT",
6
6
  "type": "module",