Explorar el Código

fix: 修复 RPC 客户端类型安全并增强 ResponsePolyfill 接口

- 修复 response-polyfill.ts 中的 @ts-expect-error 未使用错误
  - 移除不必要的 @ts-expect-error 指令,重构 Headers.raw() 类型检查逻辑
  - 使用中间变量 headersWithRaw 改善类型安全性和代码可读性

- 修复 rpc-client.ts 中的 Hono 泛型约束类型错误
  - 将 Hono<unknown, unknown, unknown> 简化为 Hono 基类型
  - 移除不必要的 Schema 类型导入

Co-Authored-By: Claude <noreply@anthropic.com>
yourname hace 1 día
padre
commit
2d2f0de22e

+ 5 - 4
mini-ui-packages/mini-shared-ui-components/src/utils/rpc/response-polyfill.ts

@@ -103,9 +103,10 @@ export class ResponsePolyfill {
     if (!headers.has('Content-Type')) {
       headers.set('Content-Type', 'application/json')
     }
-    // @ts-expect-error - Headers.raw() 是非标准 API,但在小程序环境中需要使用
-    const headersRecord = typeof (headers as { raw?: () => Record<string, string> }).raw === 'function'
-      ? (headers as { raw: () => Record<string, string> }).raw()
+    // Headers.raw() 是非标准 API,某些实现(如 node-fetch)提供此方法
+    const headersWithRaw = headers as { raw?: () => Record<string, string> }
+    const headersRecord = typeof headersWithRaw.raw === 'function'
+      ? headersWithRaw.raw()
       : {}
     return new ResponsePolyfill(JSON.stringify(data), {
       ...init,
@@ -131,4 +132,4 @@ export function registerGlobalResponsePolyfill(): void {
     // @ts-expect-error - 全局类型赋值
     globalThis.Response = ResponsePolyfill
   }
-}
+}

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

@@ -193,8 +193,7 @@ const taroFetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<
   }
 }
 
-// 创建Hono RPC客户端
-export const rpcClient = <T extends Hono<unknown, unknown, unknown>>(apiBasePath?: string): ReturnType<typeof hc<T>> => {
+export const rpcClient = <T extends Hono>(apiBasePath?: string): ReturnType<typeof hc<T>> => {
   return hc<T>(`${API_BASE_URL}${apiBasePath}`, {
     fetch: taroFetch
   })