Przeglądaj źródła

♻️ refactor(client): 提取axios适配器到独立工具文件

- 将axiosFetch函数从api.ts移动到新文件utils/axios-fetch.ts
- 通过导入使用外部化的axiosFetch函数,优化代码组织结构
- 保持原有功能不变,提高代码可维护性和复用性
yourname 3 miesięcy temu
rodzic
commit
fbb31537f3
2 zmienionych plików z 57 dodań i 56 usunięć
  1. 2 56
      src/client/api.ts
  2. 55 0
      src/client/utils/axios-fetch.ts

+ 2 - 56
src/client/api.ts

@@ -1,64 +1,10 @@
-import axios, { isAxiosError } from 'axios';
+
 import { hc } from 'hono/client'
 import type {
   AuthRoutes, UserRoutes, RoleRoutes,
   FileRoutes, MembershipPlanRoutes
 } from '@/server/api';
-
-// 创建 axios 适配器
-const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
-  const requestHeaders:Record<string, string> = {};
-  
-  if(init?.headers instanceof Headers) {
-    init.headers.forEach((value, key) => {
-      requestHeaders[key] = value;
-    })
-  }
-
-  const response = await axios.request({  
-    url: url.toString(),  
-    method: init?.method || 'GET',  
-    headers: requestHeaders,  
-    data: init?.body,  
-  }).catch((error) => {
-    console.log('axiosFetch error', error)
-    
-    if(isAxiosError(error)) {
-      return {
-        status: error.response?.status,
-        statusText: error.response?.statusText,
-        data: error.response?.data,
-        headers: error.response?.headers
-      }
-    }
-    throw error;
-  }) 
-
-  const responseHeaders = new Headers();
-  if (response.headers) {
-    for (const [key, value] of Object.entries(response.headers)) {
-      responseHeaders.set(key, value);
-    }
-  }
-    
-  
-  // 处理204 No Content响应,不设置body
-  const body = response.status === 204
-    ? null
-    : responseHeaders.get('content-type')?.includes('application/json')
-      ? JSON.stringify(response.data)
-      : response.data;
-  
-  return new Response(
-    body,
-    {
-      status: response.status,
-      statusText: response.statusText,
-      headers: responseHeaders
-    }
-  )
-}  
-
+import { axiosFetch } from './utils/axios-fetch';
 
 export const authClient = hc<AuthRoutes>('/', {
   fetch: axiosFetch,

+ 55 - 0
src/client/utils/axios-fetch.ts

@@ -0,0 +1,55 @@
+import axios, { isAxiosError } from 'axios';
+
+// 创建 axios 适配器
+export const axiosFetch = async (url: RequestInfo | URL, init?: RequestInit) => {
+    const requestHeaders:Record<string, string> = {};
+    
+    if(init?.headers instanceof Headers) {
+      init.headers.forEach((value, key) => {
+        requestHeaders[key] = value;
+      })
+    }
+  
+    const response = await axios.request({  
+      url: url.toString(),  
+      method: init?.method || 'GET',  
+      headers: requestHeaders,  
+      data: init?.body,  
+    }).catch((error) => {
+      console.log('axiosFetch error', error)
+      
+      if(isAxiosError(error)) {
+        return {
+          status: error.response?.status,
+          statusText: error.response?.statusText,
+          data: error.response?.data,
+          headers: error.response?.headers
+        }
+      }
+      throw error;
+    }) 
+  
+    const responseHeaders = new Headers();
+    if (response.headers) {
+      for (const [key, value] of Object.entries(response.headers)) {
+        responseHeaders.set(key, value);
+      }
+    }
+      
+    
+    // 处理204 No Content响应,不设置body
+    const body = response.status === 204
+      ? null
+      : responseHeaders.get('content-type')?.includes('application/json')
+        ? JSON.stringify(response.data)
+        : response.data;
+    
+    return new Response(
+      body,
+      {
+        status: response.status,
+        statusText: response.statusText,
+        headers: responseHeaders
+      }
+    )
+  }