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

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.
@@ -0,0 +1,26 @@
1
+ type WebApi<Option = any, Res = any, Config = any> = (option: Option, config?: Config) => Promise<Res>;
2
+ /**
3
+ * web api 的调用配置
4
+ */
5
+ export type WebApiConfig<Res = any, Err = any> = {
6
+ /** 是否显示加载提示, 默认 false. (支持字符串,自定义文本) */
7
+ showLoading?: boolean | string;
8
+ /** 操作成功的toast提示, 默认不显示 */
9
+ toastSuccess?: ((res: Res) => false | string) | false | string;
10
+ /** 是否显示操作失败的详细错误信息, 默认 true. (支持字符串,自定义文本; 支持根据errMsg判断是否显示, 例如: (e) => !e.errMsg.includes('cancel') */
11
+ toastError?: ((e: Err) => boolean | string) | boolean | string;
12
+ /** 是否显示日志, 默认 true */
13
+ showLog?: boolean;
14
+ };
15
+ /**
16
+ * 拓展 web api, 使其支持loading,toast,log能力
17
+ * @param webApi web api
18
+ * @param apiName web api 名称 (可选, 用于日志输出, 默认'enhanceWebApi')
19
+ * @return 注入拓展能力的promise (默认提示异常和输出日志,不显示进度条和操作成功)
20
+ * @example
21
+ * const promise = enhanceWebApi(downloadFile, 'downloadFile');
22
+ * await promise({ url: 'xx' }, {showLoading: '下载中', toastSuccess: '下载成功'});
23
+ */
24
+ export declare function enhanceWebApi<Option = any, Res = any, Err = any, Config = any>(webApi: WebApi<Option, Res, Config>, apiName?: string): (option: Option, config?: WebApiConfig<Res, Err> & Config) => Promise<Res>;
25
+ export {};
26
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/web/async/index.ts"],"names":[],"mappings":"AAEA,KAAK,MAAM,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,IAAI,CACnD,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,MAAM,KACZ,OAAO,CAAC,GAAG,CAAC,CAAC;AAElB;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,IAAI;IAC/C,wCAAwC;IACxC,WAAW,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAE/B,0BAA0B;IAC1B,YAAY,CAAC,EAAE,CAAC,CAAC,GAAG,EAAE,GAAG,KAAK,KAAK,GAAG,MAAM,CAAC,GAAG,KAAK,GAAG,MAAM,CAAC;IAE/D,wGAAwG;IACxG,UAAU,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,GAAG,KAAK,OAAO,GAAG,MAAM,CAAC,GAAG,OAAO,GAAG,MAAM,CAAC;IAE/D,sBAAsB;IACtB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAgB,aAAa,CAAC,MAAM,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,EAAE,MAAM,GAAG,GAAG,EAC5E,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,CAAC,EACnC,OAAO,CAAC,EAAE,MAAM,IAER,QAAQ,MAAM,EAAE,SAAS,YAAY,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM,kBA+CjE"}
@@ -755,6 +755,8 @@ var baseToolsWeb = (() => {
755
755
  copyText: () => copyText,
756
756
  copyUrl: () => copyUrl,
757
757
  download: () => download,
758
+ filterRequestData: () => filterRequestData,
759
+ filterRequestHeader: () => filterRequestHeader,
758
760
  getBaseToolsConfig: () => getBaseToolsConfig,
759
761
  getBrowserName: () => getBrowserName,
760
762
  getBrowserVersion: () => getBrowserVersion,
@@ -794,6 +796,7 @@ var baseToolsWeb = (() => {
794
796
  setCookie: () => setCookie,
795
797
  setLocalStorage: () => setLocalStorage,
796
798
  unlockBodyScroll: () => unlockBodyScroll,
799
+ uploadFile: () => uploadFile,
797
800
  windowScrollTo: () => windowScrollTo
798
801
  });
799
802
 
@@ -1181,7 +1184,7 @@ var baseToolsWeb = (() => {
1181
1184
  window.scrollTo(0, y);
1182
1185
  }
1183
1186
 
1184
- // src/web/network/load.ts
1187
+ // src/web/network/download.ts
1185
1188
  function download(url, fileName = "") {
1186
1189
  return __async(this, null, function* () {
1187
1190
  if (!url) return;
@@ -1578,20 +1581,6 @@ var baseToolsWeb = (() => {
1578
1581
  return key === "__proto__";
1579
1582
  }
1580
1583
 
1581
- // node_modules/.pnpm/es-toolkit@1.42.0/node_modules/es-toolkit/dist/object/pickBy.mjs
1582
- function pickBy(obj, shouldPick) {
1583
- const result = {};
1584
- const keys = Object.keys(obj);
1585
- for (let i = 0; i < keys.length; i++) {
1586
- const key = keys[i];
1587
- const value = obj[key];
1588
- if (shouldPick(value, key)) {
1589
- result[key] = value;
1590
- }
1591
- }
1592
- return result;
1593
- }
1594
-
1595
1584
  // node_modules/.pnpm/es-toolkit@1.42.0/node_modules/es-toolkit/dist/compat/_internal/isDeepKey.mjs
1596
1585
  function isDeepKey(key) {
1597
1586
  switch (typeof key) {
@@ -1835,9 +1824,8 @@ var baseToolsWeb = (() => {
1835
1824
  const isGet = method === "GET";
1836
1825
  const isObjectData = isPlainObject(data);
1837
1826
  const isArrayData = !isObjectData && Array.isArray(data);
1838
- const fillData = isObjectData ? pickBy(data, (val) => val !== void 0) : data;
1839
- const emptyValue = [void 0, null, ""];
1840
- const fillHeader = header ? pickBy(header, (val) => !emptyValue.includes(val)) : {};
1827
+ const fillData = isObjectData ? filterRequestData(data) : data;
1828
+ const fillHeader = filterRequestHeader(header);
1841
1829
  const contentTypeKey = Object.keys(fillHeader).find(
1842
1830
  (k) => k.toLowerCase() === "content-type"
1843
1831
  );
@@ -1878,7 +1866,8 @@ var baseToolsWeb = (() => {
1878
1866
  }
1879
1867
  }
1880
1868
  const appConfig2 = getBaseToolsConfig();
1881
- if (showLoading) (_a = appConfig2.showLoading) == null ? void 0 : _a.call(appConfig2);
1869
+ if (showLoading)
1870
+ (_a = appConfig2.showLoading) == null ? void 0 : _a.call(appConfig2, typeof showLoading === "string" ? { title: showLoading } : {});
1882
1871
  let isTimeout = false;
1883
1872
  const timeoutId = setTimeout(() => {
1884
1873
  isTimeout = true;
@@ -1943,6 +1932,22 @@ var baseToolsWeb = (() => {
1943
1932
  promise.task = task;
1944
1933
  return promise;
1945
1934
  }
1935
+ function filterRequestData(data) {
1936
+ const res = {};
1937
+ Object.entries(data).forEach(([k, v]) => {
1938
+ if (v !== void 0) res[k] = v;
1939
+ });
1940
+ return res;
1941
+ }
1942
+ function filterRequestHeader(header) {
1943
+ const newHeader = {};
1944
+ if (header) {
1945
+ Object.entries(header).forEach(([k, v]) => {
1946
+ if (v !== void 0 && v !== null && v !== "") newHeader[k] = String(v);
1947
+ });
1948
+ }
1949
+ return newHeader;
1950
+ }
1946
1951
  function logRequestInfo(options) {
1947
1952
  const { log } = getBaseToolsConfig();
1948
1953
  const { isLog = true } = options.config;
@@ -2051,6 +2056,112 @@ var baseToolsWeb = (() => {
2051
2056
  return formData;
2052
2057
  }
2053
2058
 
2059
+ // src/web/async/index.ts
2060
+ function enhanceWebApi(webApi, apiName) {
2061
+ return (option, config) => {
2062
+ const finalConfig = config || {};
2063
+ const {
2064
+ showLoading = false,
2065
+ toastSuccess = false,
2066
+ toastError = true,
2067
+ showLog = true
2068
+ } = finalConfig;
2069
+ const {
2070
+ log,
2071
+ toast,
2072
+ showLoading: showLoadingFn,
2073
+ hideLoading: hideLoadingFn
2074
+ } = getBaseToolsConfig();
2075
+ const fname = apiName || "enhanceWebApi";
2076
+ if (showLoading) {
2077
+ const title = typeof showLoading === "string" ? showLoading : "";
2078
+ showLoadingFn == null ? void 0 : showLoadingFn({ title });
2079
+ }
2080
+ return new Promise((resolve, reject) => {
2081
+ webApi(option, finalConfig).then((res) => {
2082
+ if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
2083
+ if (showLog) log == null ? void 0 : log("info", { name: fname, status: "success", option, res });
2084
+ resolve(res);
2085
+ const msg = typeof toastSuccess === "function" ? toastSuccess(res) : toastSuccess;
2086
+ if (msg) toast == null ? void 0 : toast({ msg, status: "success" });
2087
+ }).catch((e) => {
2088
+ if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
2089
+ if (showLog) log == null ? void 0 : log("error", { name: fname, status: "fail", option, e });
2090
+ const msg = typeof toastError === "function" ? toastError(e) : toastError;
2091
+ if (msg) {
2092
+ toast == null ? void 0 : toast({
2093
+ msg: typeof msg === "string" ? msg : `${fname} fail: ${JSON.stringify(e)}`,
2094
+ status: "fail"
2095
+ });
2096
+ }
2097
+ reject(e);
2098
+ });
2099
+ });
2100
+ };
2101
+ }
2102
+
2103
+ // src/web/network/uploadFile.ts
2104
+ function upload(option, config) {
2105
+ return new Promise((resolve, reject) => {
2106
+ var _a;
2107
+ const xhr = new XMLHttpRequest();
2108
+ const { url, file, name = "file", header, formData, timeout = 0 } = option;
2109
+ const fail = (error) => reject(error);
2110
+ const success = (responseText) => {
2111
+ try {
2112
+ resolve(JSON.parse(responseText));
2113
+ } catch (e) {
2114
+ resolve(responseText);
2115
+ }
2116
+ };
2117
+ let onProgressUpdate;
2118
+ const task = {
2119
+ onProgressUpdate: (callback) => {
2120
+ onProgressUpdate = callback;
2121
+ },
2122
+ abort: () => xhr.abort()
2123
+ };
2124
+ (_a = config == null ? void 0 : config.onTaskReady) == null ? void 0 : _a.call(config, task);
2125
+ xhr.upload.onprogress = (e) => {
2126
+ if (!e.lengthComputable) return;
2127
+ const ev = {
2128
+ progress: Math.round(e.loaded / e.total * 100),
2129
+ loaded: e.loaded,
2130
+ total: e.total
2131
+ };
2132
+ onProgressUpdate == null ? void 0 : onProgressUpdate(ev);
2133
+ };
2134
+ xhr.onload = () => {
2135
+ if (xhr.status >= 200 && xhr.status < 300) {
2136
+ success(xhr.responseText);
2137
+ } else {
2138
+ fail({ message: `\u4E0A\u4F20\u5931\u8D25`, status: xhr.status });
2139
+ }
2140
+ };
2141
+ xhr.onerror = () => fail({ message: "\u7F51\u7EDC\u9519\u8BEF", status: 0 });
2142
+ xhr.ontimeout = () => fail({ message: "\u4E0A\u4F20\u8D85\u65F6", status: -1 });
2143
+ xhr.onabort = () => fail({ message: "\u7528\u6237\u53D6\u6D88", status: -2 });
2144
+ xhr.open("POST", url);
2145
+ if (header) {
2146
+ Object.entries(header).forEach(([k, v]) => {
2147
+ if (v !== void 0 && v !== null && v !== "") xhr.setRequestHeader(k, String(v));
2148
+ });
2149
+ }
2150
+ xhr.timeout = timeout;
2151
+ const data = new FormData();
2152
+ data.append(name, file);
2153
+ if (formData) {
2154
+ Object.entries(formData).forEach(([k, v]) => {
2155
+ if (v !== void 0 && v !== null) data.append(k, String(v));
2156
+ });
2157
+ }
2158
+ xhr.send(data);
2159
+ });
2160
+ }
2161
+ function uploadFile(option, config) {
2162
+ return enhanceWebApi(upload, "uploadFile")(option, config);
2163
+ }
2164
+
2054
2165
  // src/web/storage/index.ts
2055
2166
  var WK = {
2056
2167
  val: "__l_val",