@base-web-kits/base-tools-web 1.1.18-alpha.0 → 1.2.0-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.
@@ -1,8 +1,8 @@
1
- type WebApi<Option, Res, Config = unknown> = (option: Option, config?: Config) => Promise<Res>;
1
+ type WebApi<Option = any, Res = any, Config = any> = (option: Option, config?: Config) => Promise<Res>;
2
2
  /**
3
3
  * web api 的调用配置
4
4
  */
5
- export type WebApiConfig<Res, Err> = {
5
+ export type WebApiConfig<Res = any, Err = any> = {
6
6
  /** 是否显示加载提示, 默认 false. (支持字符串,自定义文本) */
7
7
  showLoading?: boolean | string;
8
8
  /** 操作成功的toast提示, 默认不显示 */
@@ -11,16 +11,20 @@ export type WebApiConfig<Res, Err> = {
11
11
  toastError?: ((e: Err) => boolean | string) | boolean | string;
12
12
  /** 是否显示日志, 默认 true */
13
13
  showLog?: boolean;
14
+ /** 处理成功res, 如解密操作 (返回值在成功日志中输出'resFilter'字段) */
15
+ resFilter?: (res: Res) => Res;
16
+ /** 成功和失败时,额外输出的日志数据 (可覆盖内部log参数,如'name') */
17
+ logExtra?: Record<string, unknown>;
14
18
  };
15
19
  /**
16
- * 拓展 web api, 使其支持loading,toast,log,任务对象等能力
20
+ * 拓展 web api, 使其支持loading,toast,log能力
17
21
  * @param webApi web api
18
- * @param apiName web api 名称 (可选, 用于日志输出, 默认'promisifyWebApi')
22
+ * @param apiName web api 名称 (可选, 用于日志输出, 默认'enhanceWebApi')
19
23
  * @return 注入拓展能力的promise (默认提示异常和输出日志,不显示进度条和操作成功)
20
24
  * @example
21
- * const promise = promisifyWebApi(downloadFile, 'downloadFile');
25
+ * const promise = enhanceWebApi(downloadFile, 'downloadFile');
22
26
  * await promise({ url: 'xx' }, {showLoading: '下载中', toastSuccess: '下载成功'});
23
27
  */
24
- export declare function promisifyWebApi<Option, Res, Err, Config = unknown>(webApi: WebApi<Option, Res, Config>, apiName?: string): (option: Option, config?: WebApiConfig<Res, Err> & Config) => Promise<Res>;
28
+ 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
29
  export {};
26
30
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/web/async/index.ts"],"names":[],"mappings":"AAEA,KAAK,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC;AAE/F;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,GAAG,EAAE,GAAG,IAAI;IACnC,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,eAAe,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,EAChE,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"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/web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true } = options.config;
1954
1954
  if (!log || !isLog) return;
1955
1955
  const { config, res, fromCache = false, startTime, status, e } = options;
1956
- const { url, data, header, method, extraLog } = config;
1956
+ const { url, data, header, method, logExtra } = config;
1957
1957
  const endTime = Date.now();
1958
1958
  const fmt = "YYYY-MM-DD HH:mm:ss.SSS";
1959
1959
  const info = __spreadValues({
@@ -1967,7 +1967,7 @@ var baseToolsWeb = (() => {
1967
1967
  startTime: toDayjs(startTime).format(fmt),
1968
1968
  endTime: toDayjs(endTime).format(fmt),
1969
1969
  duration: endTime - startTime
1970
- }, extraLog);
1970
+ }, logExtra);
1971
1971
  if (status === "success") {
1972
1972
  info.res = cloneDeep(res);
1973
1973
  log("info", info);
@@ -2057,14 +2057,16 @@ var baseToolsWeb = (() => {
2057
2057
  }
2058
2058
 
2059
2059
  // src/web/async/index.ts
2060
- function promisifyWebApi(webApi, apiName) {
2060
+ function enhanceWebApi(webApi, apiName) {
2061
2061
  return (option, config) => {
2062
2062
  const finalConfig = config || {};
2063
2063
  const {
2064
2064
  showLoading = false,
2065
2065
  toastSuccess = false,
2066
2066
  toastError = true,
2067
- showLog = true
2067
+ showLog = true,
2068
+ resFilter,
2069
+ logExtra
2068
2070
  } = finalConfig;
2069
2071
  const {
2070
2072
  log,
@@ -2072,7 +2074,7 @@ var baseToolsWeb = (() => {
2072
2074
  showLoading: showLoadingFn,
2073
2075
  hideLoading: hideLoadingFn
2074
2076
  } = getBaseToolsConfig();
2075
- const fname = apiName || "promisifyWebApi";
2077
+ const fname = apiName || "enhanceWebApi";
2076
2078
  if (showLoading) {
2077
2079
  const title = typeof showLoading === "string" ? showLoading : "";
2078
2080
  showLoadingFn == null ? void 0 : showLoadingFn({ title });
@@ -2080,13 +2082,23 @@ var baseToolsWeb = (() => {
2080
2082
  return new Promise((resolve, reject) => {
2081
2083
  webApi(option, finalConfig).then((res) => {
2082
2084
  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;
2085
+ const finalRes = resFilter ? resFilter(res) : res;
2086
+ if (showLog) {
2087
+ const logData = __spreadValues({ name: fname, status: "success", option }, logExtra);
2088
+ if (resFilter) {
2089
+ logData.res = res;
2090
+ logData.resFilter = cloneDeep(finalRes);
2091
+ } else {
2092
+ logData.res = cloneDeep(res);
2093
+ }
2094
+ log == null ? void 0 : log("info", logData);
2095
+ }
2096
+ resolve(finalRes);
2097
+ const msg = typeof toastSuccess === "function" ? toastSuccess(finalRes) : toastSuccess;
2086
2098
  if (msg) toast == null ? void 0 : toast({ msg, status: "success" });
2087
2099
  }).catch((e) => {
2088
2100
  if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
2089
- if (showLog) log == null ? void 0 : log("error", { name: fname, status: "fail", option, e });
2101
+ if (showLog) log == null ? void 0 : log("error", __spreadValues({ name: fname, status: "fail", option, e }, logExtra));
2090
2102
  const msg = typeof toastError === "function" ? toastError(e) : toastError;
2091
2103
  if (msg) {
2092
2104
  toast == null ? void 0 : toast({
@@ -2152,17 +2164,14 @@ var baseToolsWeb = (() => {
2152
2164
  data.append(name, file);
2153
2165
  if (formData) {
2154
2166
  Object.entries(formData).forEach(([k, v]) => {
2155
- if (v !== void 0) data.append(k, String(v));
2167
+ if (v !== void 0 && v !== null) data.append(k, String(v));
2156
2168
  });
2157
2169
  }
2158
2170
  xhr.send(data);
2159
2171
  });
2160
2172
  }
2161
2173
  function uploadFile(option, config) {
2162
- return promisifyWebApi(upload, "uploadFile")(
2163
- option,
2164
- config
2165
- );
2174
+ return enhanceWebApi(upload, "uploadFile")(option, config);
2166
2175
  }
2167
2176
 
2168
2177
  // src/web/storage/index.ts