@kevisual/query 0.0.9-alpha.2 → 0.0.10

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.
@@ -50,6 +50,11 @@ declare class QueryWs {
50
50
  getOpen(): boolean;
51
51
  }
52
52
 
53
+ /**
54
+ * 请求前处理函数
55
+ * @param opts 请求配置
56
+ * @returns 请求配置
57
+ */
53
58
  type Fn = (opts: {
54
59
  url?: string;
55
60
  headers?: Record<string, string>;
@@ -74,10 +79,21 @@ type Result<S = any> = {
74
79
  data?: S;
75
80
  message?: string;
76
81
  success: boolean;
82
+ /**
83
+ * 是否不返回 message
84
+ */
85
+ noMsg?: boolean;
86
+ /**
87
+ * 显示错误, 当 handle的信息被处理的时候,如果不是success,同时自己设置了noMsg,那么就不显示错误信息了,因为被处理。
88
+ *
89
+ * 日常: fetch().then(res=>if(res.sucess){message.error('error')})的时候,比如401被处理过了,就不再提示401错误了。
90
+ *
91
+ */
92
+ showError: (fn?: () => void) => void;
77
93
  };
78
94
  type DataOpts = Partial<QueryOpts$1> & {
79
95
  beforeRequest?: Fn;
80
- afterResponse?: (result: Result) => Promise<any>;
96
+ afterResponse?: <S, U = S>(result: Result<S>) => Promise<U>;
81
97
  };
82
98
  /**
83
99
  * const query = new Query();
@@ -88,7 +104,7 @@ type DataOpts = Partial<QueryOpts$1> & {
88
104
  *
89
105
  * U是参数 V是返回值
90
106
  */
91
- declare class Query<U = any, V = any> {
107
+ declare class Query {
92
108
  adapter: typeof adapter;
93
109
  url: string;
94
110
  beforeRequest?: Fn;
@@ -96,9 +112,33 @@ declare class Query<U = any, V = any> {
96
112
  headers?: Record<string, string>;
97
113
  timeout?: number;
98
114
  constructor(opts?: QueryOpts$1);
99
- get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>>;
100
- post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
115
+ /**
116
+ * 发送 get 请求,转到 post 请求
117
+ * T是请求类型自定义
118
+ * S是返回类型自定义
119
+ * @param params 请求参数
120
+ * @param options 请求配置
121
+ * @returns 请求结果
122
+ */
123
+ get<R = any, P = any>(params: Data & P, options?: DataOpts): Promise<Result<R>>;
124
+ /**
125
+ * 发送 post 请求
126
+ * T是请求类型自定义
127
+ * S是返回类型自定义
128
+ * @param body 请求体
129
+ * @param options 请求配置
130
+ * @returns 请求结果
131
+ */
132
+ post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
133
+ /**
134
+ * 请求前处理,设置请求前处理函数
135
+ * @param fn 处理函数
136
+ */
101
137
  before(fn: Fn): void;
138
+ /**
139
+ * 请求后处理,设置请求后处理函数
140
+ * @param fn 处理函数
141
+ */
102
142
  after(fn: (result: Result) => Promise<any>): void;
103
143
  }
104
144
 
@@ -111,7 +151,7 @@ type QueryOpts = {
111
151
  /**
112
152
  * 前端调用后端QueryRouter
113
153
  */
114
- declare class QueryClient<U = any, V = any> extends Query<U, V> {
154
+ declare class QueryClient extends Query {
115
155
  tokenName: string;
116
156
  storage: Storage;
117
157
  token: string;
@@ -127,4 +167,5 @@ declare class QueryClient<U = any, V = any> extends Query<U, V> {
127
167
  removeToken(): void;
128
168
  }
129
169
 
130
- export { Query, QueryClient, type QueryOpts, QueryWs, type QueryWsOpts, adapter };
170
+ export { Query, QueryClient, QueryWs, adapter };
171
+ export type { QueryOpts, QueryWsOpts };
@@ -245,9 +245,25 @@ class Query {
245
245
  };
246
246
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
247
247
  }
248
+ /**
249
+ * 发送 get 请求,转到 post 请求
250
+ * T是请求类型自定义
251
+ * S是返回类型自定义
252
+ * @param params 请求参数
253
+ * @param options 请求配置
254
+ * @returns 请求结果
255
+ */
248
256
  async get(params, options) {
249
257
  return this.post(params, options);
250
258
  }
259
+ /**
260
+ * 发送 post 请求
261
+ * T是请求类型自定义
262
+ * S是返回类型自定义
263
+ * @param body 请求体
264
+ * @param options 请求配置
265
+ * @returns 请求结果
266
+ */
251
267
  async post(body, options) {
252
268
  const url = options?.url || this.url;
253
269
  const headers = { ...this.headers, ...options?.headers };
@@ -267,11 +283,12 @@ class Query {
267
283
  }
268
284
  }
269
285
  catch (e) {
270
- console.error(e);
286
+ console.error('request beforeFn error', e, req);
271
287
  return {
272
288
  code: 500,
273
289
  success: false,
274
290
  message: 'api request beforeFn error',
291
+ showError: () => { },
275
292
  };
276
293
  }
277
294
  return adapter(req).then(async (res) => {
@@ -280,21 +297,39 @@ class Query {
280
297
  if (afterResponse) {
281
298
  return await afterResponse(res);
282
299
  }
300
+ /**
301
+ * 显示错误
302
+ * @param fn 错误处理函数
303
+ */
304
+ res.showError = (fn) => {
305
+ if (!res.success && !res.noResult) {
306
+ fn?.();
307
+ }
308
+ };
283
309
  return res;
284
310
  }
285
311
  catch (e) {
286
- console.error(e);
312
+ console.error('request error', e, req);
287
313
  return {
288
314
  code: 500,
289
315
  success: false,
290
316
  message: 'api request afterFn error',
317
+ showError: () => { },
291
318
  };
292
319
  }
293
320
  });
294
321
  }
322
+ /**
323
+ * 请求前处理,设置请求前处理函数
324
+ * @param fn 处理函数
325
+ */
295
326
  before(fn) {
296
327
  this.beforeRequest = fn;
297
328
  }
329
+ /**
330
+ * 请求后处理,设置请求后处理函数
331
+ * @param fn 处理函数
332
+ */
298
333
  after(fn) {
299
334
  this.afterResponse = fn;
300
335
  }
@@ -45,4 +45,5 @@ declare class QueryWs {
45
45
  getOpen(): boolean;
46
46
  }
47
47
 
48
- export { type QuerySelectState, QueryWs, type QueryWsOpts, type QueryWsStoreListener, type WsOnMessage, type WsSend };
48
+ export { QueryWs };
49
+ export type { QuerySelectState, QueryWsOpts, QueryWsStoreListener, WsOnMessage, WsSend };
package/dist/query.d.ts CHANGED
@@ -12,6 +12,11 @@ type AdapterOpts = {
12
12
  */
13
13
  declare const adapter: (opts: AdapterOpts, overloadOpts?: RequestInit) => Promise<any>;
14
14
 
15
+ /**
16
+ * 请求前处理函数
17
+ * @param opts 请求配置
18
+ * @returns 请求配置
19
+ */
15
20
  type Fn = (opts: {
16
21
  url?: string;
17
22
  headers?: Record<string, string>;
@@ -36,10 +41,21 @@ type Result<S = any> = {
36
41
  data?: S;
37
42
  message?: string;
38
43
  success: boolean;
44
+ /**
45
+ * 是否不返回 message
46
+ */
47
+ noMsg?: boolean;
48
+ /**
49
+ * 显示错误, 当 handle的信息被处理的时候,如果不是success,同时自己设置了noMsg,那么就不显示错误信息了,因为被处理。
50
+ *
51
+ * 日常: fetch().then(res=>if(res.sucess){message.error('error')})的时候,比如401被处理过了,就不再提示401错误了。
52
+ *
53
+ */
54
+ showError: (fn?: () => void) => void;
39
55
  };
40
56
  type DataOpts = Partial<QueryOpts> & {
41
57
  beforeRequest?: Fn;
42
- afterResponse?: (result: Result) => Promise<any>;
58
+ afterResponse?: <S, U = S>(result: Result<S>) => Promise<U>;
43
59
  };
44
60
  /**
45
61
  * const query = new Query();
@@ -50,7 +66,7 @@ type DataOpts = Partial<QueryOpts> & {
50
66
  *
51
67
  * U是参数 V是返回值
52
68
  */
53
- declare class Query<U = any, V = any> {
69
+ declare class Query {
54
70
  adapter: typeof adapter;
55
71
  url: string;
56
72
  beforeRequest?: Fn;
@@ -58,10 +74,35 @@ declare class Query<U = any, V = any> {
58
74
  headers?: Record<string, string>;
59
75
  timeout?: number;
60
76
  constructor(opts?: QueryOpts);
61
- get<T = any, S = any>(params: Record<string, any> & Data & U & T, options?: DataOpts): Promise<Result<V & S>>;
62
- post<T = any, S = any>(body: Record<string, any> & Data & T, options?: DataOpts): Promise<Result<S>>;
77
+ /**
78
+ * 发送 get 请求,转到 post 请求
79
+ * T是请求类型自定义
80
+ * S是返回类型自定义
81
+ * @param params 请求参数
82
+ * @param options 请求配置
83
+ * @returns 请求结果
84
+ */
85
+ get<R = any, P = any>(params: Data & P, options?: DataOpts): Promise<Result<R>>;
86
+ /**
87
+ * 发送 post 请求
88
+ * T是请求类型自定义
89
+ * S是返回类型自定义
90
+ * @param body 请求体
91
+ * @param options 请求配置
92
+ * @returns 请求结果
93
+ */
94
+ post<R = any, P = any>(body: Data & P, options?: DataOpts): Promise<Result<R>>;
95
+ /**
96
+ * 请求前处理,设置请求前处理函数
97
+ * @param fn 处理函数
98
+ */
63
99
  before(fn: Fn): void;
100
+ /**
101
+ * 请求后处理,设置请求后处理函数
102
+ * @param fn 处理函数
103
+ */
64
104
  after(fn: (result: Result) => Promise<any>): void;
65
105
  }
66
106
 
67
- export { Query, type QueryOpts, adapter };
107
+ export { Query, adapter };
108
+ export type { QueryOpts };
package/dist/query.js CHANGED
@@ -70,9 +70,25 @@ class Query {
70
70
  };
71
71
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
72
72
  }
73
+ /**
74
+ * 发送 get 请求,转到 post 请求
75
+ * T是请求类型自定义
76
+ * S是返回类型自定义
77
+ * @param params 请求参数
78
+ * @param options 请求配置
79
+ * @returns 请求结果
80
+ */
73
81
  async get(params, options) {
74
82
  return this.post(params, options);
75
83
  }
84
+ /**
85
+ * 发送 post 请求
86
+ * T是请求类型自定义
87
+ * S是返回类型自定义
88
+ * @param body 请求体
89
+ * @param options 请求配置
90
+ * @returns 请求结果
91
+ */
76
92
  async post(body, options) {
77
93
  const url = options?.url || this.url;
78
94
  const headers = { ...this.headers, ...options?.headers };
@@ -92,11 +108,12 @@ class Query {
92
108
  }
93
109
  }
94
110
  catch (e) {
95
- console.error(e);
111
+ console.error('request beforeFn error', e, req);
96
112
  return {
97
113
  code: 500,
98
114
  success: false,
99
115
  message: 'api request beforeFn error',
116
+ showError: () => { },
100
117
  };
101
118
  }
102
119
  return adapter(req).then(async (res) => {
@@ -105,21 +122,39 @@ class Query {
105
122
  if (afterResponse) {
106
123
  return await afterResponse(res);
107
124
  }
125
+ /**
126
+ * 显示错误
127
+ * @param fn 错误处理函数
128
+ */
129
+ res.showError = (fn) => {
130
+ if (!res.success && !res.noResult) {
131
+ fn?.();
132
+ }
133
+ };
108
134
  return res;
109
135
  }
110
136
  catch (e) {
111
- console.error(e);
137
+ console.error('request error', e, req);
112
138
  return {
113
139
  code: 500,
114
140
  success: false,
115
141
  message: 'api request afterFn error',
142
+ showError: () => { },
116
143
  };
117
144
  }
118
145
  });
119
146
  }
147
+ /**
148
+ * 请求前处理,设置请求前处理函数
149
+ * @param fn 处理函数
150
+ */
120
151
  before(fn) {
121
152
  this.beforeRequest = fn;
122
153
  }
154
+ /**
155
+ * 请求后处理,设置请求后处理函数
156
+ * @param fn 处理函数
157
+ */
123
158
  after(fn) {
124
159
  this.afterResponse = fn;
125
160
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.9-alpha.2",
3
+ "version": "0.0.10",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -22,10 +22,10 @@
22
22
  "license": "ISC",
23
23
  "description": "",
24
24
  "devDependencies": {
25
- "@rollup/plugin-node-resolve": "^16.0.0",
25
+ "@rollup/plugin-node-resolve": "^16.0.1",
26
26
  "@rollup/plugin-typescript": "^12.1.2",
27
- "rollup": "^4.34.9",
28
- "rollup-plugin-dts": "^6.1.1",
27
+ "rollup": "^4.36.0",
28
+ "rollup-plugin-dts": "^6.2.0",
29
29
  "ts-node": "^10.9.2",
30
30
  "tslib": "^2.8.1",
31
31
  "typescript": "^5.8.2",
@@ -58,6 +58,6 @@
58
58
  }
59
59
  },
60
60
  "dependencies": {
61
- "openai": "^4.86.1"
61
+ "openai": "^4.88.0"
62
62
  }
63
63
  }