@base-web-kits/base-tools-web 1.0.2-alpha.2 → 1.0.2-alpha.4
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/base-tools-web.umd.global.js +6 -5
- package/dist/base-tools-web.umd.global.js.map +1 -1
- package/dist/config/index.d.ts +20 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/index.cjs +6 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +6 -5
- package/dist/index.js.map +1 -1
- package/dist/network/request.d.ts +17 -0
- package/dist/network/request.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/web/config/index.ts +17 -1
- package/src/web/network/request.d.ts +6 -1
- package/src/web/network/request.ts +31 -7
|
@@ -6,6 +6,23 @@ export * from './request.d';
|
|
|
6
6
|
* @param config 请求配置
|
|
7
7
|
* @returns Promise<T> & { task?: RequestTask }
|
|
8
8
|
* @example
|
|
9
|
+
* // 在入口文件完成配置 (确保请求失败有toast提示,登录过期能够触发重新登录,log有日志输出)
|
|
10
|
+
* setBaseToolsConfig({
|
|
11
|
+
* toast: ({ msg, status }) => (status === 'fail' ? message.error(msg) : message.success(msg)),
|
|
12
|
+
* showLoading: () => message.loading('加载中...'),
|
|
13
|
+
* hideLoading: () => message.destroy(),
|
|
14
|
+
* toLogin: () => reLogin(),
|
|
15
|
+
* log(level, data) {
|
|
16
|
+
* if (data.name === 'request') {
|
|
17
|
+
* sendLog('request', data); // 请求日志
|
|
18
|
+
* } else if (level === 'error') {
|
|
19
|
+
* sendLog('error', data); // 错误日志
|
|
20
|
+
* } else {
|
|
21
|
+
* sendLog('action', data); // 操作日志
|
|
22
|
+
* }
|
|
23
|
+
* },
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
9
26
|
* // 封装项目的基础请求
|
|
10
27
|
* export function requestApi<T>(config: RequestConfig) {
|
|
11
28
|
* return request<T>({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/web/network/request.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"request.d.ts","sourceRoot":"","sources":["../../src/web/network/request.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAEV,iBAAiB,EACjB,WAAW,EACX,WAAW,EAEZ,MAAM,aAAa,CAAC;AAErB,cAAc,aAAa,CAAC;AAK5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuEG;AACH,wBAAgB,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,GAAG,WAAW,EAAE,MAAM,EAAE,iBAAiB,CAAC,CAAC,CAAC;WAmL9D,WAAW;EAMxC"}
|
package/package.json
CHANGED
package/src/web/config/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export type AppConfig = {
|
|
2
2
|
/** 全局 Toast 提示 */
|
|
3
|
-
toast?: (msg: string) => void;
|
|
3
|
+
toast?: (option: { msg: string; status: 'success' | 'fail' }) => void;
|
|
4
4
|
/** 显示全局 Loading */
|
|
5
5
|
showLoading?: () => void;
|
|
6
6
|
/** 隐藏全局 Loading */
|
|
@@ -45,6 +45,22 @@ export function getAppConfig() {
|
|
|
45
45
|
|
|
46
46
|
/**
|
|
47
47
|
* 初始化应用配置 (在入口文件设置)
|
|
48
|
+
* @example
|
|
49
|
+
* setBaseToolsConfig({
|
|
50
|
+
* toast: ({ msg, status }) => (status === 'fail' ? message.error(msg) : message.success(msg)),
|
|
51
|
+
* showLoading: () => message.loading('加载中...'),
|
|
52
|
+
* hideLoading: () => message.destroy(),
|
|
53
|
+
* toLogin: () => reLogin(),
|
|
54
|
+
* log(level, data) {
|
|
55
|
+
* if (data.name === 'request') {
|
|
56
|
+
* sendLog('request', data); // 请求日志
|
|
57
|
+
* } else if (level === 'error') {
|
|
58
|
+
* sendLog('error', data); // 错误日志
|
|
59
|
+
* } else {
|
|
60
|
+
* sendLog('action', data); // 操作日志
|
|
61
|
+
* }
|
|
62
|
+
* },
|
|
63
|
+
* });
|
|
48
64
|
*/
|
|
49
65
|
export function setBaseToolsConfig(newConfig: AppConfig) {
|
|
50
66
|
Object.assign(appConfig, newConfig);
|
|
@@ -26,6 +26,11 @@ export type RequestData =
|
|
|
26
26
|
| unknown[]
|
|
27
27
|
| null;
|
|
28
28
|
|
|
29
|
+
/**
|
|
30
|
+
* 响应数据类型
|
|
31
|
+
*/
|
|
32
|
+
export type ResponseData = string | ArrayBuffer | Blob | Record<string, unknown> | unknown[] | null;
|
|
33
|
+
|
|
29
34
|
/**
|
|
30
35
|
* 发起请求的配置 (对外,参数可选)
|
|
31
36
|
*/
|
|
@@ -86,7 +91,7 @@ export type RequestConfigBase<D extends RequestData = RequestData> = {
|
|
|
86
91
|
responseType?: 'text' | 'arraybuffer' | 'json';
|
|
87
92
|
|
|
88
93
|
/** 响应拦截 */
|
|
89
|
-
responseInterceptor?: (data:
|
|
94
|
+
responseInterceptor?: (data: ResponseData) => ResponseData;
|
|
90
95
|
};
|
|
91
96
|
|
|
92
97
|
/**
|
|
@@ -8,7 +8,13 @@ import {
|
|
|
8
8
|
} from '../../ts';
|
|
9
9
|
import { getAppConfig } from '../config';
|
|
10
10
|
import type { AppLogInfo } from '../config';
|
|
11
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
ChunkCallback,
|
|
13
|
+
RequestConfigBase,
|
|
14
|
+
RequestData,
|
|
15
|
+
RequestTask,
|
|
16
|
+
ResponseData,
|
|
17
|
+
} from './request.d';
|
|
12
18
|
|
|
13
19
|
export * from './request.d';
|
|
14
20
|
|
|
@@ -21,6 +27,23 @@ const requestCache = new Map<string, { res: unknown; expire: number }>();
|
|
|
21
27
|
* @param config 请求配置
|
|
22
28
|
* @returns Promise<T> & { task?: RequestTask }
|
|
23
29
|
* @example
|
|
30
|
+
* // 在入口文件完成配置 (确保请求失败有toast提示,登录过期能够触发重新登录,log有日志输出)
|
|
31
|
+
* setBaseToolsConfig({
|
|
32
|
+
* toast: ({ msg, status }) => (status === 'fail' ? message.error(msg) : message.success(msg)),
|
|
33
|
+
* showLoading: () => message.loading('加载中...'),
|
|
34
|
+
* hideLoading: () => message.destroy(),
|
|
35
|
+
* toLogin: () => reLogin(),
|
|
36
|
+
* log(level, data) {
|
|
37
|
+
* if (data.name === 'request') {
|
|
38
|
+
* sendLog('request', data); // 请求日志
|
|
39
|
+
* } else if (level === 'error') {
|
|
40
|
+
* sendLog('error', data); // 错误日志
|
|
41
|
+
* } else {
|
|
42
|
+
* sendLog('action', data); // 操作日志
|
|
43
|
+
* }
|
|
44
|
+
* },
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
24
47
|
* // 封装项目的基础请求
|
|
25
48
|
* export function requestApi<T>(config: RequestConfig) {
|
|
26
49
|
* return request<T>({
|
|
@@ -225,22 +248,23 @@ export function request<T, D extends RequestData = RequestData>(config: RequestC
|
|
|
225
248
|
appConfig.toLogin?.(); // 放在后面,确保reject执行后再跳转登录
|
|
226
249
|
} else {
|
|
227
250
|
// 业务错误
|
|
228
|
-
if (toastError && msg) appConfig.toast?.(msg);
|
|
251
|
+
if (toastError && msg) appConfig.toast?.({ status: 'fail', msg });
|
|
229
252
|
reject(res);
|
|
230
253
|
}
|
|
231
254
|
} catch (e) {
|
|
255
|
+
const status = 'fail';
|
|
232
256
|
const isAbortError = e instanceof DOMException && e.name === 'AbortError'; // 取消请求不视为错误
|
|
233
257
|
|
|
234
258
|
if (isAbortError && isTimeout) {
|
|
235
|
-
if (toastError) appConfig.toast?.('请求超时');
|
|
259
|
+
if (toastError) appConfig.toast?.({ status, msg: '请求超时' });
|
|
236
260
|
const timeoutError = new Error('Request Timeout');
|
|
237
|
-
logRequestInfo({ status
|
|
261
|
+
logRequestInfo({ status, config: logConfig, startTime, e: timeoutError });
|
|
238
262
|
reject(timeoutError);
|
|
239
263
|
return;
|
|
240
264
|
}
|
|
241
265
|
|
|
242
|
-
if (!isAbortError && toastError) appConfig.toast?.('网络请求失败');
|
|
243
|
-
logRequestInfo({ status
|
|
266
|
+
if (!isAbortError && toastError) appConfig.toast?.({ status, msg: '网络请求失败' });
|
|
267
|
+
logRequestInfo({ status, config: logConfig, startTime, e });
|
|
244
268
|
reject(e);
|
|
245
269
|
} finally {
|
|
246
270
|
if (timeoutId) clearTimeout(timeoutId);
|
|
@@ -344,7 +368,7 @@ async function handleStreamResponse(response: Response, chunkCallback: ChunkCall
|
|
|
344
368
|
* 解析响应数据
|
|
345
369
|
*/
|
|
346
370
|
async function parseResponse(response: Response, responseType: string) {
|
|
347
|
-
let resData:
|
|
371
|
+
let resData: ResponseData;
|
|
348
372
|
if (responseType === 'arraybuffer') {
|
|
349
373
|
resData = await response.arrayBuffer();
|
|
350
374
|
} else if (responseType === 'text') {
|