Skip to main content
The NCT Hub platform uses tRPC for end-to-end type-safe API communication between the Next.js frontend and backend. This ensures type safety for club management operations, member interactions, and data synchronization.

Architecture Overview

Directory Structure

Creating a New Router

Step 1: Define the Router

Create a new file in apps/web/src/trpc/routers/:

Step 2: Add to Root Router

Add your router to apps/web/src/trpc/routers/_app.ts:

Step 3: Use in Components

Client Component with useQuery

Client Component with useMutation

Server Component (Server-Side Caller)

Procedure Types

publicProcedure

No authentication required. Use for public endpoints.

protectedProcedure

Requires authentication. Provides ctx.user in handler.
Implementation in apps/web/src/trpc/init.ts:

Input Validation with Zod

Always validate inputs using Zod schemas:

Common Zod Patterns

Error Handling

Throwing Errors

Error Codes

  • BAD_REQUEST - Invalid input
  • UNAUTHORIZED - Not authenticated
  • FORBIDDEN - Authenticated but not authorized
  • NOT_FOUND - Resource not found
  • INTERNAL_SERVER_ERROR - Server error
  • TIMEOUT - Request timeout
  • CONFLICT - Resource conflict

Handling Errors Client-Side

React Query Integration

tRPC uses React Query under the hood. You have access to all React Query features:

Query Options

Optimistic Updates

Cache Invalidation

Performance Best Practices

1. Use Query Keys Wisely

2. Enable Request Batching

tRPC automatically batches requests made within 10ms:

3. Use staleTime for Cacheable Data

4. Prefetch on Server

Testing tRPC Procedures

External Resources