@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
package/dist/index.d.cts
CHANGED
|
@@ -566,6 +566,8 @@ type UploadFileOption = {
|
|
|
566
566
|
data?: Record<string, string | number>;
|
|
567
567
|
/** 超时时间,单位 ms,默认 0(不超时) */
|
|
568
568
|
timeout?: number;
|
|
569
|
+
/** 响应类型, 默认'text' */
|
|
570
|
+
responseType?: 'text' | 'json';
|
|
569
571
|
};
|
|
570
572
|
type OnUploadProgressUpdate = (res: UploadProgressEvent) => void;
|
|
571
573
|
type UploadProgressEvent = {
|
|
@@ -589,6 +591,7 @@ type UploadConfig = {
|
|
|
589
591
|
type UploadFail = {
|
|
590
592
|
message: string;
|
|
591
593
|
status: number;
|
|
594
|
+
data?: unknown;
|
|
592
595
|
};
|
|
593
596
|
/**
|
|
594
597
|
* 上传文件
|
|
@@ -604,10 +607,13 @@ type UploadFail = {
|
|
|
604
607
|
* task.onProgressUpdate((res) => console.log('上传进度:', res.progress)),
|
|
605
608
|
* });
|
|
606
609
|
*
|
|
610
|
+
* // 直接返回json对象
|
|
611
|
+
* const json = await uploadFile({ url: 'https://xx', file: file, responseType: 'json' });
|
|
612
|
+
*
|
|
607
613
|
* // 解析上传结果
|
|
608
614
|
* console.log('uploadFile ok', JSON.parse(res));
|
|
609
615
|
*/
|
|
610
|
-
declare function uploadFile(option: UploadFileOption, config?: UploadConfig & WebApiConfig): Promise<
|
|
616
|
+
declare function uploadFile<T = string>(option: UploadFileOption, config?: UploadConfig & WebApiConfig<T, UploadFail>): Promise<T>;
|
|
611
617
|
|
|
612
618
|
/**
|
|
613
619
|
* 写入 localStorage(自动 JSON 序列化)
|
package/dist/index.mjs
CHANGED
|
@@ -2341,13 +2341,39 @@ function enhanceWebApi(webApi, apiName) {
|
|
|
2341
2341
|
}
|
|
2342
2342
|
|
|
2343
2343
|
// src/web/network/uploadFile.ts
|
|
2344
|
+
function parseJsonSafe(text) {
|
|
2345
|
+
try {
|
|
2346
|
+
return JSON.parse(text);
|
|
2347
|
+
} catch (e) {
|
|
2348
|
+
return null;
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
function getErrorMessage(responseText, fallback) {
|
|
2352
|
+
const parsed = parseJsonSafe(responseText);
|
|
2353
|
+
if (parsed && typeof parsed === "object" && "message" in parsed) {
|
|
2354
|
+
const message = parsed.message;
|
|
2355
|
+
if (typeof message === "string" && message.trim()) {
|
|
2356
|
+
return message;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
return fallback;
|
|
2360
|
+
}
|
|
2344
2361
|
function upload(option, config) {
|
|
2345
2362
|
return new Promise((resolve, reject) => {
|
|
2346
2363
|
var _a;
|
|
2347
2364
|
const xhr = new XMLHttpRequest();
|
|
2348
|
-
const { url, file, name = "file", header, data, timeout = 0 } = option;
|
|
2365
|
+
const { url, file, name = "file", header, data, timeout = 0, responseType = "text" } = option;
|
|
2349
2366
|
const fail = (error) => reject(error);
|
|
2350
2367
|
const success = (responseText) => {
|
|
2368
|
+
if (responseType === "json") {
|
|
2369
|
+
const parsed = parseJsonSafe(responseText);
|
|
2370
|
+
if (parsed === null) {
|
|
2371
|
+
fail({ message: "\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON", status: xhr.status });
|
|
2372
|
+
return;
|
|
2373
|
+
}
|
|
2374
|
+
resolve(parsed);
|
|
2375
|
+
return;
|
|
2376
|
+
}
|
|
2351
2377
|
resolve(responseText);
|
|
2352
2378
|
};
|
|
2353
2379
|
let onProgressUpdate;
|
|
@@ -2368,10 +2394,15 @@ function upload(option, config) {
|
|
|
2368
2394
|
onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
|
|
2369
2395
|
};
|
|
2370
2396
|
xhr.onload = () => {
|
|
2397
|
+
const responseText = xhr.responseText || "";
|
|
2371
2398
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
2372
|
-
success(
|
|
2399
|
+
success(responseText);
|
|
2373
2400
|
} else {
|
|
2374
|
-
fail({
|
|
2401
|
+
fail({
|
|
2402
|
+
message: getErrorMessage(responseText, "\u4E0A\u4F20\u5931\u8D25"),
|
|
2403
|
+
status: xhr.status,
|
|
2404
|
+
data: parseJsonSafe(responseText)
|
|
2405
|
+
});
|
|
2375
2406
|
}
|
|
2376
2407
|
};
|
|
2377
2408
|
xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
|
|
@@ -2395,7 +2426,10 @@ function upload(option, config) {
|
|
|
2395
2426
|
});
|
|
2396
2427
|
}
|
|
2397
2428
|
function uploadFile(option, config) {
|
|
2398
|
-
return enhanceWebApi(
|
|
2429
|
+
return enhanceWebApi(
|
|
2430
|
+
upload,
|
|
2431
|
+
"uploadFile"
|
|
2432
|
+
)(option, config);
|
|
2399
2433
|
}
|
|
2400
2434
|
|
|
2401
2435
|
// src/web/storage/index.ts
|