Introduction
Khedma Market uses tRPC to build a type-safe API layer between the client and server. This provides end-to-end type safety from your database to your frontend components without code generation.Architecture
What is tRPC?
tRPC allows you to easily build & consume fully typesafe APIs without schemas or code generation. It leverages TypeScript to provide:- Type Safety: Full type inference from server to client
- Auto-completion: IDE support for all API endpoints
- Runtime Safety: Validation using Zod schemas
- Developer Experience: No need to maintain separate API documentation
Core Components
The tRPC setup in Khedma Market consists of:- Root Router (
~/workspace/source/src/server/api/root.ts:13-23): Combines all feature routers - Context (
~/workspace/source/src/server/api/trpc.ts:29-36): Provides database and session access to procedures - Procedures: Public and protected endpoints for each feature
- Client (
~/workspace/source/src/trpc/react.tsx): React Query integration for the frontend
Base URL
The tRPC API is available at:- Development:
http://localhost:3000/api/trpc - Production:
https://your-domain.com/api/trpc - Vercel:
https://${VERCEL_URL}/api/trpc
~/workspace/source/src/trpc/shared.ts:8-15 for the implementation.
Available Routers
Khedma Market has 9 feature routers:User Router
Namespace:userFeatures:
- User profile management
- Personal information updates
- Security settings (password, 2FA)
- Username availability checking
- Account role management (client/freelancer/company)
- Company creation
getUserbyUsername(protected)updateUserPersonalInfo(protected)updateUserSecurityInfo(protected)checkUsername(protected)updateAccount(protected)createCompany(protected)
File Router
Namespace:fileFeatures:
- Generate pre-signed S3 URLs for secure file uploads
- Direct client-to-S3 upload flow
generateUrl(protected)
Order Router
Namespace:orderFeatures:
- Order creation with package selection
- Payment processing integration
- Order status tracking
create(protected)
Profile Router
Namespace:profileFeatures:
- Skills management (add, update, remove)
- Languages management (add, update, remove)
- User description updates
- Public profile queries
addSkill,updateSkill,removeSkill(protected)addLanguage,updateLanguage,removeLanguage(protected)getUserSkills,getLanguages(queries)
Category Router
Namespace:categoryFeatures:
- Browse top-level categories
- Get subcategories by parent ID
- Hierarchical category structure
getCategories(public)getSubCategories(public)
Gig Router
Namespace:gigFeatures:
- Gig creation and drafts
- Package management (basic, standard, premium)
- Description and FAQ updates
- Gallery/attachment management
- Publishing and deletion
- User gig listings
createDraft(protected)updateOverview(protected)createPackages(protected)updateDescriptionFaq(protected)updateGallery(protected)publish(protected)delete(protected)getUserGigs(protected)
Project Router
Namespace:projectFeatures:
- Portfolio project creation
- Draft workflow with gallery uploads
- Project publishing and management
createDraft(protected)createProject(protected)delete(protected)
Conversation Router
Namespace:conversationFeatures:
- Real-time messaging between users
- Conversation creation and retrieval
- Message sending with attachments
getConverstion(protected)sendMessage(protected)
Job Router
Namespace:jobFeatures:
- Job posting creation and updates
- Application submission
- Job type management (full-time, part-time, contract, intern)
create(protected)update(protected)apply(protected)
Making API Calls
Client-Side (React Components)
Khedma Market uses@trpc/react-query for client-side API calls. The API client is exported from ~/workspace/source/src/trpc/react.tsx:16.
Queries
Mutations
Server-Side (Server Components)
For Next.js Server Components and API routes:Error Handling
Error Types
tRPC uses specific error codes based on HTTP status codes:UNAUTHORIZED: User is not authenticated (401)FORBIDDEN: User lacks permission (403)NOT_FOUND: Resource doesn’t exist (404)BAD_REQUEST: Invalid input data (400)UNPROCESSABLE_CONTENT: Valid input but cannot process (422)INTERNAL_SERVER_ERROR: Server error (500)
Error Formatting
Errors include Zod validation details when input validation fails (~/workspace/source/src/server/api/trpc.ts:48-57):
Handling Errors in React
Custom Error Throwing
In procedures, throw errors usingTRPCError:
~/workspace/source/src/server/api/routers/user.ts:76-80 and ~/workspace/source/src/server/api/routers/gig.ts:30-34.
Type Safety Benefits
Input/Output Type Inference
Khedma Market exports type helpers for inferring router types:~/workspace/source/src/server/api/root.ts:27-28 for the implementation.
Zod Schema Validation
All inputs are validated using Zod schemas before reaching your procedure:SuperJSON Transformer
Khedma Market uses SuperJSON to serialize complex JavaScript types (Date, Map, Set, etc.) across the network without losing type information. This is configured in:- Server:
~/workspace/source/src/server/api/trpc.ts:47 - Client:
~/workspace/source/src/trpc/shared.ts:6
Data Transformer
The API uses SuperJSON for data serialization, which allows:- Date objects: Automatically serialized/deserialized
- undefined values: Preserved (unlike JSON)
- BigInt, Map, Set: Supported types
Next Steps
- Authentication - Learn about protected procedures and session management
- User Endpoints - User management endpoints
- Gig Endpoints - Gig creation and management endpoints
- Order Endpoints - Order and payment management
- Job Endpoints - Job posting and application management
- Conversation Endpoints - Messaging endpoints
- Profile Endpoints - Profile management endpoints