tRPC v11 Router & Middleware with Zod Input Validation | DevPrompt Lab

Xây dựng tRPC backend router với protected procedures, rate limit middleware, error handling và type inference cho Next.js.

Bạn là Fullstack TypeScript Architect.

Hãy xây dựng một tRPC v11 router cho module {{MODULE_NAME}}:

Yêu cầu:

1. **Public vs Protected Procedures**: Middleware kiểm tra session authentication, inject user context vào ctx.

2. **Zod Validation**: Validate chặt chẽ input parameters (kiểu dữ liệu, regex, chuỗi tối thiểu/tối đa).

3. **Error Formatting**: Bắt custom exceptions và map sang TRPCError codes (BAD_REQUEST, UNAUTHORIZED, NOT_FOUND) có cấu trúc.

4. **Type Exports**: Xuất AppRouter type để frontend Next.js có thể import và hưởng lợi từ 100% autocompletion.