@base-web-kits/base-tools-web 1.3.20 → 1.4.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.d.cts CHANGED
@@ -362,6 +362,15 @@ type RequestData = string | ArrayBuffer | ArrayBufferView | Blob | FormData | UR
362
362
  * 响应数据类型
363
363
  */
364
364
  type ResponseData = string | ArrayBuffer | Blob | Record<string, unknown> | unknown[] | null;
365
+ /** 请求前转换上下文 */
366
+ type TransformRequestContext<D extends RequestData = RequestData> = {
367
+ url: string;
368
+ method: RequestMethod;
369
+ header: Record<string, string>;
370
+ data?: D;
371
+ };
372
+ /** 请求前转换结果 */
373
+ type TransformRequestResult<D extends RequestData = RequestData> = Partial<Pick<TransformRequestContext<D>, 'url' | 'header' | 'data'>>;
365
374
  /**
366
375
  * 发起请求的配置 (对外,参数可选)
367
376
  */
@@ -406,8 +415,10 @@ type RequestConfigBase<D extends RequestData = RequestData> = {
406
415
  showLog?: boolean;
407
416
  /** 成功和失败时,额外输出的日志数据 (可覆盖内部log参数,如'name') */
408
417
  logExtra?: Record<string, unknown>;
418
+ /** 请求前的数据转换, 可用于加密 header、data 或重写 url */
419
+ transformRequest?: (ctx: TransformRequestContext<D>) => TransformRequestResult<D> | Promise<TransformRequestResult<D>>;
409
420
  /** 响应数据的转换 */
410
- resMap?: (data: ResponseData) => ResponseData;
421
+ transformResponse?: (data: ResponseData) => ResponseData;
411
422
  /** 获取请求对象, 用于取消请求 */
412
423
  onTaskReady?: (task: RequestTask) => void;
413
424
  /**
@@ -453,7 +464,8 @@ type RequestTask = {
453
464
  * export function requestApi<T>(config: RequestConfig) {
454
465
  * return request<T>({
455
466
  * header: { token: 'xx', version: 'xx', tid: 'xx' }, // 会自动过滤空值
456
- * // resMap: (res) => res, // 响应数据的转换, 如解密操作 (可选)
467
+ * // transformRequest: ({ header, data }) => ({ header, data }), // 请求前转换, 如加密请求头和请求参数 (可选)
468
+ * // transformResponse: (res) => res, // 响应数据的转换, 如解密操作 (可选)
457
469
  * resKey: 'data',
458
470
  * msgKey: 'message',
459
471
  * codeKey: 'status',
@@ -532,8 +544,8 @@ type WebApiConfig<Res = any, Err = any> = {
532
544
  toastError?: ((e: Err) => boolean | string) | boolean | string;
533
545
  /** 是否显示日志, 默认 true */
534
546
  showLog?: boolean;
535
- /** 响应数据的转换, 如解密操作 (返回值在成功日志中输出'resMap'字段) */
536
- resMap?: (res: any) => Res;
547
+ /** 响应数据的转换, 如解密操作 (返回值在成功日志中输出'transformResponse'字段) */
548
+ transformResponse?: (res: any) => Res;
537
549
  /** 成功和失败时,额外输出的日志数据 (可覆盖内部log参数,如'name') */
538
550
  logExtra?: Record<string, unknown>;
539
551
  };
@@ -670,4 +682,4 @@ declare function getUrlNumber(key: string, url?: string): number | null;
670
682
  */
671
683
  declare function getUrlParams(url?: string): Record<string, string>;
672
684
 
673
- export { type AppConfig, type AppLogInfo, type OnUploadProgressUpdate, type RequestConfig, type RequestConfigBase, type RequestData, type RequestMethod, type RequestTask, type ResponseData, type UploadConfig, type UploadFail, type UploadFileOption, type UploadProgressEvent, type UploadTask, copyBlob, copyHtml, copyImage, copyNode, copyRtf, copyTable, copyText, copyUrl, download, filterRequestData, filterRequestHeader, getBaseToolsConfig, getBrowserName, getBrowserVersion, getCookie, getDevicePixelRatio, getDispositionFileName, getLocalStorage, getOS, getUA, getUrlNumber, getUrlParam, getUrlParams, getWindowHeight, getWindowScrollLeft, getWindowScrollTop, getWindowWidth, hasCss, hasJs, isAndroid, isChrome, isIOS, isInViewport, isMobile, isPC, isTablet, isTouchSupported, isWeChat, loadCss, loadJs, lockBodyScroll, preloadImage, removeCookie, removeLocalStorage, request, setBaseToolsConfig, setCookie, setLocalStorage, unlockBodyScroll, uploadFile, windowScrollTo };
685
+ export { type AppConfig, type AppLogInfo, type OnUploadProgressUpdate, type RequestConfig, type RequestConfigBase, type RequestData, type RequestMethod, type RequestTask, type ResponseData, type TransformRequestContext, type TransformRequestResult, type UploadConfig, type UploadFail, type UploadFileOption, type UploadProgressEvent, type UploadTask, copyBlob, copyHtml, copyImage, copyNode, copyRtf, copyTable, copyText, copyUrl, download, filterRequestData, filterRequestHeader, getBaseToolsConfig, getBrowserName, getBrowserVersion, getCookie, getDevicePixelRatio, getDispositionFileName, getLocalStorage, getOS, getUA, getUrlNumber, getUrlParam, getUrlParams, getWindowHeight, getWindowScrollLeft, getWindowScrollTop, getWindowWidth, hasCss, hasJs, isAndroid, isChrome, isIOS, isInViewport, isMobile, isPC, isTablet, isTouchSupported, isWeChat, loadCss, loadJs, lockBodyScroll, preloadImage, removeCookie, removeLocalStorage, request, setBaseToolsConfig, setCookie, setLocalStorage, unlockBodyScroll, uploadFile, windowScrollTo };
package/dist/index.mjs CHANGED
@@ -1983,7 +1983,8 @@ function request(config) {
1983
1983
  toastError = true,
1984
1984
  enableChunked = false,
1985
1985
  cacheTime,
1986
- resMap,
1986
+ transformRequest,
1987
+ transformResponse,
1987
1988
  responseType = "json",
1988
1989
  timeout = 6e4,
1989
1990
  onTaskReady,
@@ -2003,18 +2004,34 @@ function request(config) {
2003
2004
  const execute = () => __async(null, null, function* () {
2004
2005
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
2005
2006
  const isGet = method === "GET";
2006
- const isObjectData = isPlainObject(data);
2007
- const isArrayData = !isObjectData && Array.isArray(data);
2008
- const fillData = isObjectData ? filterRequestData(data) : data;
2009
- const fillHeader = filterRequestHeader(header);
2007
+ let fillData = isPlainObject(data) ? filterRequestData(data) : data;
2008
+ let fillHeader = filterRequestHeader(header);
2009
+ let fillUrl = url;
2010
+ const requestTransformer = transformRequest;
2011
+ if (requestTransformer) {
2012
+ const transformed = (yield requestTransformer({
2013
+ url: fillUrl,
2014
+ method,
2015
+ header: __spreadValues({}, fillHeader),
2016
+ data: fillData
2017
+ })) || {};
2018
+ if (transformed.url !== void 0) fillUrl = transformed.url;
2019
+ if (transformed.header !== void 0) fillHeader = filterRequestHeader(transformed.header);
2020
+ if (transformed.data !== void 0) {
2021
+ const transformedData = transformed.data;
2022
+ fillData = isPlainObject(transformedData) ? filterRequestData(transformedData) : transformedData;
2023
+ }
2024
+ }
2010
2025
  const contentTypeKey = Object.keys(fillHeader).find(
2011
2026
  (k) => k.toLowerCase() === "content-type"
2012
2027
  );
2013
2028
  const contentType = contentTypeKey ? String(fillHeader[contentTypeKey]).toLowerCase() : "";
2029
+ const isObjectData = isPlainObject(fillData);
2030
+ const isArrayData = !isObjectData && Array.isArray(fillData);
2014
2031
  if (!isGet && fillData && (isObjectData || isArrayData) && !contentType) {
2015
2032
  fillHeader["Content-Type"] = "application/json";
2016
2033
  }
2017
- const fillUrl = isGet && isObjectData ? appendUrlParam(url, fillData) : url;
2034
+ fillUrl = isGet && isPlainObject(fillData) ? appendUrlParam(fillUrl, fillData) : fillUrl;
2018
2035
  let fillBody;
2019
2036
  if (!isGet && fillData) {
2020
2037
  if (isObjectData && contentType.includes("application/x-www-form-urlencoded")) {
@@ -2085,7 +2102,7 @@ function request(config) {
2085
2102
  }
2086
2103
  const resData = yield parseResponse(response, responseType);
2087
2104
  if (showLoading) (_d = appConfig2.hideLoading) == null ? void 0 : _d.call(appConfig2);
2088
- const res = resMap ? resMap(resData) : resData;
2105
+ const res = transformResponse ? transformResponse(resData) : resData;
2089
2106
  const code = getObjectValue(res, codeKey);
2090
2107
  const scode = successKey ? getObjectValue(res, successKey) : code;
2091
2108
  const msg = getObjectValue(res, msgKey);
@@ -2273,7 +2290,7 @@ function enhanceWebApi(webApi, apiName) {
2273
2290
  toastSuccess = false,
2274
2291
  toastError = true,
2275
2292
  showLog = true,
2276
- resMap,
2293
+ transformResponse,
2277
2294
  logExtra
2278
2295
  } = finalConfig;
2279
2296
  const {
@@ -2290,12 +2307,12 @@ function enhanceWebApi(webApi, apiName) {
2290
2307
  return new Promise((resolve, reject) => {
2291
2308
  webApi(option, finalConfig).then((res) => {
2292
2309
  if (showLoading) hideLoadingFn == null ? void 0 : hideLoadingFn();
2293
- const finalRes = resMap ? resMap(res) : res;
2310
+ const finalRes = transformResponse ? transformResponse(res) : res;
2294
2311
  if (showLog) {
2295
2312
  const logData = __spreadValues({ name: fname, status: "success", option }, logExtra);
2296
- if (resMap) {
2313
+ if (transformResponse) {
2297
2314
  logData.res = res;
2298
- logData.resMap = cloneDeep(finalRes);
2315
+ logData.transformResponse = cloneDeep(finalRes);
2299
2316
  } else {
2300
2317
  logData.res = cloneDeep(res);
2301
2318
  }