ルーターの定義
tRPC ベースの API の構築を開始するには、最初にルーターを定義する必要があります。基本をマスターしたら、より高度なユースケースに対応してルーターを カスタマイズ できます。
tRPC の初期化
tRPC はアプリケーションごとに 1 回だけ初期化する必要があります。tRPC のインスタンスが複数あると問題が発生します。
server/trpc.tsts
import {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.tsts
import {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.tsts
import {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.tsts
import {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
オブジェクトをカスタマイズできます。次のように
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.dokyumento.jp/docs/v11/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @link https://trpc.dokyumento.jp/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.dokyumento.jp/docs/v11/data-transformers*/transformer: TTypes['transformer'];/*** Use custom error formatting* @link https://trpc.dokyumento.jp/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;}