Define Routers
Initialize tRPC​
tip
- If you don't like the variable name t, you can call it whatever you want
- You should create your root t-variable exactly once per application
- You can also create the t-variable with a context, metadata, a error formatter, or a data transformer.
- It's good to limit the methods you export from the tobject in order to constrain your team to use only a few base procedures
server/trpc.tstsinitTRPC } from '@trpc/server';Âconstt =initTRPC .create ();Â// We explicitly export the methods we use here// This allows us to create reusable & protected base proceduresexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;
server/trpc.tstsinitTRPC } from '@trpc/server';Âconstt =initTRPC .create ();Â// We explicitly export the methods we use here// This allows us to create reusable & protected base proceduresexport constmiddleware =t .middleware ;export constrouter =t .router ;export constpublicProcedure =t .procedure ;
Defining a router​
server/_app.tststrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';ÂconstappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});Â// Export only the **type** of a router to avoid importing server code on the clientexport typeAppRouter = typeofappRouter ;
server/_app.tststrpc from '@trpc/server';import {publicProcedure ,router } from './trpc';ÂconstappRouter =router ({greeting :publicProcedure .query (() => 'hello tRPC v10!'),});Â// Export only the **type** of a router to avoid importing server code on the clientexport typeAppRouter = typeofappRouter ;
initTRPC options​
Use chaining to setup your t-object, example:
ts
ts
.context<Context>()​
Setup a request context.
.meta<Meta>()​
Setup metadata for your procedures.
.create(opts: Partial<RuntimeConfig>)​
RuntimeConfig reference:
ts
ts