@base-web-kits/base-tools-web 1.4.2 → 1.4.5
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 +38 -4
- package/dist/base-tools-web.umd.global.js.map +1 -1
- package/dist/index.cjs +38 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -1
- package/dist/index.mjs +38 -4
- package/dist/index.mjs.map +1 -1
- package/dist/network/uploadFile.d.ts +7 -1
- package/dist/network/uploadFile.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/web/network/uploadFile.ts +54 -7
|
@@ -15,6 +15,8 @@ export type UploadFileOption = {
|
|
|
15
15
|
data?: Record<string, string | number>;
|
|
16
16
|
/** 超时时间,单位 ms,默认 0(不超时) */
|
|
17
17
|
timeout?: number;
|
|
18
|
+
/** 响应类型, 默认'text' */
|
|
19
|
+
responseType?: 'text' | 'json';
|
|
18
20
|
};
|
|
19
21
|
export type OnUploadProgressUpdate = (res: UploadProgressEvent) => void;
|
|
20
22
|
export type UploadProgressEvent = {
|
|
@@ -38,6 +40,7 @@ export type UploadConfig = {
|
|
|
38
40
|
export type UploadFail = {
|
|
39
41
|
message: string;
|
|
40
42
|
status: number;
|
|
43
|
+
data?: unknown;
|
|
41
44
|
};
|
|
42
45
|
/**
|
|
43
46
|
* 上传文件
|
|
@@ -53,8 +56,11 @@ export type UploadFail = {
|
|
|
53
56
|
* task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
|
|
54
57
|
* });
|
|
55
58
|
*
|
|
59
|
+
* // 直接返回json对象
|
|
60
|
+
* const json = await uploadFile({ url: 'https://xx', file: file, responseType: 'json' });
|
|
61
|
+
*
|
|
56
62
|
* // 解析上传结果
|
|
57
63
|
* console.log('uploadFile ok', JSON.parse(res));
|
|
58
64
|
*/
|
|
59
|
-
export declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig): Promise<
|
|
65
|
+
export declare function uploadFile<T = string>(option: UploadFileOption, config?: UploadConfig & WebApiConfig<T, UploadFail>): Promise<T>;
|
|
60
66
|
//# sourceMappingURL=uploadFile.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../../src/web/network/uploadFile.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa;IACb,GAAG,EAAE,MAAM,CAAC;IAEZ,eAAe;IACf,IAAI,EAAE,IAAI,CAAC;IAEX,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,UAAU;IACV,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IAEzC,WAAW;IACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IAEvC,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"uploadFile.d.ts","sourceRoot":"","sources":["../../src/web/network/uploadFile.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAE7C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC7B,aAAa;IACb,GAAG,EAAE,MAAM,CAAC;IAEZ,eAAe;IACf,IAAI,EAAE,IAAI,CAAC;IAEX,mDAAmD;IACnD,IAAI,CAAC,EAAE,MAAM,CAAC;IAEd,UAAU;IACV,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IAEzC,WAAW;IACX,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,CAAC;IAEvC,2BAA2B;IAC3B,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,qBAAqB;IACrB,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAChC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,CAAC,GAAG,EAAE,mBAAmB,KAAK,IAAI,CAAC;AAExE,MAAM,MAAM,mBAAmB,GAAG;IAChC,qBAAqB;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa;IACb,MAAM,EAAE,MAAM,CAAC;IACf,WAAW;IACX,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,WAAW;IACX,gBAAgB,EAAE,CAAC,QAAQ,EAAE,sBAAsB,KAAK,IAAI,CAAC;IAC7D,WAAW;IACX,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IACzB,eAAe;IACf,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,UAAU,KAAK,IAAI,CAAC;CAC1C,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AA6GF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,CAAC,CAAC,GAAG,MAAM,EACnC,MAAM,EAAE,gBAAgB,EACxB,MAAM,CAAC,EAAE,YAAY,GAAG,YAAY,CAAC,CAAC,EAAE,UAAU,CAAC,GAClD,OAAO,CAAC,CAAC,CAAC,CAKZ"}
|
package/package.json
CHANGED
|
@@ -22,6 +22,9 @@ export type UploadFileOption = {
|
|
|
22
22
|
|
|
23
23
|
/** 超时时间,单位 ms,默认 0(不超时) */
|
|
24
24
|
timeout?: number;
|
|
25
|
+
|
|
26
|
+
/** 响应类型, 默认'text' */
|
|
27
|
+
responseType?: 'text' | 'json';
|
|
25
28
|
};
|
|
26
29
|
|
|
27
30
|
export type OnUploadProgressUpdate = (res: UploadProgressEvent) => void;
|
|
@@ -50,16 +53,46 @@ export type UploadConfig = {
|
|
|
50
53
|
export type UploadFail = {
|
|
51
54
|
message: string;
|
|
52
55
|
status: number;
|
|
56
|
+
data?: unknown;
|
|
53
57
|
};
|
|
54
58
|
|
|
55
|
-
function
|
|
56
|
-
|
|
59
|
+
function parseJsonSafe(text: string) {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(text);
|
|
62
|
+
} catch {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function getErrorMessage(responseText: string, fallback: string) {
|
|
68
|
+
const parsed = parseJsonSafe(responseText);
|
|
69
|
+
if (parsed && typeof parsed === 'object' && 'message' in parsed) {
|
|
70
|
+
const message = (parsed as { message?: unknown }).message;
|
|
71
|
+
if (typeof message === 'string' && message.trim()) {
|
|
72
|
+
return message;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return fallback;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function upload<T = unknown>(option: UploadFileOption, config?: UploadConfig) {
|
|
79
|
+
return new Promise<string | T>((resolve, reject) => {
|
|
57
80
|
const xhr = new XMLHttpRequest();
|
|
58
|
-
const { url, file, name = 'file', header, data, timeout = 0 } = option;
|
|
81
|
+
const { url, file, name = 'file', header, data, timeout = 0, responseType = 'text' } = option;
|
|
59
82
|
|
|
60
83
|
const fail = (error: UploadFail) => reject(error);
|
|
61
84
|
|
|
62
85
|
const success = (responseText: string) => {
|
|
86
|
+
if (responseType === 'json') {
|
|
87
|
+
const parsed = parseJsonSafe(responseText);
|
|
88
|
+
if (parsed === null) {
|
|
89
|
+
fail({ message: '响应不是合法 JSON', status: xhr.status });
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
resolve(parsed as T);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
63
96
|
resolve(responseText);
|
|
64
97
|
};
|
|
65
98
|
|
|
@@ -86,10 +119,15 @@ function upload(option: UploadFileOption, config?: UploadConfig) {
|
|
|
86
119
|
|
|
87
120
|
// 监听事件
|
|
88
121
|
xhr.onload = () => {
|
|
122
|
+
const responseText = xhr.responseText || '';
|
|
89
123
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
90
|
-
success(
|
|
124
|
+
success(responseText);
|
|
91
125
|
} else {
|
|
92
|
-
fail({
|
|
126
|
+
fail({
|
|
127
|
+
message: getErrorMessage(responseText, '上传失败'),
|
|
128
|
+
status: xhr.status,
|
|
129
|
+
data: parseJsonSafe(responseText),
|
|
130
|
+
});
|
|
93
131
|
}
|
|
94
132
|
};
|
|
95
133
|
xhr.onerror = () => fail({ message: '网络错误', status: 0 });
|
|
@@ -139,9 +177,18 @@ function upload(option: UploadFileOption, config?: UploadConfig) {
|
|
|
139
177
|
* task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
|
|
140
178
|
* });
|
|
141
179
|
*
|
|
180
|
+
* // 直接返回json对象
|
|
181
|
+
* const json = await uploadFile({ url: 'https://xx', file: file, responseType: 'json' });
|
|
182
|
+
*
|
|
142
183
|
* // 解析上传结果
|
|
143
184
|
* console.log('uploadFile ok', JSON.parse(res));
|
|
144
185
|
*/
|
|
145
|
-
export function uploadFile
|
|
146
|
-
|
|
186
|
+
export function uploadFile<T = string>(
|
|
187
|
+
option: UploadFileOption,
|
|
188
|
+
config?: UploadConfig & WebApiConfig<T, UploadFail>,
|
|
189
|
+
): Promise<T> {
|
|
190
|
+
return enhanceWebApi(
|
|
191
|
+
upload as (option: UploadFileOption, config?: UploadConfig) => Promise<T>,
|
|
192
|
+
'uploadFile',
|
|
193
|
+
)(option, config);
|
|
147
194
|
}
|