Преглед на файлове

feat(axios): Do you want to return the original response header? For example, use this property when you need to get the response header

zuihou преди 4 години
родител
ревизия
56d8af147e
променени са 2 файла, в които са добавени 9 реда и са изтрити 1 реда
  1. 7 1
      src/utils/http/axios/index.ts
  2. 2 0
      src/utils/http/axios/types.ts

+ 7 - 1
src/utils/http/axios/index.ts

@@ -35,7 +35,11 @@ const transform: AxiosTransform = {
    */
   transformRequestHook: (res: AxiosResponse<Result>, options: RequestOptions) => {
     const { t } = useI18n();
-    const { isTransformRequestResult } = options;
+    const { isTransformRequestResult, isReturnNativeResponse } = options;
+    // 是否返回原生响应头 比如:需要获取响应头时使用该属性
+    if (isReturnNativeResponse) {
+      return res;
+    }
     // 不进行任何处理,直接返回
     // 用于页面代码可能需要直接获取code,data,message这些信息时开启
     if (!isTransformRequestResult) {
@@ -192,6 +196,8 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) {
         requestOptions: {
           // 默认将prefix 添加到url
           joinPrefix: true,
+          // 是否返回原生响应头 比如:需要获取响应头时使用该属性
+          isReturnNativeResponse: false,
           // 需要对返回数据进行处理
           isTransformRequestResult: true,
           // post请求的时候添加参数到url

+ 2 - 0
src/utils/http/axios/types.ts

@@ -7,6 +7,8 @@ export interface RequestOptions {
   formatDate?: boolean;
   //  Whether to process the request result
   isTransformRequestResult?: boolean;
+  // 是否返回原生响应头 比如:需要获取响应头时使用该属性
+  isReturnNativeResponse?: boolean;
   // Whether to join url
   joinPrefix?: boolean;
   // Interface address, use the default apiUrl if you leave it blank