@base-web-kits/base-tools-web 1.4.2 → 1.4.3

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/index.cjs CHANGED
@@ -2404,13 +2404,40 @@ 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
- var _a;
2426
+ var _a, _b;
2410
2427
  const xhr = new XMLHttpRequest();
2411
2428
  const { url, file, name = "file", header, data, timeout = 0 } = option;
2429
+ const responseType = (_a = config == null ? void 0 : config.responseType) != null ? _a : "text";
2412
2430
  const fail = (error) => reject(error);
2413
2431
  const success = (responseText) => {
2432
+ if (responseType === "json") {
2433
+ const parsed = parseJsonSafe(responseText);
2434
+ if (parsed === null) {
2435
+ fail({ message: "\u54CD\u5E94\u4E0D\u662F\u5408\u6CD5 JSON", status: xhr.status });
2436
+ return;
2437
+ }
2438
+ resolve(parsed);
2439
+ return;
2440
+ }
2414
2441
  resolve(responseText);
2415
2442
  };
2416
2443
  let onProgressUpdate;
@@ -2420,7 +2447,7 @@ function upload(option, config) {
2420
2447
  },
2421
2448
  abort: () => xhr.abort()
2422
2449
  };
2423
- (_a = config == null ? void 0 : config.onTaskReady) == null ? void 0 : _a.call(config, task);
2450
+ (_b = config == null ? void 0 : config.onTaskReady) == null ? void 0 : _b.call(config, task);
2424
2451
  xhr.upload.onprogress = (e) => {
2425
2452
  if (!e.lengthComputable) return;
2426
2453
  const ev = {
@@ -2431,10 +2458,15 @@ function upload(option, config) {
2431
2458
  onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
2432
2459
  };
2433
2460
  xhr.onload = () => {
2461
+ const responseText = xhr.responseText || "";
2434
2462
  if (xhr.status >= 200 && xhr.status < 300) {
2435
- success(xhr.responseText);
2463
+ success(responseText);
2436
2464
  } else {
2437
- fail({ message: `\u4E0A\u4F20\u5931\u8D25`, status: xhr.status });
2465
+ fail({
2466
+ message: getErrorMessage(responseText, "\u4E0A\u4F20\u5931\u8D25"),
2467
+ status: xhr.status,
2468
+ data: parseJsonSafe(responseText)
2469
+ });
2438
2470
  }
2439
2471
  };
2440
2472
  xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
@@ -2458,7 +2490,10 @@ function upload(option, config) {
2458
2490
  });
2459
2491
  }
2460
2492
  function uploadFile(option, config) {
2461
- return enhanceWebApi(upload, "uploadFile")(option, config);
2493
+ return enhanceWebApi(
2494
+ upload,
2495
+ "uploadFile"
2496
+ )(option, config);
2462
2497
  }
2463
2498
 
2464
2499
  // src/web/storage/index.ts