@base-web-kits/base-tools-web 1.1.11 → 1.1.18-alpha.0

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
@@ -439,7 +439,7 @@ function unlockBodyScroll() {
439
439
  window.scrollTo(0, y);
440
440
  }
441
441
 
442
- // src/web/network/load.ts
442
+ // src/web/network/download.ts
443
443
  function download(url, fileName = "") {
444
444
  return __async(this, null, function* () {
445
445
  if (!url) return;
@@ -710,6 +710,7 @@ var EventBus = class {
710
710
  return this;
711
711
  }
712
712
  };
713
+ var EventBus_default = new EventBus();
713
714
 
714
715
  // src/ts/day/index.ts
715
716
  import dayjs from "dayjs";
@@ -1602,9 +1603,8 @@ function request(config) {
1602
1603
  const isGet = method === "GET";
1603
1604
  const isObjectData = (0, ts_exports.isPlainObject)(data);
1604
1605
  const isArrayData = !isObjectData && Array.isArray(data);
1605
- const fillData = isObjectData ? (0, ts_exports.pickBy)(data, (val) => val !== void 0) : data;
1606
- const emptyValue = [void 0, null, ""];
1607
- const fillHeader = header ? (0, ts_exports.pickBy)(header, (val) => !emptyValue.includes(val)) : {};
1606
+ const fillData = isObjectData ? filterRequestData(data) : data;
1607
+ const fillHeader = filterRequestHeader(header);
1608
1608
  const contentTypeKey = Object.keys(fillHeader).find(
1609
1609
  (k) => k.toLowerCase() === "content-type"
1610
1610
  );
@@ -1645,7 +1645,8 @@ function request(config) {
1645
1645
  }
1646
1646
  }
1647
1647
  const appConfig2 = getBaseToolsConfig();
1648
- if (showLoading) (_a = appConfig2.showLoading) == null ? void 0 : _a.call(appConfig2);
1648
+ if (showLoading)
1649
+ (_a = appConfig2.showLoading) == null ? void 0 : _a.call(appConfig2, typeof showLoading === "string" ? { title: showLoading } : {});
1649
1650
  let isTimeout = false;
1650
1651
  const timeoutId = setTimeout(() => {
1651
1652
  isTimeout = true;
@@ -1710,6 +1711,22 @@ function request(config) {
1710
1711
  promise.task = task;
1711
1712
  return promise;
1712
1713
  }
1714
+ function filterRequestData(data) {
1715
+ const res = {};
1716
+ Object.entries(data).forEach(([k, v]) => {
1717
+ if (v !== void 0) res[k] = v;
1718
+ });
1719
+ return res;
1720
+ }
1721
+ function filterRequestHeader(header) {
1722
+ const newHeader = {};
1723
+ if (header) {
1724
+ Object.entries(header).forEach(([k, v]) => {
1725
+ if (v !== void 0 && v !== null && v !== "") newHeader[k] = String(v);
1726
+ });
1727
+ }
1728
+ return newHeader;
1729
+ }
1713
1730
  function logRequestInfo(options) {
1714
1731
  const { log } = getBaseToolsConfig();
1715
1732
  const { isLog = true } = options.config;
@@ -1818,6 +1835,115 @@ function toFormData(data) {
1818
1835
  return formData;
1819
1836
  }
1820
1837
 
1838
+ // src/web/async/index.ts
1839
+ function promisifyWebApi(webApi, apiName) {
1840
+ return (option, config) => {
1841
+ const finalConfig = config || {};
1842
+ const {
1843
+ showLoading = false,
1844
+ toastSuccess = false,
1845
+ toastError = true,
1846
+ showLog = true
1847
+ } = finalConfig;
1848
+ const {
1849
+ log,
1850
+ toast,
1851
+ showLoading: showLoadingFn,
1852
+ hideLoading: hideLoadingFn
1853
+ } = getBaseToolsConfig();
1854
+ const fname = apiName || "promisifyWebApi";
1855
+ if (showLoading) {
1856
+ const title = typeof showLoading === "string" ? showLoading : "";
1857
+ showLoadingFn == null ? void 0 : showLoadingFn({ title });
1858
+ }
1859
+ return new Promise((resolve, reject) => {
1860
+ webApi(option, finalConfig).then((res) => {
1861
+ if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
1862
+ if (showLog) log == null ? void 0 : log("info", { name: fname, status: "success", option, res });
1863
+ resolve(res);
1864
+ const msg = typeof toastSuccess === "function" ? toastSuccess(res) : toastSuccess;
1865
+ if (msg) toast == null ? void 0 : toast({ msg, status: "success" });
1866
+ }).catch((e) => {
1867
+ if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
1868
+ if (showLog) log == null ? void 0 : log("error", { name: fname, status: "fail", option, e });
1869
+ const msg = typeof toastError === "function" ? toastError(e) : toastError;
1870
+ if (msg) {
1871
+ toast == null ? void 0 : toast({
1872
+ msg: typeof msg === "string" ? msg : `${fname} fail: ${JSON.stringify(e)}`,
1873
+ status: "fail"
1874
+ });
1875
+ }
1876
+ reject(e);
1877
+ });
1878
+ });
1879
+ };
1880
+ }
1881
+
1882
+ // src/web/network/uploadFile.ts
1883
+ function upload(option, config) {
1884
+ return new Promise((resolve, reject) => {
1885
+ var _a;
1886
+ const xhr = new XMLHttpRequest();
1887
+ const { url, file, name = "file", header, formData, timeout = 0 } = option;
1888
+ const fail = (error) => reject(error);
1889
+ const success = (responseText) => {
1890
+ try {
1891
+ resolve(JSON.parse(responseText));
1892
+ } catch (e) {
1893
+ resolve(responseText);
1894
+ }
1895
+ };
1896
+ let onProgressUpdate;
1897
+ const task = {
1898
+ onProgressUpdate: (callback) => {
1899
+ onProgressUpdate = callback;
1900
+ },
1901
+ abort: () => xhr.abort()
1902
+ };
1903
+ (_a = config == null ? void 0 : config.onTaskReady) == null ? void 0 : _a.call(config, task);
1904
+ xhr.upload.onprogress = (e) => {
1905
+ if (!e.lengthComputable) return;
1906
+ const ev = {
1907
+ progress: Math.round(e.loaded / e.total * 100),
1908
+ loaded: e.loaded,
1909
+ total: e.total
1910
+ };
1911
+ onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
1912
+ };
1913
+ xhr.onload = () => {
1914
+ if (xhr.status >= 200 && xhr.status < 300) {
1915
+ success(xhr.responseText);
1916
+ } else {
1917
+ fail({ message: `\u4E0A\u4F20\u5931\u8D25`, status: xhr.status });
1918
+ }
1919
+ };
1920
+ xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
1921
+ xhr.ontimeout = () => fail({ message: "\u4E0A\u4F20\u8D85\u65F6", status: -1 });
1922
+ xhr.onabort = () => fail({ message: "\u7528\u6237\u53D6\u6D88", status: -2 });
1923
+ xhr.open("POST", url);
1924
+ if (header) {
1925
+ Object.entries(header).forEach(([k, v]) => {
1926
+ if (v !== void 0 && v !== null && v !== "") xhr.setRequestHeader(k, String(v));
1927
+ });
1928
+ }
1929
+ xhr.timeout = timeout;
1930
+ const data = new FormData();
1931
+ data.append(name, file);
1932
+ if (formData) {
1933
+ Object.entries(formData).forEach(([k, v]) => {
1934
+ if (v !== void 0) data.append(k, String(v));
1935
+ });
1936
+ }
1937
+ xhr.send(data);
1938
+ });
1939
+ }
1940
+ function uploadFile(option, config) {
1941
+ return promisifyWebApi(upload, "uploadFile")(
1942
+ option,
1943
+ config
1944
+ );
1945
+ }
1946
+
1821
1947
  // src/web/storage/index.ts
1822
1948
  var WK = {
1823
1949
  val: "__l_val",
@@ -1893,6 +2019,8 @@ export {
1893
2019
  copyText,
1894
2020
  copyUrl,
1895
2021
  download,
2022
+ filterRequestData,
2023
+ filterRequestHeader,
1896
2024
  getBaseToolsConfig,
1897
2025
  getBrowserName,
1898
2026
  getBrowserVersion,
@@ -1932,6 +2060,7 @@ export {
1932
2060
  setCookie,
1933
2061
  setLocalStorage,
1934
2062
  unlockBodyScroll,
2063
+ uploadFile,
1935
2064
  windowScrollTo
1936
2065
  };
1937
2066
  //# sourceMappingURL=index.js.map