feat: 使用 zod 合约实现类型安全的 todo 服务

- 添加 @orpc/contract 依赖以支持合约定义和类型安全。
- 添加 @orpc/contract 依赖以支持契约定义和类型安全。
- 更新客户端类型定义并移除冗余的 APIRouterClient 引入,确保客户端实例类型与路由定义一致。
- 添加基于 zod 的类型安全接口定义,包含待办事项的增删改查操作契约及对应的输入输出验证规则。
- 使用合约定义重构 Todo 处理函数,统一接口输入输出验证并移除冗余的 Zod 模式定义。
- 更新导出模块,将路由功能改为导出合约定义。
- 移除未使用的导入和类型定义,精简路由配置文件。
This commit is contained in:
2026-01-18 03:20:05 +08:00
parent 26c50acdf6
commit f0ae8196cd
7 changed files with 63 additions and 51 deletions

View File

@@ -1,10 +1,10 @@
import { createORPCClient } from '@orpc/client'
import { RPCLink } from '@orpc/client/fetch'
import { createRouterClient } from '@orpc/server'
import { createRouterClient, type RouterClient } from '@orpc/server'
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
import { createIsomorphicFn } from '@tanstack/react-start'
import { getRequestHeaders } from '@tanstack/react-start/server'
import { type APIRouterClient, router } from '@/orpc'
import { router } from './router'
const getORPCClient = createIsomorphicFn()
.server(() =>
@@ -14,13 +14,13 @@ const getORPCClient = createIsomorphicFn()
}),
}),
)
.client(() => {
.client((): RouterClient<typeof router> => {
const link = new RPCLink({
url: `${window.location.origin}/api/rpc`,
})
return createORPCClient<APIRouterClient>(link)
return createORPCClient(link)
})
const client: APIRouterClient = getORPCClient()
const client: RouterClient<typeof router> = getORPCClient()
export const orpc = createTanstackQueryUtils(client)