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:

DatabaseBackendAPI ClientMiddlewareRouterFrontendUserDatabaseBackendAPI ClientMiddlewareRouterFrontendUserUser InteractionRoute NavigationRun Middleware ChainAuthenticated RequestHTTP RequestProcess RequestQuery DataReturn ResultsHTTP ResponseUpdate StateRender UI

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:

  1. Select auth method (e.g., password, 2FA)
  2. Verify credentials
  3. Generate access and refresh tokens
  4. Log attempt, update user record
  5. 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:

  1. Get model definition from DSL registry
  2. Validate user access for the operation
  3. 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:

DatabaseTokenServicePasswordAuthAuthMethodServiceAuthServiceAuthControllerClientDatabaseTokenServicePasswordAuthAuthMethodServiceAuthServiceAuthControllerClientPOST /auth/loginlogin(credentials)initiate(credentials)initiate(credentials)findUser(email)UserverifyPassword()UserUsergenerateTokens(user)storeRefreshToken()TokensupdateLastLogin(userId){user, tokens, abilityRules}Response

Context System in Action

The context system provides request-scoped data and dependency injection:

HTTP Request

ContextMiddleware

Create Context

Store User Info

Store Request Info

Attach to Request

Run in Context

Controller

Get Current Context

Access Services

Access Request Data

Business Logic

HTTP Response

Middleware Chain

Requests pass through a series of middleware:

  1. CORS Middleware: Handles cross-origin requests
  2. Body Parser: Parses JSON request bodies
  3. Request Logger: Logs incoming requests
  4. Context Middleware: Creates request context
  5. Authentication Middleware: Verifies user authentication
  6. Validation Middleware: Validates request data
  7. Route Handler: Processes the request
  8. Error Handler: Catches and formats errors

Frontend Middleware Chain

Frontend routes also pass through middleware:

  1. Auth Guard: Checks if user is authenticated
  2. Ability Guard: Checks if user has permission
  3. Layout Resolver: Determines the layout to use
  4. 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:

  1. Isolation: Each request has its own context
  2. Modularity: Components are loosely coupled
  3. Testability: Services can be easily mocked
  4. Consistency: Standard request and response formats
  5. Observability: Comprehensive logging throughout the cycle

WARNING

Failed to fetch dynamically imported module: https://typus.dev/assets/RecursiveNavItem-Cep7andh.js

{ "stack": "AppError: Failed to fetch dynamically imported module: https://typus.dev/assets/RecursiveNavItem-Cep7andh.js\n at https://typus.dev/assets/index-DS79FI73.js:315:420\n at dn (https://typus.dev/assets/vue-vendor-Ct83yDeK.js:13:1385)\n at We (https://typus.dev/assets/vue-vendor-Ct83yDeK.js:13:1455)\n at Ws.t.__weh.t.__weh (https://typus.dev/assets/vue-vendor-Ct83yDeK.js:14:7364)\n at jt (https://typus.dev/assets/vue-vendor-Ct83yDeK.js:13:1866)\n at v (https://typus.dev/assets/vue-vendor-Ct83yDeK.js:14:4019)\n at https://typus.dev/assets/vue-vendor-Ct83yDeK.js:14:4097" }