@kevisual/query 0.0.7-alpha.1 → 0.0.7-alpha.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.
@@ -0,0 +1,125 @@
1
+ import { StoreApi } from 'zustand/vanilla';
2
+
3
+ type AdapterOpts = {
4
+ url: string;
5
+ headers?: Record<string, string>;
6
+ body?: Record<string, any>;
7
+ timeout?: number;
8
+ };
9
+ declare const adapter: (opts: AdapterOpts) => Promise<any>;
10
+
11
+ type QueryWsStore = {
12
+ connected: boolean;
13
+ status: 'connecting' | 'connected' | 'disconnected';
14
+ setConnected: (connected: boolean) => void;
15
+ setStatus: (status: QuerySelectState) => void;
16
+ };
17
+ type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
18
+ type QueryWsOpts = {
19
+ url?: string;
20
+ store?: StoreApi<QueryWsStore>;
21
+ ws?: WebSocket;
22
+ };
23
+ declare class QueryWs {
24
+ url: string;
25
+ store: StoreApi<QueryWsStore>;
26
+ ws: WebSocket;
27
+ constructor(opts?: QueryWsOpts);
28
+ /**
29
+ * 连接 WebSocket
30
+ */
31
+ connect(opts?: {
32
+ timeout?: number;
33
+ }): Promise<unknown>;
34
+ listenConnect(callback: () => void): () => void;
35
+ onMessage<T = any, U = any>(fn: (data: U, event: MessageEvent) => void, opts?: {
36
+ isJson?: boolean;
37
+ selector?: (data: T) => U;
38
+ }): () => void;
39
+ close(): void;
40
+ send<T = any, U = any>(data: T, opts?: {
41
+ isJson?: boolean;
42
+ wrapper?: (data: T) => U;
43
+ }): void;
44
+ getOpen(): boolean;
45
+ }
46
+
47
+ type Fn = (opts: {
48
+ url?: string;
49
+ headers?: Record<string, string>;
50
+ body?: Record<string, any>;
51
+ [key: string]: any;
52
+ timeout?: number;
53
+ }) => Promise<Record<string, any>>;
54
+ type QueryOpts$1 = {
55
+ url?: string;
56
+ adapter?: typeof adapter;
57
+ headers?: Record<string, string>;
58
+ timeout?: number;
59
+ };
60
+ type Data = {
61
+ path?: string;
62
+ key?: string;
63
+ payload?: Record<string, any>;
64
+ [key: string]: any;
65
+ };
66
+ type Result<S = any> = {
67
+ code: number;
68
+ data?: S;
69
+ message?: string;
70
+ success: boolean;
71
+ };
72
+ type DataOpts = Partial<QueryOpts$1> & {
73
+ beforeRequest?: Fn;
74
+ afterResponse?: (result: Result) => Promise<any>;
75
+ };
76
+ /**
77
+ * const query = new Query();
78
+ * const res = await query.post({
79
+ * path: 'demo',
80
+ * key: '1',
81
+ * });
82
+ *
83
+ * U是参数 V是返回值
84
+ */
85
+ declare class Query<U = any, V = any> {
86
+ adapter: typeof adapter;
87
+ url: string;
88
+ beforeRequest?: Fn;
89
+ afterResponse?: (result: Result) => Promise<any>;
90
+ headers?: Record<string, string>;
91
+ timeout?: number;
92
+ constructor(opts?: QueryOpts$1);
93
+ get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>>;
94
+ post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
95
+ before(fn: Fn): void;
96
+ after(fn: (result: Result) => Promise<any>): void;
97
+ }
98
+
99
+ type QueryOpts = {
100
+ url?: string;
101
+ adapter?: typeof adapter;
102
+ headers?: Record<string, string>;
103
+ timeout?: number;
104
+ };
105
+ /**
106
+ * 前端调用后端QueryRouter
107
+ */
108
+ declare class QueryClient<U = any, V = any> extends Query<U, V> {
109
+ tokenName: string;
110
+ storage: Storage;
111
+ token: string;
112
+ qws: QueryWs;
113
+ constructor(opts?: QueryOpts & {
114
+ tokenName?: string;
115
+ storage?: Storage;
116
+ io?: boolean;
117
+ });
118
+ createWs(opts?: QueryWsOpts): void;
119
+ getToken(): string;
120
+ saveToken(token: string): void;
121
+ removeToken(): void;
122
+ }
123
+ declare const client: QueryClient<any, any>;
124
+
125
+ export { Query, QueryClient, type QueryOpts, QueryWs, type QueryWsOpts, adapter, client };
@@ -1,7 +1,7 @@
1
1
  const adapter = async (opts) => {
2
2
  const controller = new AbortController();
3
3
  const signal = controller.signal;
4
- const timeout = opts.timeout || 60000; // 默认超时时间为 60s
4
+ const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
5
5
  const timer = setTimeout(() => {
6
6
  controller.abort();
7
7
  }, timeout);
@@ -244,7 +244,7 @@ class Query {
244
244
  this.headers = opts?.headers || {
245
245
  'Content-Type': 'application/json',
246
246
  };
247
- this.timeout = opts?.timeout || 60000; // 默认超时时间为 60s
247
+ this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
248
248
  }
249
249
  async get(params, options) {
250
250
  return this.post(params, options);
@@ -280,6 +280,7 @@ class Query {
280
280
  this.afterResponse = fn;
281
281
  }
282
282
  }
283
+
283
284
  /**
284
285
  * 前端调用后端QueryRouter
285
286
  */
@@ -307,7 +308,7 @@ class QueryClient extends Query {
307
308
  }
308
309
  }
309
310
  createWs(opts) {
310
- this.qws = new QueryWs({ url: this.url });
311
+ this.qws = new QueryWs({ url: this.url, ...opts });
311
312
  }
312
313
  getToken() {
313
314
  return this.storage.getItem(this.tokenName);
@@ -319,5 +320,6 @@ class QueryClient extends Query {
319
320
  this.storage.removeItem(this.tokenName);
320
321
  }
321
322
  }
323
+ const client = new QueryClient();
322
324
 
323
- export { Query, QueryClient, QueryWs, adapter };
325
+ export { Query, QueryClient, QueryWs, adapter, client };
@@ -1,26 +1,27 @@
1
1
  import { StoreApi } from 'zustand/vanilla';
2
+
2
3
  type QueryWsStore = {
3
4
  connected: boolean;
4
5
  status: 'connecting' | 'connected' | 'disconnected';
5
6
  setConnected: (connected: boolean) => void;
6
7
  setStatus: (status: QuerySelectState) => void;
7
8
  };
8
- export type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
9
- export type QueryWsStoreListener = (newState: QueryWsStore, oldState: QueryWsStore) => void;
10
- export type QueryWsOpts = {
9
+ type QuerySelectState = 'connecting' | 'connected' | 'disconnected';
10
+ type QueryWsStoreListener = (newState: QueryWsStore, oldState: QueryWsStore) => void;
11
+ type QueryWsOpts = {
11
12
  url?: string;
12
13
  store?: StoreApi<QueryWsStore>;
13
14
  ws?: WebSocket;
14
15
  };
15
- export type WsSend<T = any, U = any> = (data: T, opts?: {
16
+ type WsSend<T = any, U = any> = (data: T, opts?: {
16
17
  isJson?: boolean;
17
18
  wrapper?: (data: T) => U;
18
19
  }) => any;
19
- export type WsOnMessage<T = any, U = any> = (fn: (data: U, event: MessageEvent) => void, opts?: {
20
+ type WsOnMessage<T = any, U = any> = (fn: (data: U, event: MessageEvent) => void, opts?: {
20
21
  isJson?: boolean;
21
22
  selector?: (data: T) => U;
22
23
  }) => any;
23
- export declare class QueryWs {
24
+ declare class QueryWs {
24
25
  url: string;
25
26
  store: StoreApi<QueryWsStore>;
26
27
  ws: WebSocket;
@@ -43,4 +44,5 @@ export declare class QueryWs {
43
44
  }): void;
44
45
  getOpen(): boolean;
45
46
  }
46
- export {};
47
+
48
+ export { type QuerySelectState, QueryWs, type QueryWsOpts, type QueryWsStoreListener, type WsOnMessage, type WsSend };
@@ -1,6 +1,11 @@
1
- import { adapter } from './adapter.ts';
2
- import { QueryWs, QueryWsOpts } from './ws.ts';
3
- export { QueryOpts, QueryWs };
1
+ type AdapterOpts = {
2
+ url: string;
3
+ headers?: Record<string, string>;
4
+ body?: Record<string, any>;
5
+ timeout?: number;
6
+ };
7
+ declare const adapter: (opts: AdapterOpts) => Promise<any>;
8
+
4
9
  type Fn = (opts: {
5
10
  url?: string;
6
11
  headers?: Record<string, string>;
@@ -39,7 +44,7 @@ type DataOpts = Partial<QueryOpts> & {
39
44
  *
40
45
  * U是参数 V是返回值
41
46
  */
42
- export declare class Query<U = any, V = any> {
47
+ declare class Query<U = any, V = any> {
43
48
  adapter: typeof adapter;
44
49
  url: string;
45
50
  beforeRequest?: Fn;
@@ -52,22 +57,5 @@ export declare class Query<U = any, V = any> {
52
57
  before(fn: Fn): void;
53
58
  after(fn: (result: Result) => Promise<any>): void;
54
59
  }
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
- io?: boolean;
67
- });
68
- createWs(opts?: QueryWsOpts): void;
69
- getToken(): string;
70
- saveToken(token: string): void;
71
- removeToken(): void;
72
- }
73
- export { adapter };
60
+
61
+ export { Query, type QueryOpts, adapter };
package/dist/query.js ADDED
@@ -0,0 +1,101 @@
1
+ const adapter = async (opts) => {
2
+ const controller = new AbortController();
3
+ const signal = controller.signal;
4
+ const timeout = opts.timeout || 60000 * 3; // 默认超时时间为 60s * 3
5
+ const timer = setTimeout(() => {
6
+ controller.abort();
7
+ }, timeout);
8
+ return fetch(opts.url, {
9
+ method: 'POST',
10
+ headers: {
11
+ 'Content-Type': 'application/json',
12
+ ...opts.headers,
13
+ },
14
+ body: JSON.stringify(opts.body),
15
+ signal,
16
+ })
17
+ .then((response) => {
18
+ // 获取 Content-Type 头部信息
19
+ const contentType = response.headers.get('Content-Type');
20
+ // 判断返回的数据类型
21
+ if (contentType && contentType.includes('application/json')) {
22
+ return response.json(); // 解析为 JSON
23
+ }
24
+ else {
25
+ return response.text(); // 解析为文本
26
+ }
27
+ })
28
+ .catch((err) => {
29
+ if (err.name === 'AbortError') {
30
+ console.log('Request timed out and was aborted');
31
+ }
32
+ console.error(err);
33
+ return {
34
+ code: 500,
35
+ };
36
+ })
37
+ .finally(() => {
38
+ clearTimeout(timer);
39
+ });
40
+ };
41
+
42
+ /**
43
+ * const query = new Query();
44
+ * const res = await query.post({
45
+ * path: 'demo',
46
+ * key: '1',
47
+ * });
48
+ *
49
+ * U是参数 V是返回值
50
+ */
51
+ class Query {
52
+ adapter;
53
+ url;
54
+ beforeRequest;
55
+ afterResponse;
56
+ headers;
57
+ timeout;
58
+ constructor(opts) {
59
+ this.adapter = opts?.adapter || adapter;
60
+ this.url = opts?.url || '/api/router';
61
+ this.headers = opts?.headers || {
62
+ 'Content-Type': 'application/json',
63
+ };
64
+ this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
65
+ }
66
+ async get(params, options) {
67
+ return this.post(params, options);
68
+ }
69
+ async post(body, options) {
70
+ const url = options?.url || this.url;
71
+ const headers = { ...this.headers, ...options?.headers };
72
+ const adapter = options?.adapter || this.adapter;
73
+ const beforeRequest = options?.beforeRequest || this.beforeRequest;
74
+ const afterResponse = options?.afterResponse || this.afterResponse;
75
+ const timeout = options?.timeout || this.timeout;
76
+ const req = {
77
+ url: url,
78
+ headers: headers,
79
+ body,
80
+ timeout,
81
+ };
82
+ if (beforeRequest) {
83
+ await beforeRequest(req);
84
+ }
85
+ return adapter(req).then(async (res) => {
86
+ res.success = res.code === 200;
87
+ if (afterResponse) {
88
+ return await afterResponse(res);
89
+ }
90
+ return res;
91
+ });
92
+ }
93
+ before(fn) {
94
+ this.beforeRequest = fn;
95
+ }
96
+ after(fn) {
97
+ this.afterResponse = fn;
98
+ }
99
+ }
100
+
101
+ export { Query, adapter };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.7-alpha.1",
3
+ "version": "0.0.7-alpha.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,7 +8,7 @@
8
8
  "type": "module",
9
9
  "scripts": {
10
10
  "build": "npm run clean && rollup -c",
11
- "test": "NODE_ENV=development node --experimental-vm-modules node_modules/jest/bin/jest.js --detectOpenHandles",
11
+ "build:app": "npm run build && rsync dist/* ../deploy/dist",
12
12
  "clean": "rm -rf dist"
13
13
  },
14
14
  "files": [
@@ -27,8 +27,9 @@
27
27
  "rollup": "^4.21.2",
28
28
  "ts-node": "^10.9.2",
29
29
  "tslib": "^2.7.0",
30
+ "typescript": "^5.5.4",
30
31
  "zustand": "^4.5.5",
31
- "typescript": "^5.5.4"
32
+ "rollup-plugin-dts": "^6.1.1"
32
33
  },
33
34
  "packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447",
34
35
  "publishConfig": {
@@ -40,16 +41,17 @@
40
41
  },
41
42
  "exports": {
42
43
  ".": {
43
- "import": "./dist/index.js",
44
- "require": "./dist/index.js"
44
+ "import": "./dist/query-browser.js",
45
+ "require": "./dist/query-browser.js"
45
46
  },
46
- "./node": {
47
- "import": "./dist/node-adapter.js",
48
- "require": "./dist/node-adapter.js"
47
+ "./query": {
48
+ "import": "./dist/query.js",
49
+ "require": "./dist/query.js"
49
50
  },
50
51
  "./ws": {
51
- "import": "./dist/ws.js",
52
- "require": "./dist/ws.js"
52
+ "import": "./dist/query-ws.js",
53
+ "require": "./dist/query-ws.js"
53
54
  }
54
- }
55
+ },
56
+ "dependencies": {}
55
57
  }
package/readme.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  主要目的:请求路径默认`/api/router`,使用`post`,`post`的数据分流使用`path`和`key`.
6
6
 
7
+ 适配后端的项目 [@kevisual/router](https://git.xiongxiao.me/kevisual/router)
8
+
7
9
  ## query
8
10
 
9
11
 
package/dist/adapter.d.ts DELETED
@@ -1,8 +0,0 @@
1
- type AdapterOpts = {
2
- url: string;
3
- headers?: Record<string, string>;
4
- body?: Record<string, any>;
5
- timeout?: number;
6
- };
7
- export declare const adapter: (opts: AdapterOpts) => Promise<any>;
8
- export {};
package/dist/utils.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export declare const parseUrl: (url: string) => string;
2
- export declare const parseWsUrl: (url: string) => string;
File without changes