定义路由器
非官方测试版翻译
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
要构建基于 tRPC 的 API,首先需要定义路由器。掌握基础后,你可以自定义路由器满足更高级的用例。
初始化 tRPC
每个应用应仅初始化一次 tRPC。多个 tRPC 实例会导致问题。
server/trpc.tstsimport {initTRPC } from '@trpc/server';// You can use any variable name you like.// We use t to keep things simple.constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;
server/trpc.tstsimport {initTRPC } from '@trpc/server';// You can use any variable name you like.// We use t to keep things simple.constt =initTRPC .create ();export constrouter =t .router ;export constpublicProcedure =t .procedure ;
注意我们导出的是 t 变量的特定方法而非 t 本身,这是为了在代码库中建立一套规范化的过程(procedures)使用模式。
定义路由器
接下来定义包含过程(procedure)的路由器,这样我们就创建了 API 的"端点"。
要使这些端点暴露给前端,需在适配器(Adapter)中配置你的 appRouter 实例。
server/_app.tstsimport {publicProcedure ,router } from './trpc';constappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});// Export only the type of a router!// This prevents us from importing server code on the client.export typeAppRouter = typeofappRouter ;
server/_app.tstsimport {publicProcedure ,router } from './trpc';constappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});// Export only the type of a router!// This prevents us from importing server code on the client.export typeAppRouter = typeofappRouter ;
高级用法
初始化路由器时,tRPC 支持以下操作:
-
为过程分配元数据(metadata)
初始化时可通过方法链(method chaining)定制 t 对象,例如:
tsconst t = initTRPC.context<Context>().meta<Meta>().create({/* [...] */});
tsconst t = initTRPC.context<Context>().meta<Meta>().create({/* [...] */});
运行时配置
tsexport interface RootConfig<TTypes extends RootTypes> {/*** Use a data transformer* @see https://trpc.io/docs/v11/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @see https://trpc.io/docs/v11/error-formatting*/errorFormatter: ErrorFormatter<TTypes['ctx'], any>;/*** Allow `@trpc/server` to run in non-server environments* @warning **Use with caution**, this should likely mainly be used within testing.* @default false*/allowOutsideOfServer: boolean;/*** Is this a server environment?* @warning **Use with caution**, this should likely mainly be used within testing.* @default typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test'*/isServer: boolean;/*** Is this development?* Will be used to decide if the API should return stack traces* @default process.env.NODE_ENV !== 'production'*/isDev: boolean;}
tsexport interface RootConfig<TTypes extends RootTypes> {/*** Use a data transformer* @see https://trpc.io/docs/v11/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @see https://trpc.io/docs/v11/error-formatting*/errorFormatter: ErrorFormatter<TTypes['ctx'], any>;/*** Allow `@trpc/server` to run in non-server environments* @warning **Use with caution**, this should likely mainly be used within testing.* @default false*/allowOutsideOfServer: boolean;/*** Is this a server environment?* @warning **Use with caution**, this should likely mainly be used within testing.* @default typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test'*/isServer: boolean;/*** Is this development?* Will be used to decide if the API should return stack traces* @default process.env.NODE_ENV !== 'production'*/isDev: boolean;}