updating middleware doc

This commit is contained in:
2025-11-19 21:55:34 +01:00
parent 92a2610b70
commit a18ad48721
3 changed files with 144 additions and 13 deletions

View File

@@ -184,23 +184,58 @@ class ProductValidationException(ValidationException):
**Rationale**: Exception files are domain-focused, not collection-focused.
### Middleware Files (DESCRIPTIVE)
### Middleware Files (SIMPLE NOUNS)
**Rule**: Middleware files use descriptive names based on their function.
**Rule**: Middleware files use simple, descriptive noun names **without redundant suffixes**.
**Location**: `middleware/`
**Naming Pattern**: `{purpose}.py` (no "_middleware" suffix)
**Examples**:
```
middleware/
├── auth.py # Authentication middleware
├── vendor_context.py # Vendor context detection
├── rate_limiter.py # Rate limiting functionality
├── logging_middleware.py # Request logging
── decorators.py # Cross-cutting decorators
├── auth.py # ✅ AuthManager - Authentication & JWT
├── rate_limiter.py # ✅ RateLimiter - Request throttling
├── vendor_context.py # ✅ VendorContextManager - Multi-tenant detection
├── context.py # ✅ ContextManager - Request context detection
── theme_context.py # ✅ ThemeContextManager - Theme loading
├── logging.py # ✅ LoggingMiddleware - Request/response logging
└── decorators.py # ✅ rate_limit decorator - Cross-cutting concerns
```
**Rationale**: Middleware serves specific cross-cutting functions.
**Rationale**:
- Keeps names concise and consistent
- Follows Django, Flask, and FastAPI conventions
- Avoids redundant "_middleware" suffix
- Makes imports cleaner: `from middleware.logging import LoggingMiddleware`
**Test File Naming**: Test files directly mirror the middleware filename:
```
tests/unit/middleware/
├── test_auth.py # Tests auth.py
├── test_rate_limiter.py # Tests rate_limiter.py
├── test_vendor_context.py # Tests vendor_context.py
├── test_context.py # Tests context.py
├── test_theme_context.py # Tests theme_context.py
├── test_logging.py # Tests logging.py
└── test_decorators.py # Tests decorators.py
```
**One Test File Per Component**: Each test file should test **exactly one** middleware component, following the Single Responsibility Principle.
**Class Names Within Files**:
```python
# middleware/logging.py
class LoggingMiddleware(BaseHTTPMiddleware): # Class name can include "Middleware"
pass
# middleware/context.py
class ContextManager: # Manager for business logic
class ContextMiddleware(BaseHTTPMiddleware): # Middleware wrapper
```
**Rationale**: Middleware serves specific cross-cutting functions with clean, predictable naming.
### Frontend Files
@@ -397,7 +432,8 @@ Consider implementing linting rules or pre-commit hooks to enforce:
| Schema/Pydantic | SINGULAR | `product.py`, `order.py` |
| Services | SINGULAR + service | `product_service.py` |
| Exceptions | SINGULAR | `product.py`, `order.py` |
| Middleware | DESCRIPTIVE | `auth.py`, `rate_limiter.py` |
| Middleware | SIMPLE NOUN | `auth.py`, `logging.py`, `context.py` |
| Middleware Tests | test_{name}.py | `test_auth.py`, `test_logging.py` |
| Database Tables | SINGULAR | `product`, `inventory` |
| Database Columns | SINGULAR | `vendor_id`, `created_at` |
| API Endpoints | PLURAL | `/products`, `/orders` |