ルーターの定義
非公式ベータ版翻訳
このページは 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 v11!'),});// 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 v11!'),});// Export only the type of a router!// This prevents us from importing server code on the client.export typeAppRouter = typeofappRouter ;
インラインサブルーターの定義
インラインサブルーターを定義する場合、ルーターをプレーンオブジェクトとして表現できます。
以下の例では、nested1 と nested2 は同等です:
server/_app.tstsimport * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';constappRouter =router ({// Using the router() methodnested1 :router ({proc :publicProcedure .query (() => '...'),}),// Using an inline sub-routernested2 : {proc :publicProcedure .query (() => '...'),},});
server/_app.tstsimport * astrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';constappRouter =router ({// Using the router() methodnested1 :router ({proc :publicProcedure .query (() => '...'),}),// Using an inline sub-routernested2 : {proc :publicProcedure .query (() => '...'),},});
高度な使用方法
ルーターを初期化する際、tRPCでは以下の操作が可能です:
メソッドチェーンを使用して初期化時のtオブジェクトをカスタマイズできます。例:
tsconstt =initTRPC .context <Context >().meta <Meta >().create ({/* [...] */});
tsconstt =initTRPC .context <Context >().meta <Meta >().create ({/* [...] */});
ランタイム設定
tsinterfaceRootConfig {/*** Use a data transformer* @see https://trpc.io/docs/v11/data-transformers*/transformer :DataTransformerOptions ;/*** Use custom error formatting* @see https://trpc.io/docs/v11/error-formatting*/errorFormatter :ErrorFormatter ;/*** 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;}
tsinterfaceRootConfig {/*** Use a data transformer* @see https://trpc.io/docs/v11/data-transformers*/transformer :DataTransformerOptions ;/*** Use custom error formatting* @see https://trpc.io/docs/v11/error-formatting*/errorFormatter :ErrorFormatter ;/*** 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;}