trpc-router

Implements tRPC routers, procedures, and API endpoints for server applications.

Updated Jan 27, 2026
One-click install
npx skills add https://github.com/SmallAi-API/smaihub --skill trpc-router-smallai-api
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: trpc-router
Source: https://github.com/SmallAi-API/smaihub/tree/main/.agents/skills/trpc-router
Command: npx skills add https://github.com/SmallAi-API/smaihub --skill trpc-router-smallai-api

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing tRPC routers without consistent conventions leads to duplicated model instantiation, inconsistent error handling, and N+1 request patterns between client and server. This Skill enforces a standardized structure for routers, procedures, middleware, and error handling in apps/server/src/routers. ## Core Features & Use Cases - Middleware-Based Model Injection: Injects database models into the tRPC context via middleware instead of constructing models inside every procedure. - Standardized Procedure Patterns: Provides query and mutation templates with zod input validation, TRPCError re-throwing, and consistent { data, success: true } return shapes. - Aggregated Detail Endpoints: Builds single detail procedures that fetch related data in parallel with Promise.all to avoid sequential client requests. - Use Case: When adding a new domain endpoint to the server, follow this guide to scaffold the router file, wire models through middleware, and return properly typed, error-handled responses. ## Quick Start Create a new tRPC router for the task domain following the conventions in this guide.

Frequently Asked Questions about trpc-router

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a tRPC router with procedures in TypeScript?

Define a router with the router() function and add procedures using authedProcedure with zod input schemas. Use .query() for reads and .mutation() for writes, returning { data, success: true } for queries and { data, message, success: true } for mutations.

How to inject database models into tRPC context with middleware?

Chain middleware with authedProcedure.use(serverDatabase).use() and return models in opts.next({ ctx }) so procedures access them via ctx.fooModel. Avoid constructing new Model instances inside individual procedures unless a different userId is required.

What is the correct way to handle errors in tRPC procedures?

Re-throw existing TRPCError instances directly, and wrap other errors with console.error using a [domain:procedure] log prefix plus a new TRPCError with code INTERNAL_SERVER_ERROR and the original error as cause.

How do I avoid multiple sequential API requests for a detail view?

Create a single aggregated detail procedure that fetches the main entity and related data in parallel using Promise.all. This returns one combined response instead of forcing the client to make N sequential requests.

When should I create a model inline instead of using middleware in tRPC?

Create models inline only when the procedure needs a different userId than the authenticated context, such as a watchdog iterating over multiple users' tasks. Otherwise always use middleware-injected models from ctx.