Overview
Guccho uses TRPC to provide end-to-end type-safe API communication between the Nuxt frontend and backend. TRPC enables auto-complete and type checking for API calls without code generation.TRPC Setup
Server Configuration
TRPC is initialized insrc/server/trpc/trpc.ts:
Context Creation
The context provides request-scoped data to procedures:Nuxt API Handler
TRPC is exposed as a Nuxt API endpoint:/api/trpc/* that processes all TRPC requests.
Router Structure
Routers are organized by domain insrc/server/trpc/routers/:
Middleware
Middleware adds authentication, session handling, and authorization:Session Middleware
User Middleware
Optional User Middleware
Creating Procedures
Query Procedure
Queries retrieve data without side effects:Mutation Procedure
Mutations modify data:Input Validation
Use Zod schemas for input validation:Error Handling
Throw TRPC errors with appropriate codes:Client Usage
On the client side, import and use the TRPC client:Type Inference
TRPC automatically infers types from the router:Best Practices
Procedure Design
- Use queries for reads, mutations for writes
- Validate all inputs with Zod schemas
- Apply appropriate middleware (public, session, user, role)
- Handle errors consistently using TRPC error codes
- Log important operations for debugging
Type Safety
- Use ID transform functions at boundaries (
idToString,stringToId) - Export router types for client inference
- Define input/output types clearly
- Avoid
anytypes - use generics instead
Performance
- Batch related queries when possible
- Paginate large result sets
- Cache frequently accessed data in providers
- Use background jobs for non-critical operations
Data Serialization
Guccho usessuperjson for request serialization and devalue for responses:
DateobjectsBigIntvaluesMapandSetundefinedvalues
Next Steps
Architecture
Learn about Guccho’s overall system design
Backend Adapters
Understand how providers are implemented