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,31 +1,10 @@
import { ORPCError, os } from '@orpc/server'
import { implement, ORPCError } from '@orpc/server'
import { eq } from 'drizzle-orm'
import {
createInsertSchema,
createSelectSchema,
createUpdateSchema,
} from 'drizzle-zod'
import { z } from 'zod'
import { todoTable } from '@/db/schema'
import { todoContract } from '@/orpc/contract'
import { dbProvider } from '@/orpc/middlewares'
const selectSchema = createSelectSchema(todoTable)
const insertSchema = createInsertSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
const updateSchema = createUpdateSchema(todoTable).omit({
id: true,
createdAt: true,
updatedAt: true,
})
export const list = os
.input(z.void())
.output(z.array(selectSchema))
export const list = implement(todoContract.list)
.use(dbProvider)
.handler(async ({ context }) => {
const todos = await context.db.query.todoTable.findMany({
@@ -34,9 +13,7 @@ export const list = os
return todos
})
export const create = os
.input(insertSchema)
.output(selectSchema)
export const create = implement(todoContract.create)
.use(dbProvider)
.handler(async ({ context, input }) => {
const [newTodo] = await context.db
@@ -51,14 +28,7 @@ export const create = os
return newTodo
})
export const update = os
.input(
z.object({
id: z.uuid(),
data: updateSchema,
}),
)
.output(selectSchema)
export const update = implement(todoContract.update)
.use(dbProvider)
.handler(async ({ context, input }) => {
const [updatedTodo] = await context.db
@@ -74,13 +44,7 @@ export const update = os
return updatedTodo
})
export const remove = os
.input(
z.object({
id: z.uuid(),
}),
)
.output(z.void())
export const remove = implement(todoContract.remove)
.use(dbProvider)
.handler(async ({ context, input }) => {
await context.db.delete(todoTable).where(eq(todoTable.id, input.id))