🏗️ Architecture

Request Lifecycle

Understand how an HTTP request flows through the Ever Gauzy API from entry to response.

Overview

Diagram; its source is available below
Diagram source
sequenceDiagram
    participant C as Client
    participant MW as Middleware
    participant G as Guards
    participant I as Interceptors
    participant P as Pipes
    participant CT as Controller
    participant S as Service
    participant R as Repository
    participant DB as Database

    C->>MW: HTTP Request
    MW->>G: TenantPermissionGuard
    G->>G: PermissionGuard
    G->>I: TransformInterceptor
    I->>P: ValidationPipe
    P->>CT: Controller method
    CT->>S: Service method
    S->>R: Repository query
    R->>DB: SQL Query
    DB-->>R: Result
    R-->>S: Entity/DTO
    S-->>CT: Business result
    CT-->>I: Response
    I-->>C: Transformed JSON

Phase 1: Middleware

Express middleware processes the raw request:

MiddlewarePurpose
helmetSecurity headers
corsCORS handling
compressionResponse compression
body-parserJSON body parsing
cookie-parserCookie parsing

Phase 2: Guards

Guards determine if the request is authorized:

GuardOrderPurpose
TenantPermissionGuard1stExtract and validate tenant
PermissionGuard2ndCheck user permissions
RoleGuard3rdCheck user role (optional)
FeatureFlagGuard4thCheck feature availability

Phase 3: Interceptors

Interceptors transform request/response:

InterceptorPurpose
TransformInterceptorStandardize response format
TimeoutInterceptorRequest timeout handling
LazyLoadInterceptorLazy-load relations

Phase 4: Pipes

Pipes validate and transform input:

PipePurpose
ValidationPipeDTO class-validator
UUIDValidationPipeUUID parameter validation
ParseJsonPipeJSON query param parsing

Phase 5: Controller → Service → Repository

The actual business logic execution:

  1. Controller — route handler, delegates to service
  2. Service — business logic, tenant filtering
  3. Repository — database operations (TypeORM/MikroORM)

Phase 6: Response

The response flows back through interceptors for transformation before being sent to the client.

Error Handling

Errors at any phase are caught by the global exception filter:

Diagram; its source is available below
Diagram source
graph LR
    A[Exception] --> B{Type?}
    B -->|HttpException| C[HTTP Status + Message]
    B -->|ValidationError| D[400 + Field Errors]
    B -->|Unauthorized| E[401 Unauthorized]
    B -->|Unknown| F[500 Internal Server Error]

Edit this page

Copyright © 2023-Present Ever Co. LTD.Docs by Sourcey