跳至主要内容
版本:11.x

定义路由器

要开始构建基于 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 本身。这是为了建立一组特定的过程,我们将在代码库中习惯性地使用它们。

定义路由器

接下来,让我们定义一个路由器,其中包含一个过程,供我们在应用程序中使用。我们现在创建了一个 API“端点”。

为了将这些端点暴露给前端,您的 适配器 应该使用您的 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 允许您

您可以使用方法链在初始化时自定义您的 t 对象。例如

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

运行时配置

ts
export interface RootConfig<TTypes extends RootTypes> {
/**
* Use a data transformer
* @link https://trpc.node.org.cn/docs/v11/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @link https://trpc.node.org.cn/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;
}
ts
export interface RootConfig<TTypes extends RootTypes> {
/**
* Use a data transformer
* @link https://trpc.node.org.cn/docs/v11/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @link https://trpc.node.org.cn/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;
}