@kevisual/query 0.0.31 → 0.0.33
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/query-adapter.js +1 -1
- package/dist/query-browser.d.ts +19 -17
- package/dist/query-browser.js +16 -23
- package/dist/query.d.ts +22 -17
- package/dist/query.js +16 -4
- package/package.json +2 -6
package/dist/query-adapter.js
CHANGED
package/dist/query-browser.d.ts
CHANGED
|
@@ -112,29 +112,24 @@ type QueryOpts$1 = {
|
|
|
112
112
|
adapter?: typeof adapter;
|
|
113
113
|
[key: string]: any;
|
|
114
114
|
} & AdapterOpts;
|
|
115
|
+
type QueryOptions = {
|
|
116
|
+
url?: string;
|
|
117
|
+
adapter?: typeof adapter;
|
|
118
|
+
headers?: Record<string, string>;
|
|
119
|
+
timeout?: number;
|
|
120
|
+
isClient?: boolean;
|
|
121
|
+
};
|
|
115
122
|
type Data = {
|
|
116
123
|
path?: string;
|
|
117
124
|
key?: string;
|
|
118
125
|
payload?: Record<string, any>;
|
|
119
126
|
[key: string]: any;
|
|
120
127
|
};
|
|
121
|
-
type Result<S = any> = {
|
|
128
|
+
type Result<S = any, U = {}> = {
|
|
122
129
|
code: number;
|
|
123
130
|
data?: S;
|
|
124
131
|
message?: string;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 是否不返回 message
|
|
128
|
-
*/
|
|
129
|
-
noMsg?: boolean;
|
|
130
|
-
/**
|
|
131
|
-
* 显示错误, 当 handle的信息被处理的时候,如果不是success,同时自己设置了noMsg,那么就不显示错误信息了,因为被处理。
|
|
132
|
-
*
|
|
133
|
-
* 日常: fetch().then(res=>if(res.sucess){message.error('error')})的时候,比如401被处理过了,就不再提示401错误了。
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
showError: (fn?: () => void) => void;
|
|
137
|
-
};
|
|
132
|
+
} & U;
|
|
138
133
|
type DataOpts = Partial<QueryOpts$1> & {
|
|
139
134
|
beforeRequest?: Fn;
|
|
140
135
|
afterResponse?: <S = any>(result: Result<S>, ctx?: {
|
|
@@ -184,7 +179,8 @@ declare class Query {
|
|
|
184
179
|
*/
|
|
185
180
|
stop?: boolean;
|
|
186
181
|
qws: QueryWs;
|
|
187
|
-
|
|
182
|
+
isClient: boolean;
|
|
183
|
+
constructor(opts?: QueryOptions);
|
|
188
184
|
setQueryWs(qws: QueryWs): void;
|
|
189
185
|
/**
|
|
190
186
|
* 突然停止请求
|
|
@@ -221,6 +217,7 @@ declare class Query {
|
|
|
221
217
|
fetchText(urlOrOptions?: string | QueryOpts$1, options?: QueryOpts$1): Promise<Result<any>>;
|
|
222
218
|
}
|
|
223
219
|
|
|
220
|
+
/** @deprecated */
|
|
224
221
|
declare class BaseQuery<T extends Query = Query, R extends {
|
|
225
222
|
queryChain?: any;
|
|
226
223
|
query?: any;
|
|
@@ -239,6 +236,10 @@ declare class BaseQuery<T extends Query = Query, R extends {
|
|
|
239
236
|
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
240
237
|
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
241
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* @deprecated
|
|
241
|
+
* 前端调用后端QueryRouter, 默认路径 /client/router
|
|
242
|
+
*/
|
|
242
243
|
declare class ClientQuery extends Query {
|
|
243
244
|
constructor(opts?: QueryOpts$1);
|
|
244
245
|
}
|
|
@@ -248,6 +249,7 @@ type QueryOpts = {
|
|
|
248
249
|
adapter?: typeof adapter;
|
|
249
250
|
headers?: Record<string, string>;
|
|
250
251
|
timeout?: number;
|
|
252
|
+
isClient?: boolean;
|
|
251
253
|
};
|
|
252
254
|
/**
|
|
253
255
|
* 前端调用后端QueryRouter, 封装 beforeRequest 和 wss
|
|
@@ -256,7 +258,7 @@ declare class QueryClient extends Query {
|
|
|
256
258
|
tokenName: string;
|
|
257
259
|
storage: Storage;
|
|
258
260
|
token: string;
|
|
259
|
-
constructor(opts?:
|
|
261
|
+
constructor(opts?: QueryOptions & {
|
|
260
262
|
tokenName?: string;
|
|
261
263
|
storage?: Storage;
|
|
262
264
|
io?: boolean;
|
|
@@ -268,4 +270,4 @@ declare class QueryClient extends Query {
|
|
|
268
270
|
}
|
|
269
271
|
|
|
270
272
|
export { BaseQuery, ClientQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
|
|
271
|
-
export type { Data, DataOpts, QueryOpts, QueryWsOpts, Result };
|
|
273
|
+
export type { Data, DataOpts, QueryOptions, QueryOpts, QueryWsOpts, Result };
|
package/dist/query-browser.js
CHANGED
|
@@ -79,7 +79,7 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
79
79
|
}
|
|
80
80
|
else if (isTextForContentType(contentType)) {
|
|
81
81
|
return {
|
|
82
|
-
code:
|
|
82
|
+
code: response.status,
|
|
83
83
|
status: response.status,
|
|
84
84
|
data: await response.text(), // 直接返回文本内容
|
|
85
85
|
};
|
|
@@ -318,25 +318,6 @@ class QueryWs {
|
|
|
318
318
|
}
|
|
319
319
|
}
|
|
320
320
|
|
|
321
|
-
/**
|
|
322
|
-
* 设置基础响应, 设置 success 和 showError,
|
|
323
|
-
* success 是 code 是否等于 200
|
|
324
|
-
* showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
|
|
325
|
-
* @param res 响应
|
|
326
|
-
*/
|
|
327
|
-
const setBaseResponse = (res) => {
|
|
328
|
-
res.success = res.code === 200;
|
|
329
|
-
/**
|
|
330
|
-
* 显示错误
|
|
331
|
-
* @param fn 错误处理函数
|
|
332
|
-
*/
|
|
333
|
-
res.showError = (fn) => {
|
|
334
|
-
if (!res.success && !res.noMsg) {
|
|
335
|
-
fn?.();
|
|
336
|
-
}
|
|
337
|
-
};
|
|
338
|
-
return res;
|
|
339
|
-
};
|
|
340
321
|
const wrapperError = ({ code, message }) => {
|
|
341
322
|
const result = {
|
|
342
323
|
code: code || 500,
|
|
@@ -377,9 +358,11 @@ class Query {
|
|
|
377
358
|
stop;
|
|
378
359
|
// 默认不使用ws
|
|
379
360
|
qws;
|
|
361
|
+
isClient = false;
|
|
380
362
|
constructor(opts) {
|
|
381
363
|
this.adapter = opts?.adapter || adapter;
|
|
382
|
-
|
|
364
|
+
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
|
365
|
+
this.url = opts?.url || defaultURL;
|
|
383
366
|
this.headers = opts?.headers || {
|
|
384
367
|
'Content-Type': 'application/json',
|
|
385
368
|
};
|
|
@@ -465,7 +448,6 @@ class Query {
|
|
|
465
448
|
}
|
|
466
449
|
return _adapter(req).then(async (res) => {
|
|
467
450
|
try {
|
|
468
|
-
setBaseResponse(res);
|
|
469
451
|
if (_afterResponse) {
|
|
470
452
|
return await _afterResponse(res, {
|
|
471
453
|
req,
|
|
@@ -513,9 +495,16 @@ class Query {
|
|
|
513
495
|
...(_options?.headers || {}),
|
|
514
496
|
},
|
|
515
497
|
});
|
|
516
|
-
|
|
498
|
+
if (res && !res.code) {
|
|
499
|
+
return {
|
|
500
|
+
code: 200,
|
|
501
|
+
data: res,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
return res;
|
|
517
505
|
}
|
|
518
506
|
}
|
|
507
|
+
/** @deprecated */
|
|
519
508
|
class BaseQuery {
|
|
520
509
|
query;
|
|
521
510
|
queryDefine;
|
|
@@ -541,6 +530,10 @@ class BaseQuery {
|
|
|
541
530
|
return this.query.get(data, options);
|
|
542
531
|
}
|
|
543
532
|
}
|
|
533
|
+
/**
|
|
534
|
+
* @deprecated
|
|
535
|
+
* 前端调用后端QueryRouter, 默认路径 /client/router
|
|
536
|
+
*/
|
|
544
537
|
class ClientQuery extends Query {
|
|
545
538
|
constructor(opts) {
|
|
546
539
|
super({ ...opts, url: opts?.url || '/client/router' });
|
package/dist/query.d.ts
CHANGED
|
@@ -112,29 +112,24 @@ type QueryOpts = {
|
|
|
112
112
|
adapter?: typeof adapter;
|
|
113
113
|
[key: string]: any;
|
|
114
114
|
} & AdapterOpts;
|
|
115
|
+
type QueryOptions = {
|
|
116
|
+
url?: string;
|
|
117
|
+
adapter?: typeof adapter;
|
|
118
|
+
headers?: Record<string, string>;
|
|
119
|
+
timeout?: number;
|
|
120
|
+
isClient?: boolean;
|
|
121
|
+
};
|
|
115
122
|
type Data = {
|
|
116
123
|
path?: string;
|
|
117
124
|
key?: string;
|
|
118
125
|
payload?: Record<string, any>;
|
|
119
126
|
[key: string]: any;
|
|
120
127
|
};
|
|
121
|
-
type Result<S = any> = {
|
|
128
|
+
type Result<S = any, U = {}> = {
|
|
122
129
|
code: number;
|
|
123
130
|
data?: S;
|
|
124
131
|
message?: string;
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* 是否不返回 message
|
|
128
|
-
*/
|
|
129
|
-
noMsg?: boolean;
|
|
130
|
-
/**
|
|
131
|
-
* 显示错误, 当 handle的信息被处理的时候,如果不是success,同时自己设置了noMsg,那么就不显示错误信息了,因为被处理。
|
|
132
|
-
*
|
|
133
|
-
* 日常: fetch().then(res=>if(res.sucess){message.error('error')})的时候,比如401被处理过了,就不再提示401错误了。
|
|
134
|
-
*
|
|
135
|
-
*/
|
|
136
|
-
showError: (fn?: () => void) => void;
|
|
137
|
-
};
|
|
132
|
+
} & U;
|
|
138
133
|
type DataOpts = Partial<QueryOpts> & {
|
|
139
134
|
beforeRequest?: Fn;
|
|
140
135
|
afterResponse?: <S = any>(result: Result<S>, ctx?: {
|
|
@@ -153,7 +148,11 @@ type DataOpts = Partial<QueryOpts> & {
|
|
|
153
148
|
* showError 是 如果 success 为 false 且 noMsg 为 false, 则调用 showError
|
|
154
149
|
* @param res 响应
|
|
155
150
|
*/
|
|
156
|
-
declare const setBaseResponse: (res: Partial<Result
|
|
151
|
+
declare const setBaseResponse: (res: Partial<Result & {
|
|
152
|
+
success?: boolean;
|
|
153
|
+
showError?: (fn?: () => void) => void;
|
|
154
|
+
noMsg?: boolean;
|
|
155
|
+
}>) => Result;
|
|
157
156
|
declare const wrapperError: ({ code, message }: {
|
|
158
157
|
code?: number;
|
|
159
158
|
message?: string;
|
|
@@ -191,7 +190,8 @@ declare class Query {
|
|
|
191
190
|
*/
|
|
192
191
|
stop?: boolean;
|
|
193
192
|
qws: QueryWs;
|
|
194
|
-
|
|
193
|
+
isClient: boolean;
|
|
194
|
+
constructor(opts?: QueryOptions);
|
|
195
195
|
setQueryWs(qws: QueryWs): void;
|
|
196
196
|
/**
|
|
197
197
|
* 突然停止请求
|
|
@@ -228,6 +228,7 @@ declare class Query {
|
|
|
228
228
|
fetchText(urlOrOptions?: string | QueryOpts, options?: QueryOpts): Promise<Result<any>>;
|
|
229
229
|
}
|
|
230
230
|
|
|
231
|
+
/** @deprecated */
|
|
231
232
|
declare class BaseQuery<T extends Query = Query, R extends {
|
|
232
233
|
queryChain?: any;
|
|
233
234
|
query?: any;
|
|
@@ -246,9 +247,13 @@ declare class BaseQuery<T extends Query = Query, R extends {
|
|
|
246
247
|
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
247
248
|
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
248
249
|
}
|
|
250
|
+
/**
|
|
251
|
+
* @deprecated
|
|
252
|
+
* 前端调用后端QueryRouter, 默认路径 /client/router
|
|
253
|
+
*/
|
|
249
254
|
declare class ClientQuery extends Query {
|
|
250
255
|
constructor(opts?: QueryOpts);
|
|
251
256
|
}
|
|
252
257
|
|
|
253
258
|
export { BaseQuery, ClientQuery, Query, adapter, setBaseResponse, wrapperError };
|
|
254
|
-
export type { Data, DataOpts, Fn, QueryOpts, Result };
|
|
259
|
+
export type { Data, DataOpts, Fn, QueryOptions, QueryOpts, Result };
|
package/dist/query.js
CHANGED
|
@@ -79,7 +79,7 @@ const adapter = async (opts = {}, overloadOpts) => {
|
|
|
79
79
|
}
|
|
80
80
|
else if (isTextForContentType(contentType)) {
|
|
81
81
|
return {
|
|
82
|
-
code:
|
|
82
|
+
code: response.status,
|
|
83
83
|
status: response.status,
|
|
84
84
|
data: await response.text(), // 直接返回文本内容
|
|
85
85
|
};
|
|
@@ -161,9 +161,11 @@ class Query {
|
|
|
161
161
|
stop;
|
|
162
162
|
// 默认不使用ws
|
|
163
163
|
qws;
|
|
164
|
+
isClient = false;
|
|
164
165
|
constructor(opts) {
|
|
165
166
|
this.adapter = opts?.adapter || adapter;
|
|
166
|
-
|
|
167
|
+
const defaultURL = opts?.isClient ? '/client/router' : '/api/router';
|
|
168
|
+
this.url = opts?.url || defaultURL;
|
|
167
169
|
this.headers = opts?.headers || {
|
|
168
170
|
'Content-Type': 'application/json',
|
|
169
171
|
};
|
|
@@ -249,7 +251,6 @@ class Query {
|
|
|
249
251
|
}
|
|
250
252
|
return _adapter(req).then(async (res) => {
|
|
251
253
|
try {
|
|
252
|
-
setBaseResponse(res);
|
|
253
254
|
if (_afterResponse) {
|
|
254
255
|
return await _afterResponse(res, {
|
|
255
256
|
req,
|
|
@@ -297,9 +298,16 @@ class Query {
|
|
|
297
298
|
...(_options?.headers || {}),
|
|
298
299
|
},
|
|
299
300
|
});
|
|
300
|
-
|
|
301
|
+
if (res && !res.code) {
|
|
302
|
+
return {
|
|
303
|
+
code: 200,
|
|
304
|
+
data: res,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
return res;
|
|
301
308
|
}
|
|
302
309
|
}
|
|
310
|
+
/** @deprecated */
|
|
303
311
|
class BaseQuery {
|
|
304
312
|
query;
|
|
305
313
|
queryDefine;
|
|
@@ -325,6 +333,10 @@ class BaseQuery {
|
|
|
325
333
|
return this.query.get(data, options);
|
|
326
334
|
}
|
|
327
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* @deprecated
|
|
338
|
+
* 前端调用后端QueryRouter, 默认路径 /client/router
|
|
339
|
+
*/
|
|
328
340
|
class ClientQuery extends Query {
|
|
329
341
|
constructor(opts) {
|
|
330
342
|
super({ ...opts, url: opts?.url || '/client/router' });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.33",
|
|
4
4
|
"main": "dist/query-browser.js",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
25
25
|
"@rollup/plugin-typescript": "^12.3.0",
|
|
26
|
-
"rollup": "^4.53.
|
|
26
|
+
"rollup": "^4.53.5",
|
|
27
27
|
"rollup-plugin-dts": "^6.3.0",
|
|
28
28
|
"typescript": "^5.9.3",
|
|
29
29
|
"zustand": "^5.0.9"
|
|
@@ -47,10 +47,6 @@
|
|
|
47
47
|
"./ws": {
|
|
48
48
|
"import": "./dist/query-ws.js",
|
|
49
49
|
"require": "./dist/query-ws.js"
|
|
50
|
-
},
|
|
51
|
-
"./query-ai": {
|
|
52
|
-
"import": "./dist/query-ai.js",
|
|
53
|
-
"require": "./dist/query-ai.js"
|
|
54
50
|
}
|
|
55
51
|
}
|
|
56
52
|
}
|