Selaa lähdekoodia

✨ feat(rpc-client): 添加辅助函数以不区分大小写获取响应头

- 新增 `getHeaderValue` 辅助函数,用于从响应头中不区分大小写地获取指定键的值
- 在 `taroFetch` 函数中使用新辅助函数获取 `content-type` 头,提高代码健壮性
- 添加 API 响应日志输出,便于调试
yourname 1 viikko sitten
vanhempi
sitoutus
5350962c85

+ 11 - 1
mini-ui-packages/mini-shared-ui-components/src/utils/rpc/rpc-client.ts

@@ -73,6 +73,14 @@ const refreshToken = async (): Promise<string | null> => {
   }
 }
 
+// 辅助函数:获取 header 值(大小写不敏感)
+const getHeaderValue = (headers: Record<string, string>, key: string): string | undefined => {
+  const lowerKey = key.toLowerCase()
+  return Object.keys(headers).find(k => k.toLowerCase() === lowerKey)?.[0] !== undefined
+    ? headers[Object.keys(headers).find(k => k.toLowerCase() === lowerKey)!]
+    : undefined
+}
+
 // API配置
 const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000'
 
@@ -123,13 +131,15 @@ const taroFetch: any = async (input, init) => {
     try {
       console.log('API请求:', options.url)
       const response = await Taro.request(options)
+      console.log('API响应', response)
 
       const responseHeaders = response.header;
+      const contentType = getHeaderValue(responseHeaders, 'content-type')
 
       // 处理204 No Content响应,不设置body
       const body = response.statusCode === 204
         ? null
-        : responseHeaders['content-type']!.includes('application/json')
+        : contentType?.includes('application/json')
           ? JSON.stringify(response.data)
           : response.data;