trpc-patterns
TRPC Patterns
Overview
Implement TRPC routers using established blogic-template-ts patterns for input schemas, procedure selection, middleware composition, and error handling.
When to Use This Skill
Use this skill when implementing or refactoring TRPC routers and procedures, especially when selecting base procedures, adding authorization middleware, or standardizing error handling.
Core Rules
1) Keep simple schemas inline
Define simple input schemas directly in the procedure.
Use project enums and shared types instead of hardcoded string literals.
protectedProcedure.input(z.object({ organizationId: z.string().min(1) }));
Apply these rules:
- Keep straightforward schemas inline.
- Reuse shared enums/types from project packages.
- Extract a schema constant only when reuse or complexity justifies it.
Read detailed examples in references/simple-schemas.md.
2) Select the narrowest valid base procedure
Select base procedures in this order:
- Start from access requirement, not implementation convenience.
- Pick
publicProcedurewhen no authentication is required. - Pick
protectedProcedurewhen any authenticated user can access. - Pick
adminProcedurewhen global admin role is required. - Pick
protectedMemberAccessProcedurewhenorganizationId-scoped membership is required. - Build a custom procedure with
.use()only when none of the existing procedures encode the required access rule.
Apply this decision tree:
- No auth needed ->
publicProcedure - Auth needed, no org scope ->
protectedProcedure - Global admin-only action ->
adminProcedure - Org-scoped action with membership check ->
protectedMemberAccessProcedure - Additional role/permission/resource checks -> extend with middleware on top of the closest base procedure
Read full procedure examples and context behavior in references/custom-procedures.md.
3) Compose middleware with context enhancement
Create reusable middleware with .use() and always continue with opts.next(...).
const procedure = protectedProcedure.use(async (opts) => opts.next({ ctx: {} }));
Apply these rules:
- Name middleware functions by responsibility.
- Enhance context only with values needed by downstream handlers.
- Build from existing base procedures before introducing new ones.
Read advanced middleware patterns in references/middleware-patterns.md.
4) Throw standardized application errors
Import and throw standardized error helpers from project infrastructure.
if (!resource) throw notFoundError("Resource not found");
Apply these rules:
- Throw helper-based errors (
badRequestError,unauthorizedError,forbiddenError,notFoundError). - Do not instantiate
TRPCErrormanually in route and middleware code when project helpers exist. - Keep error messages action-oriented and domain-specific.
Read full patterns in references/error-handling.md.
5) Treat imports as project conventions
Follow these conventions for blogic-template-ts repositories:
- Import shared domain enums/types from project packages (for example
@project/common). - Import standardized error helpers from project infrastructure modules (for example
@/infrastructure/errors).
When working outside this ecosystem, map these conventions to equivalent modules in the target codebase.
Type Inference Guidance
Rely on static TypeScript inference from backend router definitions.
Apply these rules:
- Export root router types consistently.
- Import router types into frontend TRPC client setup.
- Avoid manual endpoint response/input types that duplicate inferred TRPC types.
Read additional examples in references/custom-procedures.md and keep router exports consistent.
Scope Boundaries
Focus this skill on TRPC router structure, access control, middleware, and errors.
Delegate query-level performance tuning (JOIN strategy, batch reads, N+1 reduction, indexing) to the performance-optimization skill.
Resources
Read long-form examples and implementation details in:
references/simple-schemas.md- Complete examples of inline schema patternsreferences/custom-procedures.md- Base procedure behavior and context enhancementreferences/middleware-patterns.md- Advanced middleware composition patternsreferences/error-handling.md- Detailed helper-based error patterns
For query and database performance guidance, load the performance-optimization skill.
More from blogic-cz/blogic-marketplace
marketing-expert
This skill should be used when writing or rewriting marketing copy for software products, including positioning, messaging, homepage rewrite work, landing pages, product descriptions, conversion-focused updates, and sales-enablement content. Produces clear, truthful, high-performing SaaS copy.
97requirements
This skill should be used when clarifying a feature, writing a requirements spec, running a structured discovery session, or when users mention requirements-start, requirements-status, requirements-current, requirements-list, requirements-remind, or requirements-end.
78frontend-design
This skill should be used when a task requires designing or implementing frontend UI (components, pages, layouts, styling) and no more specialized frontend skill is a better fit. It guides production-grade, brand-consistent visual implementation with distinctive but controlled aesthetics.
78testing-patterns
This skill should be used when implementing or reviewing testing workflows in template-ts projects, especially for testing, Vitest, Playwright, integration test, and mocking scenarios.
76git-workflow
Automates the full PR lifecycle — create or update a pull request, then aggressively monitor CI checks and review feedback in a continuous loop, fixing failures and addressing comments until the PR is fully green. Also covers push, branch creation, and branch sync workflows.
76debugging-with-opensrc
Load this skill when debugging behavior in external libraries by reading local OpenSrc mirrors (Effect, TanStack, TRPC, Drizzle, Better Auth, Sentry, Pino), or when docs conflict with runtime behavior and source-level verification is required.
75