浏览代码

♻️ refactor(rpc-client): optimize header handling logic

- 重构请求头处理方式,使用显式对象构建requestHeaders
- 修复Headers对象转换问题,确保所有请求头正确传递
- 移除硬编码的Content-Type设置,允许外部灵活指定请求头
yourname 4 月之前
父节点
当前提交
8e3e56402d
共有 1 个文件被更改,包括 9 次插入4 次删除
  1. 9 4
      mini/src/utils/rpc-client.ts

+ 9 - 4
mini/src/utils/rpc-client.ts

@@ -12,15 +12,20 @@ const taroFetch: typeof fetch = async (input, init) => {
   const url = typeof input === 'string' ? input : input.url
   const method = init?.method || 'GET'
   
+  const requestHeaders: Record<string, string> = {};
+
+  if (init?.headers instanceof Headers) {
+    init.headers.forEach((value, key) => {
+      requestHeaders[key] = value;
+    })
+  }
+
   // 构建Taro请求选项
   const options: Taro.request.Option = {
     url,
     method: method as any,
     data: init?.body,
-    header: {
-      'Content-Type': 'application/json',
-      ...Object.fromEntries(new Headers(init?.headers || {}))
-    }
+    header: requestHeaders
   }
 
   // 添加token