Преглед изворни кода

♻️ refactor(mini): 优化rpc客户端实现和项目配置

- 修改项目配置,启用ES7特性支持
- 移除rpc-client中多余的可选链操作符
- 修复content-type检查中的空值问题,确保JSON解析安全
yourname пре 4 месеци
родитељ
комит
eca3be6007
2 измењених фајлова са 5 додато и 5 уклоњено
  1. 1 1
      mini/project.config.json
  2. 4 4
      mini/src/utils/rpc-client.ts

+ 1 - 1
mini/project.config.json

@@ -5,7 +5,7 @@
   "appid": "wx6765e6b2fe06378c",
   "setting": {
     "urlCheck": false,
-    "es6": true,
+    "es7": true,
     "enhance": false,
     "compileHotReLoad": false,
     "postcss": false,

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

@@ -10,11 +10,11 @@ const API_BASE_URL = process.env.TARO_APP_API_BASE_URL || 'http://localhost:3000
 // 创建自定义fetch函数,适配Taro.request
 const taroFetch: typeof fetch = async (input, init) => {
   const url = typeof input === 'string' ? input : input.url
-  const method = init?.method || 'GET'
+  const method = init.method || 'GET'
   
   const requestHeaders: Record<string, string> = {};
 
-  if (init?.headers instanceof Headers) {
+  if (init.headers instanceof Headers) {
     init.headers.forEach((value, key) => {
       requestHeaders[key] = value;
     })
@@ -24,7 +24,7 @@ const taroFetch: typeof fetch = async (input, init) => {
   const options: Taro.request.Option = {
     url,
     method: method as any,
-    data: init?.body,
+    data: init.body,
     header: requestHeaders
   }
 
@@ -50,7 +50,7 @@ const taroFetch: typeof fetch = async (input, init) => {
       // 处理204 No Content响应,不设置body
     const body = response.statusCode === 204
     ? null
-    : responseHeaders.get('content-type')?.includes('application/json')
+    : responseHeaders.get('content-type')!.includes('application/json')
       ? JSON.stringify(response.data)
       : response.data;