跳至主要內容
版本: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「端點」。

為了讓這些端點公開到前端,您的 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 允許您

您可以在初始化時使用方法鏈來自訂您的 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.dev.org.tw/docs/v11/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @link https://trpc.dev.org.tw/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.dev.org.tw/docs/v11/data-transformers
*/
transformer: TTypes['transformer'];
/**
* Use custom error formatting
* @link https://trpc.dev.org.tw/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;
}