@kevisual/query 0.0.11 → 0.0.12
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 +7 -3
- package/dist/query-browser.js +24 -11
- package/dist/query.d.ts +16 -5
- package/dist/query.js +25 -12
- package/package.json +1 -1
package/dist/query-browser.d.ts
CHANGED
|
@@ -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
|
|
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,7 +112,7 @@ declare class Query {
|
|
|
108
112
|
adapter: typeof adapter;
|
|
109
113
|
url: string;
|
|
110
114
|
beforeRequest?: Fn;
|
|
111
|
-
afterResponse?:
|
|
115
|
+
afterResponse?: DataOpts['afterResponse'];
|
|
112
116
|
headers?: Record<string, string>;
|
|
113
117
|
timeout?: number;
|
|
114
118
|
constructor(opts?: QueryOpts$1);
|
|
@@ -139,7 +143,7 @@ declare class Query {
|
|
|
139
143
|
* 请求后处理,设置请求后处理函数
|
|
140
144
|
* @param fn 处理函数
|
|
141
145
|
*/
|
|
142
|
-
after(fn: (result: Result) => Promise<any>): void;
|
|
146
|
+
after(fn: (result: Result, req?: any) => Promise<any>): void;
|
|
143
147
|
}
|
|
144
148
|
|
|
145
149
|
type QueryOpts = {
|
package/dist/query-browser.js
CHANGED
|
@@ -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({
|
|
@@ -293,19 +311,14 @@ class Query {
|
|
|
293
311
|
}
|
|
294
312
|
return adapter(req).then(async (res) => {
|
|
295
313
|
try {
|
|
296
|
-
res
|
|
314
|
+
setBaseResponse(res);
|
|
297
315
|
if (afterResponse) {
|
|
298
|
-
return await afterResponse(res
|
|
316
|
+
return await afterResponse(res, {
|
|
317
|
+
req,
|
|
318
|
+
res,
|
|
319
|
+
fetch: adapter,
|
|
320
|
+
});
|
|
299
321
|
}
|
|
300
|
-
/**
|
|
301
|
-
* 显示错误
|
|
302
|
-
* @param fn 错误处理函数
|
|
303
|
-
*/
|
|
304
|
-
res.showError = (fn) => {
|
|
305
|
-
if (!res.success && !res.noResult) {
|
|
306
|
-
fn?.();
|
|
307
|
-
}
|
|
308
|
-
};
|
|
309
322
|
return res;
|
|
310
323
|
}
|
|
311
324
|
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
|
|
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,7 +81,7 @@ declare class Query {
|
|
|
70
81
|
adapter: typeof adapter;
|
|
71
82
|
url: string;
|
|
72
83
|
beforeRequest?: Fn;
|
|
73
|
-
afterResponse?:
|
|
84
|
+
afterResponse?: DataOpts['afterResponse'];
|
|
74
85
|
headers?: Record<string, string>;
|
|
75
86
|
timeout?: number;
|
|
76
87
|
constructor(opts?: QueryOpts);
|
|
@@ -101,8 +112,8 @@ declare class Query {
|
|
|
101
112
|
* 请求后处理,设置请求后处理函数
|
|
102
113
|
* @param fn 处理函数
|
|
103
114
|
*/
|
|
104
|
-
after(fn: (result: Result) => Promise<any>): void;
|
|
115
|
+
after(fn: (result: Result, req?: any) => Promise<any>): void;
|
|
105
116
|
}
|
|
106
117
|
|
|
107
|
-
export { Query, adapter };
|
|
108
|
-
export type { Data, QueryOpts, Result };
|
|
118
|
+
export { Query, adapter, setBaseResponse };
|
|
119
|
+
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({
|
|
@@ -118,19 +136,14 @@ class Query {
|
|
|
118
136
|
}
|
|
119
137
|
return adapter(req).then(async (res) => {
|
|
120
138
|
try {
|
|
121
|
-
res
|
|
139
|
+
setBaseResponse(res);
|
|
122
140
|
if (afterResponse) {
|
|
123
|
-
return await afterResponse(res
|
|
141
|
+
return await afterResponse(res, {
|
|
142
|
+
req,
|
|
143
|
+
res,
|
|
144
|
+
fetch: adapter,
|
|
145
|
+
});
|
|
124
146
|
}
|
|
125
|
-
/**
|
|
126
|
-
* 显示错误
|
|
127
|
-
* @param fn 错误处理函数
|
|
128
|
-
*/
|
|
129
|
-
res.showError = (fn) => {
|
|
130
|
-
if (!res.success && !res.noResult) {
|
|
131
|
-
fn?.();
|
|
132
|
-
}
|
|
133
|
-
};
|
|
134
147
|
return res;
|
|
135
148
|
}
|
|
136
149
|
catch (e) {
|
|
@@ -160,4 +173,4 @@ class Query {
|
|
|
160
173
|
}
|
|
161
174
|
}
|
|
162
175
|
|
|
163
|
-
export { Query, adapter };
|
|
176
|
+
export { Query, adapter, setBaseResponse };
|