@base-web-kits/base-tools-web 1.1.0-alpha.0 → 1.1.0-alpha.2

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.js CHANGED
@@ -507,9 +507,9 @@ __export(ts_exports, {
507
507
  appendUrlParam: () => appendUrlParam,
508
508
  arrayMove: () => arrayMove,
509
509
  buildOSSUrl: () => buildOSSUrl,
510
- createRandId: () => createRandId,
511
510
  createTimeRandId: () => createTimeRandId,
512
511
  createUUID: () => createUUID,
512
+ createViewRandId: () => createViewRandId,
513
513
  dayjs: () => dayjs,
514
514
  getAgeByBirthdate: () => getAgeByBirthdate,
515
515
  getByteLength: () => getByteLength,
@@ -959,7 +959,7 @@ function createUUID() {
959
959
  return v.toString(16);
960
960
  });
961
961
  }
962
- function createRandId(prefix = "id_") {
962
+ function createViewRandId(prefix = "id_") {
963
963
  return `${prefix}${Math.random().toString(36).substring(2, 16)}`;
964
964
  }
965
965
  function createTimeRandId(digits = 6) {
@@ -1494,7 +1494,7 @@ function request(config) {
1494
1494
  let chunkCallback = null;
1495
1495
  const task = {
1496
1496
  abort: () => controller.abort(),
1497
- onChunkReceived: (cb) => {
1497
+ onProgressUpdate: (cb) => {
1498
1498
  chunkCallback = cb;
1499
1499
  },
1500
1500
  offChunkReceived: () => {
@@ -1527,11 +1527,27 @@ function request(config) {
1527
1527
  const isArrayData = !isObjectData && Array.isArray(data);
1528
1528
  const fillData = isObjectData ? (0, ts_exports.pickBy)(data, (val) => val !== void 0) : data;
1529
1529
  const fillHeader = header ? (0, ts_exports.pickBy)(header, (val) => !!val) : {};
1530
- if (!isGet && fillData && (isObjectData || isArrayData) && !fillHeader["Content-Type"]) {
1530
+ const contentTypeKey = Object.keys(fillHeader).find(
1531
+ (k) => k.toLowerCase() === "content-type"
1532
+ );
1533
+ const contentType = contentTypeKey ? fillHeader[contentTypeKey].toLowerCase() : "";
1534
+ if (!isGet && fillData && (isObjectData || isArrayData) && !contentType) {
1531
1535
  fillHeader["Content-Type"] = "application/json";
1532
1536
  }
1533
1537
  const fillUrl = isGet && isObjectData ? appendUrlParam(url, fillData) : url;
1534
- const fillBody = !isGet && fillData ? isObjectData || isArrayData ? JSON.stringify(fillData) : fillData : void 0;
1538
+ let fillBody;
1539
+ if (!isGet && fillData) {
1540
+ if (isObjectData && contentType.includes("application/x-www-form-urlencoded")) {
1541
+ fillBody = toSearchParams(fillData);
1542
+ } else if (isObjectData && contentType.includes("multipart/form-data")) {
1543
+ fillBody = toFormData(fillData);
1544
+ if (contentTypeKey) delete fillHeader[contentTypeKey];
1545
+ } else if (isObjectData || isArrayData) {
1546
+ fillBody = JSON.stringify(fillData);
1547
+ } else {
1548
+ fillBody = fillData;
1549
+ }
1550
+ }
1535
1551
  const logConfig = { ...config, data: fillData, header: fillHeader, url: fillUrl };
1536
1552
  const startTime = Date.now();
1537
1553
  const isCache = cacheTime && cacheTime > 0;
@@ -1686,6 +1702,40 @@ async function parseResponse(response, responseType) {
1686
1702
  }
1687
1703
  return resData;
1688
1704
  }
1705
+ function toSearchParams(data) {
1706
+ const params = new URLSearchParams();
1707
+ for (const key in data) {
1708
+ const val = data[key];
1709
+ if (val === null) continue;
1710
+ if (Array.isArray(val)) {
1711
+ val.forEach((v) => params.append(key, typeof v === "object" ? JSON.stringify(v) : String(v)));
1712
+ } else {
1713
+ params.append(key, typeof val === "object" ? JSON.stringify(val) : String(val));
1714
+ }
1715
+ }
1716
+ return params;
1717
+ }
1718
+ function toFormData(data) {
1719
+ const formData = new FormData();
1720
+ for (const key in data) {
1721
+ const val = data[key];
1722
+ if (val === null) continue;
1723
+ if (Array.isArray(val)) {
1724
+ val.forEach(
1725
+ (v) => formData.append(
1726
+ key,
1727
+ v instanceof Blob ? v : typeof v === "object" ? JSON.stringify(v) : String(v)
1728
+ )
1729
+ );
1730
+ } else {
1731
+ formData.append(
1732
+ key,
1733
+ val instanceof Blob ? val : typeof val === "object" ? JSON.stringify(val) : String(val)
1734
+ );
1735
+ }
1736
+ }
1737
+ return formData;
1738
+ }
1689
1739
 
1690
1740
  // src/web/storage/index.ts
1691
1741
  var WK = {