forked from imbytecat/fullstack-starter
refactor: 重构ORPC客户端并统一导出路径
- 删除API入口文件中的导出内容 - 更新上下文类型为导入的Context类型并移除注释掉的旧类型定义。 - 添加空的 Context 类型定义以支持上下文类型,暂时忽略复杂度检查警告 - 创建支持服务端和客户端的统一ORPC客户端,基于请求头上下文和Fetch链接实现前后端一致的RPC调用。 - 重构客户端代码,将ORPC客户端初始化逻辑移至独立文件并统一导出,提升代码可维护性和模块化程度。 - 更新导入路径,将 orpc 从 '@/api' 改为 '@/lib/orpc/query-client'。
This commit is contained in:
24
apps/server/src/lib/orpc/client.ts
Normal file
24
apps/server/src/lib/orpc/client.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { createORPCClient } from '@orpc/client'
|
||||
import { RPCLink } from '@orpc/client/fetch'
|
||||
import { createRouterClient } from '@orpc/server'
|
||||
import { createIsomorphicFn } from '@tanstack/react-start'
|
||||
import { getRequestHeaders } from '@tanstack/react-start/server'
|
||||
import { router } from '@/api/routers'
|
||||
import type { RouterClient } from '@/api/types'
|
||||
|
||||
const getORPCClient = createIsomorphicFn()
|
||||
.server(() =>
|
||||
createRouterClient(router, {
|
||||
context: () => ({
|
||||
headers: getRequestHeaders(),
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.client(() => {
|
||||
const link = new RPCLink({
|
||||
url: `${window.location.origin}/api/rpc`,
|
||||
})
|
||||
return createORPCClient<RouterClient>(link)
|
||||
})
|
||||
|
||||
export const client: RouterClient = getORPCClient()
|
||||
Reference in New Issue
Block a user