forked from imbytecat/fullstack-starter
refactor: 重构RPC合约与客户端类型体系
- 更新客户端类型引用,将APIClient替换为RouterClient以保持类型一致性。 - 删除旧的 RPC 合约定义文件,移除过时的类型和验证逻辑。 - 添加合约入口文件并导出待办事项合约 - 添加待办事项合约,定义列表、创建、更新和删除操作的输入输出验证规则。 - 更新 todo 处理程序以使用统一的合约导入并简化导入路径。 - 调整导出内容,仅导出客户端的orpc函数和类型模块。 - 使用合约类型替换原有路由客户端类型,并引入合约输入输出的类型推断。
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { createORPCClient } from '@orpc/client'
|
||||
import { RPCLink } from '@orpc/client/fetch'
|
||||
import { createRouterClient, type RouterClient } from '@orpc/server'
|
||||
import { createRouterClient } from '@orpc/server'
|
||||
import { createTanstackQueryUtils } from '@orpc/tanstack-query'
|
||||
import { createIsomorphicFn } from '@tanstack/react-start'
|
||||
import { getRequestHeaders } from '@tanstack/react-start/server'
|
||||
import { router } from './router'
|
||||
import type { APIClient } from './types'
|
||||
import type { RouterClient } from './types'
|
||||
|
||||
const getORPCClient = createIsomorphicFn()
|
||||
.server(() =>
|
||||
@@ -19,9 +19,9 @@ const getORPCClient = createIsomorphicFn()
|
||||
const link = new RPCLink({
|
||||
url: `${window.location.origin}/api/rpc`,
|
||||
})
|
||||
return createORPCClient<APIClient>(link)
|
||||
return createORPCClient<RouterClient>(link)
|
||||
})
|
||||
|
||||
const client: APIClient = getORPCClient()
|
||||
const client: RouterClient = getORPCClient()
|
||||
|
||||
export const orpc = createTanstackQueryUtils(client)
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
import { oc } from '@orpc/contract'
|
||||
import { z } from 'zod'
|
||||
|
||||
export const todoSchema = z.object({
|
||||
id: z.uuid(),
|
||||
title: z.string(),
|
||||
completed: z.boolean(),
|
||||
createdAt: z.date(),
|
||||
updatedAt: z.date(),
|
||||
})
|
||||
|
||||
export const todoInsertSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
completed: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const todoUpdateSchema = z.object({
|
||||
title: z.string().min(1).optional(),
|
||||
completed: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const todoContract = {
|
||||
list: oc.input(z.void()).output(z.array(todoSchema)),
|
||||
|
||||
create: oc.input(todoInsertSchema).output(todoSchema),
|
||||
|
||||
update: oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
data: todoUpdateSchema,
|
||||
}),
|
||||
)
|
||||
.output(todoSchema),
|
||||
|
||||
remove: oc
|
||||
.input(
|
||||
z.object({
|
||||
id: z.uuid(),
|
||||
}),
|
||||
)
|
||||
.output(z.void()),
|
||||
}
|
||||
|
||||
export const contract = {
|
||||
todo: todoContract,
|
||||
}
|
||||
|
||||
export type Contract = typeof contract
|
||||
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()),
|
||||
}
|
||||
@@ -1,10 +1,10 @@
|
||||
import { implement, ORPCError } from '@orpc/server'
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { todoTable } from '@/db/schema'
|
||||
import { todoContract } from '@/orpc/contract'
|
||||
import { dbProvider } from '@/orpc/middlewares'
|
||||
import { contract } from '../contracts'
|
||||
|
||||
export const list = implement(todoContract.list)
|
||||
export const list = implement(contract.todo.list)
|
||||
.use(dbProvider)
|
||||
.handler(async ({ context }) => {
|
||||
const todos = await context.db.query.todoTable.findMany({
|
||||
@@ -13,7 +13,7 @@ export const list = implement(todoContract.list)
|
||||
return todos
|
||||
})
|
||||
|
||||
export const create = implement(todoContract.create)
|
||||
export const create = implement(contract.todo.create)
|
||||
.use(dbProvider)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [newTodo] = await context.db
|
||||
@@ -28,7 +28,7 @@ export const create = implement(todoContract.create)
|
||||
return newTodo
|
||||
})
|
||||
|
||||
export const update = implement(todoContract.update)
|
||||
export const update = implement(contract.todo.update)
|
||||
.use(dbProvider)
|
||||
.handler(async ({ context, input }) => {
|
||||
const [updatedTodo] = await context.db
|
||||
@@ -44,7 +44,7 @@ export const update = implement(todoContract.update)
|
||||
return updatedTodo
|
||||
})
|
||||
|
||||
export const remove = implement(todoContract.remove)
|
||||
export const remove = implement(contract.todo.remove)
|
||||
.use(dbProvider)
|
||||
.handler(async ({ context, input }) => {
|
||||
await context.db.delete(todoTable).where(eq(todoTable.id, input.id))
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
export * from './client'
|
||||
export * from './contract'
|
||||
export { orpc } from './client'
|
||||
export * from './types'
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import type { RouterClient } from '@orpc/server'
|
||||
import type { router } from './router'
|
||||
import type {
|
||||
ContractRouterClient,
|
||||
InferContractRouterInputs,
|
||||
InferContractRouterOutputs,
|
||||
} from '@orpc/contract'
|
||||
import type { contract } from './contracts'
|
||||
|
||||
export type APIClient = RouterClient<typeof router>
|
||||
export type Contract = typeof contract
|
||||
export type RouterClient = ContractRouterClient<Contract>
|
||||
export type RouterInputs = InferContractRouterInputs<Contract>
|
||||
export type RouterOutputs = InferContractRouterOutputs<Contract>
|
||||
|
||||
Reference in New Issue
Block a user