跳至主内容
版本:10.x

定义路由器

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

要构建基于 tRPC 的 API,首先需要定义路由器。掌握基础后,你可以自定义路由器满足更高级的用例。

初始化 tRPC

每个应用应仅初始化一次 tRPC。多个 tRPC 实例会导致问题。

server/trpc.ts
ts
import { initTRPC } from '@trpc/server';
 
// You can use any variable name you like.
// We use t to keep things simple.
const t = initTRPC.create();
 
export const router = t.router;
export const publicProcedure = t.procedure;
server/trpc.ts
ts
import { initTRPC } from '@trpc/server';
 
// You can use any variable name you like.
// We use t to keep things simple.
const t = initTRPC.create();
 
export const router = t.router;
export const publicProcedure = t.procedure;

注意我们导出的是 t 变量的特定方法而非 t 本身,这是为了在代码库中建立一套规范化的过程(procedures)使用模式。

定义路由器

接下来定义包含过程(procedure)的路由器,这样我们就创建了 API 的"端点"。

要使这些端点暴露给前端,需在适配器(Adapter)中配置你的 appRouter 实例。

server/_app.ts
ts
import { publicProcedure, router } from './trpc';
 
const appRouter = 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 type AppRouter = typeof appRouter;
server/_app.ts
ts
import { publicProcedure, router } from './trpc';
 
const appRouter = 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 type AppRouter = typeof appRouter;

高级用法

初始化路由器时,tRPC 支持以下操作:

初始化时可通过方法链(method chaining)定制 t 对象,例如:

ts
const t = initTRPC.context<Context>().meta<Meta>().create({
/* [...] */
});
ts
const t = initTRPC.context<Context>().meta<Meta>().create({
/* [...] */
});

运行时配置

ts
export interface RuntimeConfig<TTypes extends RootConfigTypes> {
/**
* Use a data transformer
* @see https://trpc.io/docs/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @see https://trpc.io/docs/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;
}
ts
export interface RuntimeConfig<TTypes extends RootConfigTypes> {
/**
* Use a data transformer
* @see https://trpc.io/docs/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @see https://trpc.io/docs/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;
}