@kevisual/query 0.0.11 → 0.0.13

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.
@@ -93,7 +93,11 @@ type Result<S = any> = {
93
93
  };
94
94
  type DataOpts = Partial<QueryOpts$1> & {
95
95
  beforeRequest?: Fn;
96
- afterResponse?: <S, U = S>(result: Result<S>) => Promise<U>;
96
+ afterResponse?: <S, U = S>(result: Result<S>, ctx?: {
97
+ req?: any;
98
+ res?: any;
99
+ fetch?: any;
100
+ }) => Promise<U>;
97
101
  };
98
102
  /**
99
103
  * const query = new Query();
@@ -108,10 +112,18 @@ declare class Query {
108
112
  adapter: typeof adapter;
109
113
  url: string;
110
114
  beforeRequest?: Fn;
111
- afterResponse?: (result: Result) => Promise<any>;
115
+ afterResponse?: DataOpts['afterResponse'];
112
116
  headers?: Record<string, string>;
113
117
  timeout?: number;
118
+ /**
119
+ * 需要突然停止请求,比如401的时候
120
+ */
121
+ stop?: boolean;
114
122
  constructor(opts?: QueryOpts$1);
123
+ /**
124
+ * 突然停止请求
125
+ */
126
+ setStop(stop: boolean): void;
115
127
  /**
116
128
  * 发送 get 请求,转到 post 请求
117
129
  * T是请求类型自定义
@@ -139,7 +151,7 @@ declare class Query {
139
151
  * 请求后处理,设置请求后处理函数
140
152
  * @param fn 处理函数
141
153
  */
142
- after(fn: (result: Result) => Promise<any>): void;
154
+ after(fn: (result: Result, req?: any) => Promise<any>): void;
143
155
  }
144
156
 
145
157
  type QueryOpts = {
@@ -221,6 +221,24 @@ class QueryWs {
221
221
  }
222
222
  }
223
223
 
224
+ /**
225
+ * 设置基础响应, 设置 success 和 showError,
226
+ * success 是 code 是否等于 200
227
+ * showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
228
+ * @param res 响应
229
+ */
230
+ const setBaseResponse = (res) => {
231
+ res.success = res.code === 200;
232
+ /**
233
+ * 显示错误
234
+ * @param fn 错误处理函数
235
+ */
236
+ res.showError = (fn) => {
237
+ if (!res.success && !res.noMsg) {
238
+ fn?.();
239
+ }
240
+ };
241
+ };
224
242
  /**
225
243
  * const query = new Query();
226
244
  * const res = await query.post({
@@ -237,6 +255,10 @@ class Query {
237
255
  afterResponse;
238
256
  headers;
239
257
  timeout;
258
+ /**
259
+ * 需要突然停止请求,比如401的时候
260
+ */
261
+ stop;
240
262
  constructor(opts) {
241
263
  this.adapter = opts?.adapter || adapter;
242
264
  this.url = opts?.url || '/api/router';
@@ -245,6 +267,12 @@ class Query {
245
267
  };
246
268
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
247
269
  }
270
+ /**
271
+ * 突然停止请求
272
+ */
273
+ setStop(stop) {
274
+ this.stop = stop;
275
+ }
248
276
  /**
249
277
  * 发送 get 请求,转到 post 请求
250
278
  * T是请求类型自定义
@@ -291,21 +319,32 @@ class Query {
291
319
  showError: () => { },
292
320
  };
293
321
  }
322
+ if (this.stop) {
323
+ const that = this;
324
+ await new Promise((resolve) => {
325
+ let timer = 0;
326
+ const detect = setInterval(() => {
327
+ if (!that.stop) {
328
+ clearInterval(detect);
329
+ resolve(true);
330
+ }
331
+ timer++;
332
+ if (timer > 30) {
333
+ console.error('request stop: timeout', req.url, timer);
334
+ }
335
+ }, 1000);
336
+ });
337
+ }
294
338
  return adapter(req).then(async (res) => {
295
339
  try {
296
- res.success = res.code === 200;
340
+ setBaseResponse(res);
297
341
  if (afterResponse) {
298
- return await afterResponse(res);
342
+ return await afterResponse(res, {
343
+ req,
344
+ res,
345
+ fetch: adapter,
346
+ });
299
347
  }
300
- /**
301
- * 显示错误
302
- * @param fn 错误处理函数
303
- */
304
- res.showError = (fn) => {
305
- if (!res.success && !res.noResult) {
306
- fn?.();
307
- }
308
- };
309
348
  return res;
310
349
  }
311
350
  catch (e) {
package/dist/query.d.ts CHANGED
@@ -55,8 +55,19 @@ type Result<S = any> = {
55
55
  };
56
56
  type DataOpts = Partial<QueryOpts> & {
57
57
  beforeRequest?: Fn;
58
- afterResponse?: <S, U = S>(result: Result<S>) => Promise<U>;
58
+ afterResponse?: <S, U = S>(result: Result<S>, ctx?: {
59
+ req?: any;
60
+ res?: any;
61
+ fetch?: any;
62
+ }) => Promise<U>;
59
63
  };
64
+ /**
65
+ * 设置基础响应, 设置 success 和 showError,
66
+ * success 是 code 是否等于 200
67
+ * showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
68
+ * @param res 响应
69
+ */
70
+ declare const setBaseResponse: (res: Result) => void;
60
71
  /**
61
72
  * const query = new Query();
62
73
  * const res = await query.post({
@@ -70,10 +81,18 @@ declare class Query {
70
81
  adapter: typeof adapter;
71
82
  url: string;
72
83
  beforeRequest?: Fn;
73
- afterResponse?: (result: Result) => Promise<any>;
84
+ afterResponse?: DataOpts['afterResponse'];
74
85
  headers?: Record<string, string>;
75
86
  timeout?: number;
87
+ /**
88
+ * 需要突然停止请求,比如401的时候
89
+ */
90
+ stop?: boolean;
76
91
  constructor(opts?: QueryOpts);
92
+ /**
93
+ * 突然停止请求
94
+ */
95
+ setStop(stop: boolean): void;
77
96
  /**
78
97
  * 发送 get 请求,转到 post 请求
79
98
  * T是请求类型自定义
@@ -101,8 +120,8 @@ declare class Query {
101
120
  * 请求后处理,设置请求后处理函数
102
121
  * @param fn 处理函数
103
122
  */
104
- after(fn: (result: Result) => Promise<any>): void;
123
+ after(fn: (result: Result, req?: any) => Promise<any>): void;
105
124
  }
106
125
 
107
- export { Query, adapter };
108
- export type { Data, QueryOpts, Result };
126
+ export { Query, adapter, setBaseResponse };
127
+ export type { Data, DataOpts, Fn, QueryOpts, Result };
package/dist/query.js CHANGED
@@ -46,6 +46,24 @@ const adapter = async (opts, overloadOpts) => {
46
46
  });
47
47
  };
48
48
 
49
+ /**
50
+ * 设置基础响应, 设置 success 和 showError,
51
+ * success 是 code 是否等于 200
52
+ * showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
53
+ * @param res 响应
54
+ */
55
+ const setBaseResponse = (res) => {
56
+ res.success = res.code === 200;
57
+ /**
58
+ * 显示错误
59
+ * @param fn 错误处理函数
60
+ */
61
+ res.showError = (fn) => {
62
+ if (!res.success && !res.noMsg) {
63
+ fn?.();
64
+ }
65
+ };
66
+ };
49
67
  /**
50
68
  * const query = new Query();
51
69
  * const res = await query.post({
@@ -62,6 +80,10 @@ class Query {
62
80
  afterResponse;
63
81
  headers;
64
82
  timeout;
83
+ /**
84
+ * 需要突然停止请求,比如401的时候
85
+ */
86
+ stop;
65
87
  constructor(opts) {
66
88
  this.adapter = opts?.adapter || adapter;
67
89
  this.url = opts?.url || '/api/router';
@@ -70,6 +92,12 @@ class Query {
70
92
  };
71
93
  this.timeout = opts?.timeout || 60000 * 3; // 默认超时时间为 60s * 3
72
94
  }
95
+ /**
96
+ * 突然停止请求
97
+ */
98
+ setStop(stop) {
99
+ this.stop = stop;
100
+ }
73
101
  /**
74
102
  * 发送 get 请求,转到 post 请求
75
103
  * T是请求类型自定义
@@ -116,21 +144,32 @@ class Query {
116
144
  showError: () => { },
117
145
  };
118
146
  }
147
+ if (this.stop) {
148
+ const that = this;
149
+ await new Promise((resolve) => {
150
+ let timer = 0;
151
+ const detect = setInterval(() => {
152
+ if (!that.stop) {
153
+ clearInterval(detect);
154
+ resolve(true);
155
+ }
156
+ timer++;
157
+ if (timer > 30) {
158
+ console.error('request stop: timeout', req.url, timer);
159
+ }
160
+ }, 1000);
161
+ });
162
+ }
119
163
  return adapter(req).then(async (res) => {
120
164
  try {
121
- res.success = res.code === 200;
165
+ setBaseResponse(res);
122
166
  if (afterResponse) {
123
- return await afterResponse(res);
167
+ return await afterResponse(res, {
168
+ req,
169
+ res,
170
+ fetch: adapter,
171
+ });
124
172
  }
125
- /**
126
- * 显示错误
127
- * @param fn 错误处理函数
128
- */
129
- res.showError = (fn) => {
130
- if (!res.success && !res.noResult) {
131
- fn?.();
132
- }
133
- };
134
173
  return res;
135
174
  }
136
175
  catch (e) {
@@ -160,4 +199,4 @@ class Query {
160
199
  }
161
200
  }
162
201
 
163
- export { Query, adapter };
202
+ export { Query, adapter, setBaseResponse };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/query",
3
- "version": "0.0.11",
3
+ "version": "0.0.13",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",