refactor: 重构RPC合约与客户端类型体系
- 更新客户端类型引用,将APIClient替换为RouterClient以保持类型一致性。 - 删除旧的 RPC 合约定义文件,移除过时的类型和验证逻辑。 - 添加合约入口文件并导出待办事项合约 - 添加待办事项合约,定义列表、创建、更新和删除操作的输入输出验证规则。 - 更新 todo 处理程序以使用统一的合约导入并简化导入路径。 - 调整导出内容,仅导出客户端的orpc函数和类型模块。 - 使用合约类型替换原有路由客户端类型,并引入合约输入输出的类型推断。
This commit is contained in:
5
src/orpc/contracts/index.ts
Normal file
5
src/orpc/contracts/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { todoContract } from './todo'
|
||||
|
||||
export const contract = {
|
||||
todo: todoContract,
|
||||
}
|
||||
45
src/orpc/contracts/todo.ts
Normal file
45
src/orpc/contracts/todo.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { oc } from '@orpc/contract'
|
||||
import {
|
||||
createInsertSchema,
|
||||
createSelectSchema,
|
||||
createUpdateSchema,
|
||||
} from 'drizzle-zod'
|
||||
import { z } from 'zod'
|
||||
import { todoTable } from '@/db/schema'
|
||||
|
||||
const selectSchema = createSelectSchema(todoTable).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
})
|
||||
|
||||
const insertSchema = createInsertSchema(todoTable).omit({
|
||||
id: true,
|
||||
createdAt: true,
|
||||
updatedAt: true,
|
||||
})
|
||||
|
||||
const updateSchema = createUpdateSchema(todoTable)
|
||||
|
||||
export const todoContract = {
|
||||
list: oc.input(z.void()).output(z.array(selectSchema)),
|
||||
|
||||
create: oc.input(insertSchema).output(selectSchema),
|
||||
|
||||
update: oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
data: updateSchema,
|
||||
}),
|
||||
)
|
||||
.output(selectSchema),
|
||||
|
||||
remove: oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
}),
|
||||
)
|
||||
.output(z.void()),
|
||||
}
|
||||
Reference in New Issue
Block a user