refactor: 重构ORPC客户端并统一导出路径

- 删除API入口文件中的导出内容
- 更新上下文类型为导入的Context类型并移除注释掉的旧类型定义。
- 添加空的 Context 类型定义以支持上下文类型,暂时忽略复杂度检查警告
- 创建支持服务端和客户端的统一ORPC客户端,基于请求头上下文和Fetch链接实现前后端一致的RPC调用。
- 重构客户端代码,将ORPC客户端初始化逻辑移至独立文件并统一导出,提升代码可维护性和模块化程度。
- 更新导入路径,将 orpc 从 '@/api' 改为 '@/lib/orpc/query-client'。
This commit is contained in:
2026-01-21 16:26:59 +08:00
parent e49e8606da
commit f1608c3546
6 changed files with 30 additions and 31 deletions

View 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()

View File

@@ -0,0 +1,30 @@
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
import { client } from './client'
export const orpc = createTanstackQueryUtils(client, {
experimental_defaults: {
todo: {
create: {
mutationOptions: {
onSuccess: (_, __, ___, ctx) => {
ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() })
},
},
},
update: {
mutationOptions: {
onSuccess: (_, __, ___, ctx) => {
ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() })
},
},
},
remove: {
mutationOptions: {
onSuccess: (_, __, ___, ctx) => {
ctx.client.invalidateQueries({ queryKey: orpc.todo.list.key() })
},
},
},
},
},
})