trpc-router

Guides creation of tRPC routers with middleware-injected models and zod validation.

74|11|Updated Jul 4, 2024
One-click install
npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill trpc-router-opensourceagi
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: trpc-router
Source: https://github.com/OpenSourceAGI/qwksearch-research-agent/tree/main/apps/qwk-in-lobe/.agents/skills/trpc-router
Command: npx skills add https://github.com/OpenSourceAGI/qwksearch-research-agent --skill trpc-router-opensourceagi

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @trpc/server, zod.

What problem does it solve? It standardizes how developers create and modify tRPC routers in the apps/server codebase, preventing inconsistent error handling, scattered model instantiation, and ad-hoc response shapes across API endpoints. ## Core Features & Use Cases - Middleware-Based Model Injection: Enforces injecting 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 handling, and consistent { data, success } return shapes. - Aggregated Detail Endpoints: Shows how to build a single detail procedure that fetches related data in parallel with Promise.all to avoid N sequential client requests. - Use Case: When adding a new task domain endpoint under apps/server/src/routers/lambda, follow this guide to wire models through serverDatabase middleware, validate inputs with zod schemas, and return uniform responses. ## Quick Start Create a new tRPC router for the task domain following the trpc-router conventions with middleware-injected models and zod-validated procedures.

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 new tRPC router procedure?

Define a router with the router() helper, attach procedures to a domain procedure built from authedProcedure and serverDatabase middleware, validate inputs with zod schemas, and return { data, success: true } for queries or { data, message, success: true } for mutations.

How to inject database models into tRPC context?

Use middleware chained after serverDatabase that returns opts.next with models constructed from ctx.serverDB and ctx.userId added to ctx. Procedures then access ctx.fooModel directly instead of instantiating models inside each handler.

When should I create a model inside a tRPC procedure?

Only when the model needs a different userId than the current context, such as a watchdog iterating over multiple users' tasks. Otherwise always inject models through middleware to keep procedures clean and consistent.

How do I handle errors in tRPC procedures?

Re-throw existing TRPCError instances unchanged, and wrap other errors by logging with console.error using a [domain:procedure] prefix, then throwing a new TRPCError with code INTERNAL_SERVER_ERROR and the original error as cause.

How to avoid multiple sequential API requests for detail views?

Create a single aggregated detail procedure that resolves the main entity, then fetches children and related records in parallel with Promise.all. This returns one combined payload so the frontend avoids N sequential requests.