@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.cjs
CHANGED
|
@@ -2404,13 +2404,39 @@ function enhanceWebApi(webApi, apiName) {
|
|
|
2404
2404
|
}
|
|
2405
2405
|
|
|
2406
2406
|
// src/web/network/uploadFile.ts
|
|
2407
|
+
function parseJsonSafe(text) {
|
|
2408
|
+
try {
|
|
2409
|
+
return JSON.parse(text);
|
|
2410
|
+
} catch (e) {
|
|
2411
|
+
return null;
|
|
2412
|
+
}
|
|
2413
|
+
}
|
|
2414
|
+
function getErrorMessage(responseText, fallback) {
|
|
2415
|
+
const parsed = parseJsonSafe(responseText);
|
|
2416
|
+
if (parsed && typeof parsed === "object" && "message" in parsed) {
|
|
2417
|
+
const message = parsed.message;
|
|
2418
|
+
if (typeof message === "string" && message.trim()) {
|
|
2419
|
+
return message;
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
return fallback;
|
|
2423
|
+
}
|
|
2407
2424
|
function upload(option, config) {
|
|
2408
2425
|
return new Promise((resolve, reject) => {
|
|
2409
2426
|
var _a;
|
|
2410
2427
|
const xhr = new XMLHttpRequest();
|
|
2411
|
-
const { url, file, name = "file", header, data, timeout = 0 } = option;
|
|
2428
|
+
const { url, file, name = "file", header, data, timeout = 0, responseType = "text" } = option;
|
|
2412
2429
|
const fail = (error) => reject(error);
|
|
2413
2430
|
const success = (responseText) => {
|
|
2431
|
+
if (responseType === "json") {
|
|
2432
|
+
const parsed = parseJsonSafe(responseText);
|
|
2433
|
+
if (parsed === null) {
|
|
2434
|
+
fail({ message: "\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON", status: xhr.status });
|
|
2435
|
+
return;
|
|
2436
|
+
}
|
|
2437
|
+
resolve(parsed);
|
|
2438
|
+
return;
|
|
2439
|
+
}
|
|
2414
2440
|
resolve(responseText);
|
|
2415
2441
|
};
|
|
2416
2442
|
let onProgressUpdate;
|
|
@@ -2431,10 +2457,15 @@ function upload(option, config) {
|
|
|
2431
2457
|
onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
|
|
2432
2458
|
};
|
|
2433
2459
|
xhr.onload = () => {
|
|
2460
|
+
const responseText = xhr.responseText || "";
|
|
2434
2461
|
if (xhr.status >= 200 && xhr.status < 300) {
|
|
2435
|
-
success(
|
|
2462
|
+
success(responseText);
|
|
2436
2463
|
} else {
|
|
2437
|
-
fail({
|
|
2464
|
+
fail({
|
|
2465
|
+
message: getErrorMessage(responseText, "\u4E0A\u4F20\u5931\u8D25"),
|
|
2466
|
+
status: xhr.status,
|
|
2467
|
+
data: parseJsonSafe(responseText)
|
|
2468
|
+
});
|
|
2438
2469
|
}
|
|
2439
2470
|
};
|
|
2440
2471
|
xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
|
|
@@ -2458,7 +2489,10 @@ function upload(option, config) {
|
|
|
2458
2489
|
});
|
|
2459
2490
|
}
|
|
2460
2491
|
function uploadFile(option, config) {
|
|
2461
|
-
return enhanceWebApi(
|
|
2492
|
+
return enhanceWebApi(
|
|
2493
|
+
upload,
|
|
2494
|
+
"uploadFile"
|
|
2495
|
+
)(option, config);
|
|
2462
2496
|
}
|
|
2463
2497
|
|
|
2464
2498
|
// src/web/storage/index.ts
|