Request-Response Cycle
Last Updated: May 11, 2025
This document describes the complete request-response cycle in the Typus Development Framework, from client-side request initiation to server processing and back to client rendering.
Overview
The request-response cycle in Typus involves multiple layers and components working together to process user interactions:
Frontend Request Flow
1. User Interaction
The cycle begins with a user interaction, such as clicking a button or submitting a form.
Key takeaway: User actions trigger application logic.
2. Store Actions
User interactions trigger store actions that manage application state and API communication.
Key takeaway: Stores manage state and API calls.
3. API Client
The API client handles HTTP communication with the backend.
Key takeaway: API client abstracts HTTP communication.
4. Router Navigation
After successful API operations, the application often navigates to a new route.
Key takeaway: Router handles client-side navigation.
5. Middleware Execution
Route navigation triggers middleware execution for authentication and authorization.
Key takeaway: Middleware guards routes (auth, permissions).
6. Component Rendering
Finally, the appropriate components are rendered with data.
Key takeaway: Components fetch and display data.
Backend Request Flow
1. Request Reception
When the backend receives a request, it first passes through global middleware.
Key takeaway: Middleware processes requests globally.
2. Context Creation
For each request, a new context is created to store request-specific data.
Key takeaway: Context provides request-scoped data.
3. Route Handling
The request is then routed to the appropriate controller method.
Key takeaway: Router dispatches to controller methods.
4. Controller Processing
Controllers handle the business logic for each endpoint.
Key takeaway: Controllers orchestrate service calls.
5. Service Layer
Services contain the core business logic.
Key takeaway: Services implement core business logic.
6. Authentication Process
Authentication involves multiple steps:
- Select auth method (e.g., password, 2FA)
- Verify credentials
- Generate access and refresh tokens
- Log attempt, update user record
- Return user data and tokens
Key takeaway: Authentication is a multi-step process.
7. Response Formatting
Responses are formatted in a standard structure.
Key takeaway: Responses follow a consistent format.
DSL Request Flow
The DSL (Domain Specific Language) system provides a unified API for data operations:
1. Frontend DSL Client
The frontend DSL client provides typed data access methods.
Key takeaway: DSL client provides typed data access.
2. DSL Operation Execution
The backend DSL service handles generic data operations:
- Get model definition from DSL registry
- Validate user access for the operation
- Execute database query via Prisma
Key takeaway: DSL service handles generic data operations.
3. Prisma Database Access
Prisma ORM executes the actual database queries.
Key takeaway: Prisma ORM interacts with the database.
Authentication Flow
The authentication flow involves multiple steps:
Context System in Action
The context system provides request-scoped data and dependency injection:
Middleware Chain
Requests pass through a series of middleware:
- CORS Middleware: Handles cross-origin requests
- Body Parser: Parses JSON request bodies
- Request Logger: Logs incoming requests
- Context Middleware: Creates request context
- Authentication Middleware: Verifies user authentication
- Validation Middleware: Validates request data
- Route Handler: Processes the request
- Error Handler: Catches and formats errors
Frontend Middleware Chain
Frontend routes also pass through middleware:
- Auth Guard: Checks if user is authenticated
- Ability Guard: Checks if user has permission
- Layout Resolver: Determines the layout to use
- Meta Handler: Sets page metadata
Error Handling
Errors are caught and formatted consistently throughout the system. All errors follow a standard response format with status code, error message, and optional details.
Key takeaway: Consistent error responses.
Performance Monitoring
The system includes performance monitoring that tracks request processing time and logs performance metrics.
Key takeaway: Performance is monitored.
Conclusion
The request-response cycle in Typus Development Framework demonstrates a well-structured, modular approach to handling user interactions. By leveraging middleware, context, and dependency injection, the system provides a clean separation of concerns while maintaining flexibility and extensibility.
Key benefits of this architecture include:
- Isolation: Each request has its own context
- Modularity: Components are loosely coupled
- Testability: Services can be easily mocked
- Consistency: Standard request and response formats
- Observability: Comprehensive logging throughout the cycle