ルーターの定義
非公式ベータ版翻訳
このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →
tRPCベースのAPIを構築するには、まずルーターを定義する必要があります。基本を習得したら、より高度なユースケースのためにルーターをカスタマイズできます。
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自体の特定のメソッドをエクスポートしていることに注意してください。これはコードベースで慣用的に使用する手順セットを確立するためです。
ルーターの定義
次に、アプリケーションで使用するプロシージャを持つルーターを定義しましょう。これでAPI「エンドポイント」が作成されました。
これらのエンドポイントをフロントエンドに公開するには、アダプターを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では以下の操作が可能です:
メソッドチェーンを使用して初期化時のtオブジェクトをカスタマイズできます。例:
tsconst t = initTRPC.context<Context>().meta<Meta>().create({/* [...] */});
tsconst t = initTRPC.context<Context>().meta<Meta>().create({/* [...] */});
ランタイム設定
tsexport 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;}
tsexport 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;}