メインコンテンツへスキップ
バージョン: 11.x

ルーターの定義

非公式ベータ版翻訳

このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →

tRPCベースのAPIを構築するには、まずルーターを定義する必要があります。基本を習得したら、より高度なユースケースのためにルーターをカスタマイズできます。

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
* @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;
}
ts
export 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;
}