@base-web-kits/base-tools-web 1.3.20 → 1.4.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.
package/dist/index.cjs CHANGED
@@ -2046,7 +2046,8 @@ function request(config) {
2046
2046
  toastError = true,
2047
2047
  enableChunked = false,
2048
2048
  cacheTime,
2049
- resMap,
2049
+ transformRequest,
2050
+ transformResponse,
2050
2051
  responseType = "json",
2051
2052
  timeout = 6e4,
2052
2053
  onTaskReady,
@@ -2066,18 +2067,34 @@ function request(config) {
2066
2067
  const execute = () => __async(null, null, function* () {
2067
2068
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2068
2069
  const isGet = method === "GET";
2069
- const isObjectData = isPlainObject(data);
2070
- const isArrayData = !isObjectData && Array.isArray(data);
2071
- const fillData = isObjectData ? filterRequestData(data) : data;
2072
- const fillHeader = filterRequestHeader(header);
2070
+ let fillData = isPlainObject(data) ? filterRequestData(data) : data;
2071
+ let fillHeader = filterRequestHeader(header);
2072
+ let fillUrl = url;
2073
+ const requestTransformer = transformRequest;
2074
+ if (requestTransformer) {
2075
+ const transformed = (yield requestTransformer({
2076
+ url: fillUrl,
2077
+ method,
2078
+ header: __spreadValues({}, fillHeader),
2079
+ data: fillData
2080
+ })) || {};
2081
+ if (transformed.url !== void 0) fillUrl = transformed.url;
2082
+ if (transformed.header !== void 0) fillHeader = filterRequestHeader(transformed.header);
2083
+ if (transformed.data !== void 0) {
2084
+ const transformedData = transformed.data;
2085
+ fillData = isPlainObject(transformedData) ? filterRequestData(transformedData) : transformedData;
2086
+ }
2087
+ }
2073
2088
  const contentTypeKey = Object.keys(fillHeader).find(
2074
2089
  (k) => k.toLowerCase() === "content-type"
2075
2090
  );
2076
2091
  const contentType = contentTypeKey ? String(fillHeader[contentTypeKey]).toLowerCase() : "";
2092
+ const isObjectData = isPlainObject(fillData);
2093
+ const isArrayData = !isObjectData && Array.isArray(fillData);
2077
2094
  if (!isGet && fillData && (isObjectData || isArrayData) && !contentType) {
2078
2095
  fillHeader["Content-Type"] = "application/json";
2079
2096
  }
2080
- const fillUrl = isGet && isObjectData ? appendUrlParam(url, fillData) : url;
2097
+ fillUrl = isGet && isPlainObject(fillData) ? appendUrlParam(fillUrl, fillData) : fillUrl;
2081
2098
  let fillBody;
2082
2099
  if (!isGet && fillData) {
2083
2100
  if (isObjectData && contentType.includes("application/x-www-form-urlencoded")) {
@@ -2148,7 +2165,7 @@ function request(config) {
2148
2165
  }
2149
2166
  const resData = yield parseResponse(response, responseType);
2150
2167
  if (showLoading) (_d = appConfig2.hideLoading) == null ? void 0 : _d.call(appConfig2);
2151
- const res = resMap ? resMap(resData) : resData;
2168
+ const res = transformResponse ? transformResponse(resData) : resData;
2152
2169
  const code = getObjectValue(res, codeKey);
2153
2170
  const scode = successKey ? getObjectValue(res, successKey) : code;
2154
2171
  const msg = getObjectValue(res, msgKey);
@@ -2177,7 +2194,10 @@ function request(config) {
2177
2194
  return;
2178
2195
  }
2179
2196
  if (!isAbortError && toastError)
2180
- (_i = appConfig2.toast) == null ? void 0 : _i.call(appConfig2, { status, msg: `\u8BF7\u6C42\u5931\u8D25,${JSON.stringify(e)}` });
2197
+ (_i = appConfig2.toast) == null ? void 0 : _i.call(appConfig2, {
2198
+ status,
2199
+ msg: `\u8BF7\u6C42\u5931\u8D25: ${e instanceof Error ? e.message : JSON.stringify(e)}`
2200
+ });
2181
2201
  logRequestInfo({ status, config: logConfig, startTime, e });
2182
2202
  reject(e);
2183
2203
  } finally {
@@ -2336,7 +2356,7 @@ function enhanceWebApi(webApi, apiName) {
2336
2356
  toastSuccess = false,
2337
2357
  toastError = true,
2338
2358
  showLog = true,
2339
- resMap,
2359
+ transformResponse,
2340
2360
  logExtra
2341
2361
  } = finalConfig;
2342
2362
  const {
@@ -2353,12 +2373,12 @@ function enhanceWebApi(webApi, apiName) {
2353
2373
  return new Promise((resolve, reject) => {
2354
2374
  webApi(option, finalConfig).then((res) => {
2355
2375
  if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
2356
- const finalRes = resMap ? resMap(res) : res;
2376
+ const finalRes = transformResponse ? transformResponse(res) : res;
2357
2377
  if (showLog) {
2358
2378
  const logData = __spreadValues({ name: fname, status: "success", option }, logExtra);
2359
- if (resMap) {
2379
+ if (transformResponse) {
2360
2380
  logData.res = res;
2361
- logData.resMap = cloneDeep(finalRes);
2381
+ logData.transformResponse = cloneDeep(finalRes);
2362
2382
  } else {
2363
2383
  logData.res = cloneDeep(res);
2364
2384
  }
@@ -2373,7 +2393,7 @@ function enhanceWebApi(webApi, apiName) {
2373
2393
  const msg = typeof toastError === "function" ? toastError(e) : toastError;
2374
2394
  if (msg) {
2375
2395
  toast == null ? void 0 : toast({
2376
- msg: typeof msg === "string" ? msg : `${fname} fail: ${JSON.stringify(e)}`,
2396
+ msg: typeof msg === "string" ? msg : `${fname} fail: ${e instanceof Error ? e.message : JSON.stringify(e)}`,
2377
2397
  status: "fail"
2378
2398
  });
2379
2399
  }