How hantsy/nestjs-rest-sample works
Main Components
• NestJS Application Core – At the center is the NestJS main module (src/app.module.ts) that aggregates all other modules.
• Modules – Each subdirectory under “src” represents a distinct module:
• Auth Module (src/auth): Handles authentication using Passport strategies (JWT and local). Include components like the JWT strategy, authentication controllers, guards (has-roles, jwt-auth, local-auth, roles guard), and supporting constants/interfaces.
• User Module (src/user): Manages user registration and profile operations.
• Post Module (src/post): Manages creating, retrieving, updating, and deleting posts and comments.
• Database Module (src/database): Provides the interface to MongoDB with a custom Mongoose integration, including providers for connecting to and modeling the database.
• Logger Module (src/logger): Implements custom logging functionalities.
• SendGrid Module (src/sendgrid): Integrates SendGrid for email notifications.
• Config Module (src/config): Centralizes configuration settings for JWT, MongoDB, SendGrid, etc.
• Shared Components (src/shared): Contains common utilities, enums (e.g., role types), and pipes used across the application.
Their Relationships and Interactions
• The NestJS core (bootstrapped via src/main.ts) initializes the application and ties together all modules.
• Controllers across modules (within Auth, User, Post, etc.) expose RESTful endpoints that receive HTTP requests.
• Services in each module implement business logic and interact with repositories or the Database Module.
• Authentication flows: When a user attempts login or registration, the Auth Module interacts with its strategies and guards; it then validates credentials (potentially checking user details via the Database Module) and issues JWTs.
• The Database Module provides MongoDB connection providers for handling CRUD operations via custom models for User, Post, and Comment.
• The Logger Module is used across modules to log important actions or error events.
• The SendGrid Module, integrated with the rest of the application (e.g., post notifications or registration confirmations), provides external email services.
Architectural Patterns
• Modular design (each concern, such as authentication, user management, etc., is separated into its own directory/module).
• RESTful APIs following the Richardson Maturity Model (Level 2), with clear separation of concerns.
• Integration of external services, such as SendGrid for emails.
• Use of Guards, Strategies, and Interceptors as part of NestJS’s design patterns for security and cross-cutting concerns.
• Comprehensive testing (presence of unit test files and e2e testing configurations) that also emphasize dependency injection and modular testing practices.
Key Details for Documentation
• Flows such as:
• A client sends a RESTful request → API Gateway (NestJS main entry) routes to the relevant Controller → corresponding Service processes the logic and interacts with the Database Module or SendGrid Module if needed.
• Authentication flow detailing the role of Passport’s strategies and guards.