tRpc - Move Fast and Break Nothing. End-to-end typesafe APIs made easy. Experience the full power of TypeScript inference to boost productivity for your NUXT full-stack application.
This blog is a companion to the video walkthrough of getting a Nuxt 3 application up and running with tRpc using the trpc-nuxt module.
This is the first video in the series, additional content will be created for you to follow along and build a Full-Stack Typescript Application with Nuxt and tRPC.
Entrypoint into trpc server; the embedded documentation explains what is going on.
// server/trpc/trpc.ts/**
* This is your entry point to setup the root configuration for tRPC on the server.
* - `initTRPC` should only be used once per app.
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @see https://trpc.io/docs/v10/router
* @see https://trpc.io/docs/v10/procedures
*/import{initTRPC}from'@trpc/server'import{Context}from'../trpc/context';constt=initTRPC.context<Context>().create();/**
* Unprotected procedure
**/exportconstpublicProcedure=t.procedure;exportconstrouter=t.router;exportconstmiddleware=t.middleware;
The context, this is where you can put things that you want all routes to have access to. We will be putting out database client here so we can access it through the context in our routes
// server/trpc/context.tsimport{inferAsyncReturnType}from'@trpc/server';/**
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/exportconstcreateContext=()=>({});exporttypeContext=inferAsyncReturnType<typeofcreateContext>;
The routes, the can be broken up into separate files so we will place an index file in the /trpc/routers directory to support that in the future, but for now we will just add the routes/endpoints directly to the router
// server/trpc/routers/index.tsimport{z}from'zod';import{publicProcedure,router}from'../trpc';exportconstappRouter=router({hello:publicProcedure.input(z.object({text:z.string().nullish(),})).query(({input})=>{return{greeting:`hello ${input?.text??'world'}`,};}),});// export type definition of APIexporttypeAppRouter=typeofappRouter;
The hello route has an optional string parameter text which is used in the query to return the greeting with the text parameter value or the static string hello.
Next create the tRPC Plugin so you can access the trpc client throughout your application.
You will be able to access the client using const { $trpcClient } = useNuxtApp(); anywhere in you nuxt app client.
// plugins/trpc-client.tsimport{createTRPCNuxtClient,httpBatchLink}from'trpc-nuxt/client';importtype{AppRouter}from'~/server/trpc/routers';exportdefaultdefineNuxtPlugin(()=>{/**
* createTRPCNuxtClient adds a `useQuery` composable
* built on top of `useAsyncData`.
*/consttrpcClient=createTRPCNuxtClient<AppRouter>({links:[httpBatchLink({url:'/api/trpc',}),],});return{provide:{trpcClient,},};});
Next we create and initialize the API Handler using theappRouter we create and we pass in the function to create the context
// server/api/trpc/[trpc].tsimport{createNuxtApiHandler}from'trpc-nuxt';import{appRouter}from'../../trpc/routers';import{createContext}from'../../trpc/context';// export API handlerexportdefaultcreateNuxtApiHandler({router:appRouter,createContext,});
Test Code In Application
Now that everything is in place we can test the api call in the app.vue
Move Fast and Break Nothing. End-to-end typesafe APIs made easy. Experience the full power of TypeScript inference to boost productivity for your NUXT full-stack application.
Use this project to get a Nuxt 3 application up and running with tRpc using the trpc-nuxt module.
Setup
Make sure to install the dependencies:
# npm
npm install
Development Server
Start the development server on http://localhost:3000: