@kevisual/query 0.0.19 → 0.0.20

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.
@@ -159,7 +159,9 @@ declare class Query {
159
159
  * 需要突然停止请求,比如401的时候
160
160
  */
161
161
  stop?: boolean;
162
+ qws: QueryWs;
162
163
  constructor(opts?: QueryOpts$1);
164
+ setQueryWs(qws: QueryWs): void;
163
165
  /**
164
166
  * 突然停止请求
165
167
  */
@@ -208,10 +210,13 @@ declare class BaseQuery<T extends Query = Query, R extends {
208
210
  queryDefine?: R;
209
211
  clientQuery?: T;
210
212
  });
211
- get chain(): any;
213
+ get chain(): R['queryChain'];
212
214
  post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
213
215
  get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
214
216
  }
217
+ declare class ClientQuery extends Query {
218
+ constructor(opts?: QueryOpts$1);
219
+ }
215
220
 
216
221
  type QueryOpts = {
217
222
  url?: string;
@@ -220,13 +225,12 @@ type QueryOpts = {
220
225
  timeout?: number;
221
226
  };
222
227
  /**
223
- * 前端调用后端QueryRouter
228
+ * 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
224
229
  */
225
230
  declare class QueryClient extends Query {
226
231
  tokenName: string;
227
232
  storage: Storage;
228
233
  token: string;
229
- qws: QueryWs;
230
234
  constructor(opts?: QueryOpts & {
231
235
  tokenName?: string;
232
236
  storage?: Storage;
@@ -238,5 +242,5 @@ declare class QueryClient extends Query {
238
242
  removeToken(): void;
239
243
  }
240
244
 
241
- export { BaseQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
245
+ export { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
242
246
  export type { Data, DataOpts, QueryOpts, QueryWsOpts, Result };
@@ -326,6 +326,8 @@ class Query {
326
326
  * 需要突然停止请求,比如401的时候
327
327
  */
328
328
  stop;
329
+ // 默认不使用ws
330
+ qws;
329
331
  constructor(opts) {
330
332
  this.adapter = opts?.adapter || adapter;
331
333
  this.url = opts?.url || '/api/router';
@@ -334,6 +336,9 @@ class Query {
334
336
  };
335
337
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
336
338
  }
339
+ setQueryWs(qws) {
340
+ this.qws = qws;
341
+ }
337
342
  /**
338
343
  * 突然停止请求
339
344
  */
@@ -469,16 +474,19 @@ class BaseQuery {
469
474
  return this.query.get(data, options);
470
475
  }
471
476
  }
477
+ class ClientQuery extends Query {
478
+ constructor(opts) {
479
+ super({ ...opts, url: opts?.url || '/client/router' });
480
+ }
481
+ }
472
482
 
473
483
  /**
474
- * 前端调用后端QueryRouter
484
+ * 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
475
485
  */
476
486
  class QueryClient extends Query {
477
487
  tokenName;
478
488
  storage;
479
489
  token;
480
- // 默认不使用ws
481
- qws;
482
490
  constructor(opts) {
483
491
  super(opts);
484
492
  this.tokenName = opts?.tokenName || 'token';
@@ -513,4 +521,4 @@ class QueryClient extends Query {
513
521
  // 移除默认生成的实例
514
522
  // export const client = new QueryClient();
515
523
 
516
- export { BaseQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
524
+ export { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
package/dist/query.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { StoreApi } from 'zustand/vanilla';
2
+
1
3
  declare const methods: readonly ["GET", "POST"];
2
4
  type Method = (typeof methods)[number];
3
5
  type AdapterOpts = {
@@ -15,6 +17,67 @@ type AdapterOpts = {
15
17
  */
16
18
  declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
17
19
 
20
+ type QueryWsStore = {
21
+ connected: boolean;
22
+ status: 'connecting' | 'connected' | 'disconnected';
23
+ setConnected: (connected: boolean) => void;
24
+ setStatus: (status: QuerySelectState) => void;
25
+ };
26
+ type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
27
+ type QueryWsOpts = {
28
+ url?: string;
29
+ store?: StoreApi<QueryWsStore>;
30
+ ws?: WebSocket;
31
+ };
32
+ declare class QueryWs {
33
+ url: string;
34
+ store: StoreApi<QueryWsStore>;
35
+ ws: WebSocket;
36
+ constructor(opts?: QueryWsOpts);
37
+ /**
38
+ * 连接 WebSocket
39
+ */
40
+ connect(opts?: {
41
+ timeout?: number;
42
+ }): Promise<unknown>;
43
+ /**
44
+ * ws.onopen 必须用这个去获取,否者会丢失链接信息
45
+ * @param callback
46
+ * @returns
47
+ */
48
+ listenConnect(callback: () => void): () => void;
49
+ listenClose(callback: () => void): () => void;
50
+ onMessage<T = any, U = any>(fn: (data: U, event: MessageEvent) => void, opts?: {
51
+ /**
52
+ * 是否将数据转换为 JSON
53
+ */
54
+ isJson?: boolean;
55
+ /**
56
+ * 选择器
57
+ */
58
+ selector?: (data: T) => U;
59
+ }): () => void;
60
+ close(): void;
61
+ /**
62
+ * 发送消息
63
+ *
64
+ * @param data
65
+ * @param opts
66
+ * @returns
67
+ */
68
+ send<T = any, U = any>(data: T, opts?: {
69
+ /**
70
+ * 是否将数据转换为 JSON
71
+ */
72
+ isJson?: boolean;
73
+ /**
74
+ * 包装数据
75
+ */
76
+ wrapper?: (data: T) => U;
77
+ }): void;
78
+ getOpen(): boolean;
79
+ }
80
+
18
81
  /**
19
82
  * 请求前处理函数
20
83
  * @param opts 请求配置
@@ -103,7 +166,9 @@ declare class Query {
103
166
  * 需要突然停止请求,比如401的时候
104
167
  */
105
168
  stop?: boolean;
169
+ qws: QueryWs;
106
170
  constructor(opts?: QueryOpts);
171
+ setQueryWs(qws: QueryWs): void;
107
172
  /**
108
173
  * 突然停止请求
109
174
  */
@@ -152,7 +217,7 @@ declare class BaseQuery<T extends Query = Query, R extends {
152
217
  queryDefine?: R;
153
218
  clientQuery?: T;
154
219
  });
155
- get chain(): any;
220
+ get chain(): R['queryChain'];
156
221
  post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
157
222
  get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
158
223
  }
package/dist/query.js CHANGED
@@ -110,6 +110,8 @@ class Query {
110
110
  * 需要突然停止请求,比如401的时候
111
111
  */
112
112
  stop;
113
+ // 默认不使用ws
114
+ qws;
113
115
  constructor(opts) {
114
116
  this.adapter = opts?.adapter || adapter;
115
117
  this.url = opts?.url || '/api/router';
@@ -118,6 +120,9 @@ class Query {
118
120
  };
119
121
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
120
122
  }
123
+ setQueryWs(qws) {
124
+ this.qws = qws;
125
+ }
121
126
  /**
122
127
  * 突然停止请求
123
128
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",