@kevisual/query 0.0.4 → 0.0.6

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/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { adapter } from './adapter.ts';
2
- export { QueryWs } from './ws.ts';
2
+ import { QueryWs } from './ws.ts';
3
+ export { QueryOpts };
3
4
  type Fn = (opts: {
4
5
  url?: string;
5
6
  headers?: Record<string, string>;
@@ -35,6 +36,8 @@ type DataOpts = Partial<QueryOpts> & {
35
36
  * path: 'demo',
36
37
  * key: '1',
37
38
  * });
39
+ *
40
+ * U是参数 V是返回值
38
41
  */
39
42
  export declare class Query<U = any, V = any> {
40
43
  adapter: typeof adapter;
@@ -49,4 +52,20 @@ export declare class Query<U = any, V = any> {
49
52
  before(fn: Fn): void;
50
53
  after(fn: (result: Result) => Promise<any>): void;
51
54
  }
55
+ /**
56
+ * 前端调用后端QueryRouter
57
+ */
58
+ export declare class QueryClient<U = any, V = any> extends Query<U, V> {
59
+ tokenName: string;
60
+ storage: Storage;
61
+ token: string;
62
+ qws: QueryWs;
63
+ constructor(opts?: QueryOpts & {
64
+ tokenName?: string;
65
+ storage?: Storage;
66
+ });
67
+ getToken(): string;
68
+ saveToken(token: string): void;
69
+ removeToken(): void;
70
+ }
52
71
  export { adapter };
package/dist/index.js CHANGED
@@ -130,6 +130,7 @@ class QueryWs {
130
130
  };
131
131
  ws.onclose = () => {
132
132
  store.getState().setConnected(false);
133
+ store.getState().setStatus('disconnected');
133
134
  this.ws = null;
134
135
  };
135
136
  }
@@ -212,6 +213,8 @@ class QueryWs {
212
213
  * path: 'demo',
213
214
  * key: '1',
214
215
  * });
216
+ *
217
+ * U是参数 V是返回值
215
218
  */
216
219
  class Query {
217
220
  adapter;
@@ -236,6 +239,7 @@ class Query {
236
239
  const headers = { ...this.headers, ...options?.headers };
237
240
  const adapter = options?.adapter || this.adapter;
238
241
  const beforeRequest = options?.beforeRequest || this.beforeRequest;
242
+ const afterResponse = options?.afterResponse || this.afterResponse;
239
243
  const timeout = options?.timeout || this.timeout;
240
244
  const req = {
241
245
  url: url,
@@ -248,8 +252,8 @@ class Query {
248
252
  }
249
253
  return adapter(req).then(async (res) => {
250
254
  res.success = res.code === 200;
251
- if (options?.afterResponse) {
252
- return await options.afterResponse(res);
255
+ if (afterResponse) {
256
+ return await afterResponse(res);
253
257
  }
254
258
  return res;
255
259
  });
@@ -261,5 +265,39 @@ class Query {
261
265
  this.afterResponse = fn;
262
266
  }
263
267
  }
268
+ /**
269
+ * 前端调用后端QueryRouter
270
+ */
271
+ class QueryClient extends Query {
272
+ tokenName;
273
+ storage;
274
+ token;
275
+ qws;
276
+ constructor(opts) {
277
+ super(opts);
278
+ this.tokenName = opts?.tokenName || 'token';
279
+ this.storage = opts?.storage || localStorage;
280
+ this.beforeRequest = async (opts) => {
281
+ const token = this.token || this.getToken();
282
+ if (token) {
283
+ opts.headers = {
284
+ ...opts.headers,
285
+ Authorization: `Bearer ${token}`,
286
+ };
287
+ }
288
+ return opts;
289
+ };
290
+ this.qws = new QueryWs({ url: opts?.url });
291
+ }
292
+ getToken() {
293
+ return this.storage.getItem(this.tokenName);
294
+ }
295
+ saveToken(token) {
296
+ this.storage.setItem(this.tokenName, token);
297
+ }
298
+ removeToken() {
299
+ this.storage.removeItem(this.tokenName);
300
+ }
301
+ }
264
302
 
265
- export { Query, QueryWs, adapter };
303
+ export { Query, QueryClient, adapter };
package/dist/ws.js CHANGED
@@ -89,6 +89,7 @@ class QueryWs {
89
89
  };
90
90
  ws.onclose = () => {
91
91
  store.getState().setConnected(false);
92
+ store.getState().setStatus('disconnected');
92
93
  this.ws = null;
93
94
  };
94
95
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,8 +14,11 @@
14
14
  "files": [
15
15
  "dist"
16
16
  ],
17
- "keywords": [],
18
- "author": "",
17
+ "keywords": [
18
+ "kevisual",
19
+ "query"
20
+ ],
21
+ "author": "abearxiong",
19
22
  "license": "ISC",
20
23
  "description": "",
21
24
  "devDependencies": {
@@ -31,6 +34,10 @@
31
34
  "publishConfig": {
32
35
  "access": "public"
33
36
  },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+ssh://git@github.com/abearxiong/kevisual-query.git"
40
+ },
34
41
  "exports": {
35
42
  ".": {
36
43
  "import": "./dist/index.js",