Skip to content

AlexByte18/artisan-api

Repository files navigation

Artisan API

Nest Logo

A robust REST API built with NestJS and TypeORM for managing products, authentication, and real-time messaging with WebSockets.

πŸ“‹ Description

Artisan API is a production-ready backend service that provides:

  • πŸ” JWT Authentication with secure password hashing
  • πŸ“¦ Product Management with full CRUD operations
  • πŸ”‘ Role-based Access Control (RBAC)
  • πŸ“€ File Upload Management with static file serving
  • πŸ’¬ Real-time Messaging with WebSocket support
  • 🌍 PostgreSQL Database integration
  • βœ… Data Validation with class-validator
  • πŸ“ Comprehensive Testing (unit & e2e tests)

πŸš€ Key Features

Authentication & Authorization

  • User registration and login
  • JWT token generation and validation
  • Password encryption with bcrypt
  • Role-based access control

Product Management

  • List, create, update, and delete products
  • Product search and filtering
  • Product images/files management
  • Seed data for development

Real-time Features

  • WebSocket support for live messaging
  • Socket.IO integration
  • Real-time notifications

File Management

  • File upload handling
  • Static file serving
  • Product image storage

πŸ› οΈ Tech Stack

Category Technology
Framework NestJS 9
Language TypeScript 4.7
Database PostgreSQL 14+
ORM TypeORM
Authentication JWT + Passport
WebSockets Socket.IO
Validation Class-validator
Testing Jest
Package Manager npm

πŸ“¦ Installation

Prerequisites

  • Node.js 16+
  • npm or yarn
  • PostgreSQL 14+
  • Docker (optional, for PostgreSQL via docker-compose)

Steps

# Clone the repository
git clone <repository-url>
cd artisan-api

# Install dependencies
npm install

# Create environment variables file
cp .env.example .env

# Update .env with your database credentials
# Example:
# DB_HOST=localhost
# DB_PORT=5432
# DB_USERNAME=postgres
# DB_PASSWORD=your_password
# DB_NAME=artisan_db
# JWT_SECRET=your_jwt_secret

πŸƒ Running the Application

Development

npm run start:dev

The API will run on http://localhost:3000

Production

npm run build
npm run start:prod

With Docker

# Start PostgreSQL container
docker-compose up -d

# Run migrations and start app
npm run start:dev

πŸ“ Available Scripts

# Development
npm run start:dev        # Start with watch mode
npm run start:debug      # Start with debugger
npm run start            # Start production-like mode

# Building
npm run build            # Build for production
npm run prebuild         # Clean dist folder

# Testing
npm run test             # Run unit tests
npm run test:watch       # Run tests in watch mode
npm run test:cov         # Generate coverage report
npm run test:e2e         # Run end-to-end tests

# Code Quality
npm run lint             # Run ESLint with auto-fix
npm run format           # Format code with Prettier

πŸ“ Project Structure

src/
β”œβ”€β”€ auth/               # Authentication & Authorization
β”‚   β”œβ”€β”€ strategies/     # JWT & Passport strategies
β”‚   β”œβ”€β”€ guards/         # Auth guards
β”‚   └── decorators/     # Custom decorators
β”œβ”€β”€ products/           # Product management
β”‚   β”œβ”€β”€ entities/       # Product models
β”‚   β”œβ”€β”€ dto/            # Data transfer objects
β”‚   └── services/       # Business logic
β”œβ”€β”€ files/              # File management
β”‚   β”œβ”€β”€ controllers/    # File upload endpoints
β”‚   └── services/       # File handling logic
β”œβ”€β”€ messages-ws/        # WebSocket messaging
β”‚   β”œβ”€β”€ gateways/       # Socket.IO gateways
β”‚   └── services/       # Message logic
β”œβ”€β”€ common/             # Shared utilities
β”‚   β”œβ”€β”€ decorators/     # Custom decorators
β”‚   β”œβ”€β”€ guards/         # Common guards
β”‚   β”œβ”€β”€ pipes/          # Validation pipes
β”‚   └── filters/        # Exception filters
β”œβ”€β”€ seed/               # Database seeding
└── main.ts             # Application entry point

πŸ” Authentication

The API uses JWT (JSON Web Tokens) with Passport.js:

  1. Register: Create a new user account
  2. Login: Get a JWT token
  3. Access Protected Routes: Use token in Authorization header
# Example request
curl -H "Authorization: Bearer <your_jwt_token>" \
  http://localhost:3000/api/products

πŸ—„οΈ Database

Setup PostgreSQL

Using Docker Compose:

docker-compose up -d

Manual Setup:

# Create database
createdb artisan_db

# Run migrations
npm run typeorm migration:run

Seed Database

npm run seed

πŸ§ͺ Testing

# Unit tests
npm run test

# Watch mode
npm run test:watch

# Coverage report
npm run test:cov

# E2E tests
npm run test:e2e

πŸ”§ Configuration

Environment Variables

Create a .env file in the root directory:

# Database
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=password
DB_NAME=artisan_db

# JWT
JWT_SECRET=your_secret_key
JWT_EXPIRATION=3600

# File Upload
FILE_UPLOAD_PATH=./static/uploads

# Server
PORT=3000
NODE_ENV=development

πŸ“š Code Quality

ESLint

npm run lint

Prettier

npm run format

πŸ› Troubleshooting

Database Connection Error

  • Verify PostgreSQL is running
  • Check DATABASE_URL in .env
  • Ensure database exists

JWT Validation Issues

  • Verify JWT_SECRET in .env
  • Check token format in Authorization header
  • Ensure token hasn't expired

Port Already in Use

# Find and kill process on port 3000
lsof -ti:3000 | xargs kill -9

πŸ“„ License

MIT

πŸ‘€ Author

Artisan Development Team

πŸ“ž Support

For issues and questions, please open an issue on GitHub.


Last Updated: November 2025

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published