Skip to content

DevAdriam/nest_bdd_boilerplate

Repository files navigation

NestJS BDD Boilerplate

A production-ready NestJS application boilerplate with Behavior-Driven Development (BDD) testing, Clean Architecture principles, and comprehensive authentication system.

πŸš€ Features

  • Clean Architecture: Well-structured codebase following Domain-Driven Design principles
  • BDD Testing: Cucumber.js integration for behavior-driven development
  • Authentication: JWT and Google OAuth2 authentication strategies
  • Database: Prisma ORM with PostgreSQL
  • Email Service: Nodemailer integration with template support
  • API Documentation: Swagger/OpenAPI integration
  • Validation: Class-validator and class-transformer for DTOs
  • Security: Passport.js strategies, bcrypt password hashing
  • Code Quality: ESLint, Prettier, Husky, and Commitlint
  • Type Safety: Full TypeScript support

πŸ“ Project Structure

src/
β”œβ”€ application/      # Application services / use-cases
β”‚  β”œβ”€ auth/         # Authentication business logic
β”‚  └─ user/         # User management business logic
β”œβ”€ common/           # Shared utilities, decorators, constants, DTOs
β”‚  β”œβ”€ constants/    # Application constants
β”‚  β”œβ”€ decorators/   # Custom decorators
β”‚  β”œβ”€ dto/          # Shared DTOs
β”‚  β”œβ”€ service/      # Common services
β”‚  β”œβ”€ types/        # Type definitions
β”‚  └─ utils/        # Utility functions
β”œβ”€ core/             # Core logic (exceptions, interceptors, guards)
β”‚  β”œβ”€ exceptions/   # Custom exceptions
β”‚  β”œβ”€ filters/      # Exception filters
β”‚  β”œβ”€ interceptors/ # Response interceptors
β”‚  β”œβ”€ middleware/   # Custom middleware
β”‚  └─ pipes/        # Validation pipes
β”œβ”€ domain/user/      # Domain entities and business logic
β”‚  β”œβ”€ user.entity.ts
β”‚  β”œβ”€ user.repository.interface.ts
β”‚  └─ user.repository.ts
β”œβ”€ infrastructure/   # Database, API integrations, repositories
β”‚  β”œβ”€ auth/         # Authentication strategies and guards
β”‚  β”œβ”€ config/       # Configuration modules
β”‚  β”œβ”€ database/     # Database configuration
β”‚  β”œβ”€ mail/         # Email service
β”‚  β”œβ”€ storage/      # File storage service
β”‚  └─ third-party/  # External API integrations
β”œβ”€ presentation/     # Controllers, GraphQL resolvers, API endpoints
β”‚  └─ controllers/  # REST API controllers
β”œβ”€ app.module.ts     # Root module
└─ main.ts           # Entry point (bootstrap NestJS)

πŸ› οΈ Tech Stack

  • Framework: NestJS
  • Language: TypeScript
  • Database: PostgreSQL with Prisma ORM
  • Authentication: JWT + Google OAuth2
  • Testing: Jest + Cucumber.js
  • Email: Nodemailer
  • Documentation: Swagger/OpenAPI
  • Validation: Class-validator
  • Security: Passport.js, bcrypt

πŸ“‹ Prerequisites

  • Node.js (v18 or higher)
  • pnpm (recommended) or npm
  • PostgreSQL database
  • Google OAuth2 credentials (for Google authentication)

πŸš€ Getting Started

1. Clone the repository

git clone <repository-url>
cd nest_bdd_boilerplate

2. Install dependencies

pnpm install

3. Environment Setup

Create a .env file in the root directory:

# Database
DATABASE_URL="postgresql://username:password@localhost:5432/database_name"

# JWT
JWT_SECRET="your-jwt-secret-key"
JWT_EXPIRES_IN="7d"

# Google OAuth2
GOOGLE_CLIENT_ID="your-google-client-id"
GOOGLE_CLIENT_SECRET="your-google-client-secret"
GOOGLE_CALLBACK_URL="http://localhost:3000/auth/google/callback"

# Email Configuration
MAIL_HOST="smtp.gmail.com"
MAIL_PORT=587
MAIL_USER="your-email@gmail.com"
MAIL_PASS="your-app-password"
MAIL_FROM="your-email@gmail.com"

# Application
PORT=3000
NODE_ENV="development"

4. Database Setup

# Generate Prisma client
pnpm prisma generate

# Run database migrations
pnpm prisma migrate dev

# (Optional) Seed the database
pnpm prisma db seed

5. Start the application

# Development mode
pnpm start:dev

# Production mode
pnpm build
pnpm start:prod

The application will be available at http://localhost:3000

πŸ§ͺ Testing

Unit Tests

# Run unit tests
pnpm test

# Run tests in watch mode
pnpm test:watch

# Run tests with coverage
pnpm test:cov

BDD Tests (Cucumber)

# Run BDD tests
pnpm test:e2e

Test Structure

tests/
β”œβ”€ features/        # Gherkin feature files
β”‚  └─ user/
β”‚     └─ suspend.feature
└─ steps/           # Step definitions
   └─ user/
      └─ user.step.ts

πŸ“š API Documentation

Once the application is running, you can access the Swagger documentation at:

  • Swagger UI: http://localhost:3000/api
  • JSON Schema: http://localhost:3000/api-json

πŸ” Authentication

The application supports two authentication methods:

1. JWT Authentication

  • Register: POST /auth/register
  • Login: POST /auth/login
  • Protected routes require Authorization: Bearer <token> header

2. Google OAuth2

  • Initiate: GET /auth/google
  • Callback: GET /auth/google/callback

πŸ—οΈ Architecture Principles

Clean Architecture Layers

  1. Domain Layer (domain/): Contains business entities and repository interfaces
  2. Application Layer (application/): Contains use cases and application services
  3. Infrastructure Layer (infrastructure/): Contains database implementations, external APIs
  4. Presentation Layer (presentation/): Contains controllers and API endpoints

Key Design Patterns

  • Repository Pattern: Data access abstraction
  • Dependency Injection: IoC container for loose coupling
  • Strategy Pattern: Multiple authentication strategies
  • Interceptor Pattern: Cross-cutting concerns (logging, response formatting)

πŸ“ Available Scripts

# Development
pnpm start:dev          # Start in development mode
pnpm start:debug        # Start in debug mode

# Building
pnpm build              # Build the application
pnpm start:prod         # Start in production mode

# Testing
pnpm test               # Run unit tests
pnpm test:watch         # Run tests in watch mode
pnpm test:cov           # Run tests with coverage
pnpm test:e2e           # Run BDD tests

# Code Quality
pnpm lint               # Run ESLint
pnpm format             # Format code with Prettier

# Database
pnpm prisma generate    # Generate Prisma client
pnpm prisma migrate dev # Run database migrations
pnpm prisma studio      # Open Prisma Studio

πŸ”§ Configuration

Environment Variables

Variable Description Required
DATABASE_URL PostgreSQL connection string Yes
JWT_SECRET Secret key for JWT tokens Yes
JWT_EXPIRES_IN JWT token expiration time Yes
GOOGLE_CLIENT_ID Google OAuth2 client ID Yes
GOOGLE_CLIENT_SECRET Google OAuth2 client secret Yes
MAIL_HOST SMTP server host Yes
MAIL_USER SMTP username Yes
MAIL_PASS SMTP password Yes
PORT Application port No (default: 3000)
NODE_ENV Environment mode No (default: development)

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push to the branch: git push origin feature/amazing-feature
  5. Open a Pull Request

Commit Convention

This project uses Conventional Commits:

feat: add new feature
fix: fix a bug
docs: update documentation
style: formatting changes
refactor: code refactoring
test: add or update tests
chore: maintenance tasks

πŸ“„ License

This project is licensed under the UNLICENSED License.

πŸ†˜ Support

If you encounter any issues or have questions, please:

  1. Check the Issues page
  2. Create a new issue with detailed information
  3. Contact the maintainers

πŸ™ Acknowledgments


Happy Coding! πŸš€

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors