@kevisual/query 0.0.3 → 0.0.5

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,8 +36,10 @@ type DataOpts = Partial<QueryOpts> & {
35
36
  * path: 'demo',
36
37
  * key: '1',
37
38
  * });
39
+ *
40
+ * U是参数 V是返回值
38
41
  */
39
- export declare class Query {
42
+ export declare class Query<U = any, V = any> {
40
43
  adapter: typeof adapter;
41
44
  url: string;
42
45
  beforeRequest?: Fn;
@@ -44,9 +47,25 @@ export declare class Query {
44
47
  headers?: Record<string, string>;
45
48
  timeout?: number;
46
49
  constructor(opts?: QueryOpts);
47
- get<T, S>(params: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
48
- post<T, S>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
50
+ get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>>;
51
+ post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
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;
@@ -261,5 +264,39 @@ class Query {
261
264
  this.afterResponse = fn;
262
265
  }
263
266
  }
267
+ /**
268
+ * 前端调用后端QueryRouter
269
+ */
270
+ class QueryClient extends Query {
271
+ tokenName;
272
+ storage;
273
+ token;
274
+ qws;
275
+ constructor(opts) {
276
+ super(opts);
277
+ this.tokenName = opts?.tokenName || 'token';
278
+ this.storage = opts?.storage || localStorage;
279
+ this.beforeRequest = async (opts) => {
280
+ const token = this.token || this.getToken();
281
+ if (token) {
282
+ opts.headers = {
283
+ ...opts.headers,
284
+ Authorization: `Bearer ${token}`,
285
+ };
286
+ }
287
+ return opts;
288
+ };
289
+ this.qws = new QueryWs({ url: opts?.url });
290
+ }
291
+ getToken() {
292
+ return this.storage.getItem(this.tokenName);
293
+ }
294
+ saveToken(token) {
295
+ this.storage.setItem(this.tokenName, token);
296
+ }
297
+ removeToken() {
298
+ this.storage.removeItem(this.tokenName);
299
+ }
300
+ }
264
301
 
265
- export { Query, QueryWs, adapter };
302
+ 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.3",
3
+ "version": "0.0.5",
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",