@kevisual/query 0.0.15 → 0.0.17
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-browser.d.ts +26 -3
- package/dist/query-browser.js +39 -13
- package/dist/query.d.ts +21 -2
- package/dist/query.js +39 -13
- package/package.json +1 -1
package/dist/query-browser.d.ts
CHANGED
|
@@ -89,7 +89,7 @@ type Fn = (opts: {
|
|
|
89
89
|
body?: Record<string, any>;
|
|
90
90
|
[key: string]: any;
|
|
91
91
|
timeout?: number;
|
|
92
|
-
}) => Promise<Record<string, any
|
|
92
|
+
}) => Promise<Record<string, any> | false>;
|
|
93
93
|
type QueryOpts$1 = {
|
|
94
94
|
url?: string;
|
|
95
95
|
adapter?: typeof adapter;
|
|
@@ -129,6 +129,16 @@ type DataOpts = Partial<QueryOpts$1> & {
|
|
|
129
129
|
fetch?: any;
|
|
130
130
|
}) => Promise<Result<S>>;
|
|
131
131
|
};
|
|
132
|
+
declare const wrapperError: ({ code, message }: {
|
|
133
|
+
code?: number;
|
|
134
|
+
message?: string;
|
|
135
|
+
}) => {
|
|
136
|
+
code: number;
|
|
137
|
+
success: boolean;
|
|
138
|
+
message: string;
|
|
139
|
+
showError: (fn?: () => void) => void;
|
|
140
|
+
noMsg: boolean;
|
|
141
|
+
};
|
|
132
142
|
/**
|
|
133
143
|
* const query = new Query();
|
|
134
144
|
* const res = await query.post({
|
|
@@ -184,6 +194,19 @@ declare class Query {
|
|
|
184
194
|
after(fn: DataOpts['afterResponse']): void;
|
|
185
195
|
}
|
|
186
196
|
|
|
197
|
+
declare class BaseQuery<T extends Query = Query> {
|
|
198
|
+
query: T;
|
|
199
|
+
constructor({ query }: {
|
|
200
|
+
query: T;
|
|
201
|
+
});
|
|
202
|
+
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
203
|
+
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
187
210
|
type QueryOpts = {
|
|
188
211
|
url?: string;
|
|
189
212
|
adapter?: typeof adapter;
|
|
@@ -209,5 +232,5 @@ declare class QueryClient extends Query {
|
|
|
209
232
|
removeToken(): void;
|
|
210
233
|
}
|
|
211
234
|
|
|
212
|
-
export { Query, QueryClient, QueryWs, adapter };
|
|
213
|
-
export type { QueryOpts, QueryWsOpts };
|
|
235
|
+
export { BaseQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
|
|
236
|
+
export type { Data, DataOpts, QueryOpts, QueryWsOpts, Result };
|
package/dist/query-browser.js
CHANGED
|
@@ -294,6 +294,18 @@ const setBaseResponse = (res) => {
|
|
|
294
294
|
}
|
|
295
295
|
};
|
|
296
296
|
};
|
|
297
|
+
const wrapperError = ({ code, message }) => {
|
|
298
|
+
const result = {
|
|
299
|
+
code: code || 500,
|
|
300
|
+
success: false,
|
|
301
|
+
message: message || 'api request error',
|
|
302
|
+
showError: (fn) => {
|
|
303
|
+
//
|
|
304
|
+
},
|
|
305
|
+
noMsg: true,
|
|
306
|
+
};
|
|
307
|
+
return result;
|
|
308
|
+
};
|
|
297
309
|
/**
|
|
298
310
|
* const query = new Query();
|
|
299
311
|
* const res = await query.post({
|
|
@@ -364,17 +376,22 @@ class Query {
|
|
|
364
376
|
};
|
|
365
377
|
try {
|
|
366
378
|
if (_beforeRequest) {
|
|
367
|
-
await _beforeRequest(req);
|
|
379
|
+
const res = await _beforeRequest(req);
|
|
380
|
+
if (res === false) {
|
|
381
|
+
return wrapperError({
|
|
382
|
+
code: 500,
|
|
383
|
+
message: 'request is cancel',
|
|
384
|
+
// @ts-ignore
|
|
385
|
+
req: req,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
368
388
|
}
|
|
369
389
|
}
|
|
370
390
|
catch (e) {
|
|
371
391
|
console.error('request beforeFn error', e, req);
|
|
372
|
-
return {
|
|
392
|
+
return wrapperError({
|
|
373
393
|
code: 500,
|
|
374
|
-
|
|
375
|
-
message: 'api request beforeFn error',
|
|
376
|
-
showError: () => { },
|
|
377
|
-
};
|
|
394
|
+
message: 'api request beforeFn error'});
|
|
378
395
|
}
|
|
379
396
|
if (this.stop) {
|
|
380
397
|
const that = this;
|
|
@@ -405,13 +422,10 @@ class Query {
|
|
|
405
422
|
return res;
|
|
406
423
|
}
|
|
407
424
|
catch (e) {
|
|
408
|
-
console.error('request error', e, req);
|
|
409
|
-
return {
|
|
425
|
+
console.error('request afterFn error', e, req);
|
|
426
|
+
return wrapperError({
|
|
410
427
|
code: 500,
|
|
411
|
-
|
|
412
|
-
message: 'api request afterFn error',
|
|
413
|
-
showError: () => { },
|
|
414
|
-
};
|
|
428
|
+
message: 'api request afterFn error'});
|
|
415
429
|
}
|
|
416
430
|
});
|
|
417
431
|
}
|
|
@@ -430,6 +444,18 @@ class Query {
|
|
|
430
444
|
this.afterResponse = fn;
|
|
431
445
|
}
|
|
432
446
|
}
|
|
447
|
+
class BaseQuery {
|
|
448
|
+
query;
|
|
449
|
+
constructor({ query }) {
|
|
450
|
+
this.query = query;
|
|
451
|
+
}
|
|
452
|
+
post(data, options) {
|
|
453
|
+
return this.query.post(data, options);
|
|
454
|
+
}
|
|
455
|
+
get(data, options) {
|
|
456
|
+
return this.query.get(data, options);
|
|
457
|
+
}
|
|
458
|
+
}
|
|
433
459
|
|
|
434
460
|
/**
|
|
435
461
|
* 前端调用后端QueryRouter
|
|
@@ -474,4 +500,4 @@ class QueryClient extends Query {
|
|
|
474
500
|
// 移除默认生成的实例
|
|
475
501
|
// export const client = new QueryClient();
|
|
476
502
|
|
|
477
|
-
export { Query, QueryClient, QueryWs, adapter };
|
|
503
|
+
export { BaseQuery, Query, QueryClient, QueryWs, adapter, wrapperError };
|
package/dist/query.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ type Fn = (opts: {
|
|
|
26
26
|
body?: Record<string, any>;
|
|
27
27
|
[key: string]: any;
|
|
28
28
|
timeout?: number;
|
|
29
|
-
}) => Promise<Record<string, any
|
|
29
|
+
}) => Promise<Record<string, any> | false>;
|
|
30
30
|
type QueryOpts = {
|
|
31
31
|
url?: string;
|
|
32
32
|
adapter?: typeof adapter;
|
|
@@ -73,6 +73,16 @@ type DataOpts = Partial<QueryOpts> & {
|
|
|
73
73
|
* @param res 响应
|
|
74
74
|
*/
|
|
75
75
|
declare const setBaseResponse: (res: Result) => void;
|
|
76
|
+
declare const wrapperError: ({ code, message }: {
|
|
77
|
+
code?: number;
|
|
78
|
+
message?: string;
|
|
79
|
+
}) => {
|
|
80
|
+
code: number;
|
|
81
|
+
success: boolean;
|
|
82
|
+
message: string;
|
|
83
|
+
showError: (fn?: () => void) => void;
|
|
84
|
+
noMsg: boolean;
|
|
85
|
+
};
|
|
76
86
|
/**
|
|
77
87
|
* const query = new Query();
|
|
78
88
|
* const res = await query.post({
|
|
@@ -128,5 +138,14 @@ declare class Query {
|
|
|
128
138
|
after(fn: DataOpts['afterResponse']): void;
|
|
129
139
|
}
|
|
130
140
|
|
|
131
|
-
|
|
141
|
+
declare class BaseQuery<T extends Query = Query> {
|
|
142
|
+
query: T;
|
|
143
|
+
constructor({ query }: {
|
|
144
|
+
query: T;
|
|
145
|
+
});
|
|
146
|
+
post<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
147
|
+
get<R = any, P = any>(data: P, options?: DataOpts): Promise<Result<R>>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export { BaseQuery, Query, adapter, setBaseResponse, wrapperError };
|
|
132
151
|
export type { Data, DataOpts, Fn, QueryOpts, Result };
|
package/dist/query.js
CHANGED
|
@@ -78,6 +78,18 @@ const setBaseResponse = (res) => {
|
|
|
78
78
|
}
|
|
79
79
|
};
|
|
80
80
|
};
|
|
81
|
+
const wrapperError = ({ code, message }) => {
|
|
82
|
+
const result = {
|
|
83
|
+
code: code || 500,
|
|
84
|
+
success: false,
|
|
85
|
+
message: message || 'api request error',
|
|
86
|
+
showError: (fn) => {
|
|
87
|
+
//
|
|
88
|
+
},
|
|
89
|
+
noMsg: true,
|
|
90
|
+
};
|
|
91
|
+
return result;
|
|
92
|
+
};
|
|
81
93
|
/**
|
|
82
94
|
* const query = new Query();
|
|
83
95
|
* const res = await query.post({
|
|
@@ -148,17 +160,22 @@ class Query {
|
|
|
148
160
|
};
|
|
149
161
|
try {
|
|
150
162
|
if (_beforeRequest) {
|
|
151
|
-
await _beforeRequest(req);
|
|
163
|
+
const res = await _beforeRequest(req);
|
|
164
|
+
if (res === false) {
|
|
165
|
+
return wrapperError({
|
|
166
|
+
code: 500,
|
|
167
|
+
message: 'request is cancel',
|
|
168
|
+
// @ts-ignore
|
|
169
|
+
req: req,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
152
172
|
}
|
|
153
173
|
}
|
|
154
174
|
catch (e) {
|
|
155
175
|
console.error('request beforeFn error', e, req);
|
|
156
|
-
return {
|
|
176
|
+
return wrapperError({
|
|
157
177
|
code: 500,
|
|
158
|
-
|
|
159
|
-
message: 'api request beforeFn error',
|
|
160
|
-
showError: () => { },
|
|
161
|
-
};
|
|
178
|
+
message: 'api request beforeFn error'});
|
|
162
179
|
}
|
|
163
180
|
if (this.stop) {
|
|
164
181
|
const that = this;
|
|
@@ -189,13 +206,10 @@ class Query {
|
|
|
189
206
|
return res;
|
|
190
207
|
}
|
|
191
208
|
catch (e) {
|
|
192
|
-
console.error('request error', e, req);
|
|
193
|
-
return {
|
|
209
|
+
console.error('request afterFn error', e, req);
|
|
210
|
+
return wrapperError({
|
|
194
211
|
code: 500,
|
|
195
|
-
|
|
196
|
-
message: 'api request afterFn error',
|
|
197
|
-
showError: () => { },
|
|
198
|
-
};
|
|
212
|
+
message: 'api request afterFn error'});
|
|
199
213
|
}
|
|
200
214
|
});
|
|
201
215
|
}
|
|
@@ -214,5 +228,17 @@ class Query {
|
|
|
214
228
|
this.afterResponse = fn;
|
|
215
229
|
}
|
|
216
230
|
}
|
|
231
|
+
class BaseQuery {
|
|
232
|
+
query;
|
|
233
|
+
constructor({ query }) {
|
|
234
|
+
this.query = query;
|
|
235
|
+
}
|
|
236
|
+
post(data, options) {
|
|
237
|
+
return this.query.post(data, options);
|
|
238
|
+
}
|
|
239
|
+
get(data, options) {
|
|
240
|
+
return this.query.get(data, options);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
217
243
|
|
|
218
|
-
export { Query, adapter, setBaseResponse };
|
|
244
|
+
export { BaseQuery, Query, adapter, setBaseResponse, wrapperError };
|