NexGenStudioDev/FastKit

Build Pluggable & Modular User Feature Module

Aberta

#8 aberto em 27 de jun. de 2025

 (0 comentário) (0 reação) (0 responsável)TypeScript (0 fork)auto 404
TypeScriptbackenddocumentationenhancementgood first issuenpm

Métricas do repositório

Stars
 (1 estrela)
Métricas de merge de PR
 (Métricas PR pendentes)

Description

🧑‍💼 User Feature Module (FastKit-style)

🧩 Description

Build a fully modular, pluggable, and class-based User module for managing user profiles. This module should expose a UserController class that can be directly used in any route with bind-safe methods.

Developers should be able to:

  • Read, update, and delete user profiles

  • Use class-based controller and service

  • Validate user profile updates

  • Plug in the controller with any route setup


🧱 Why This Is Important

  • 🎯 Every real app requires user management

  • 🔁 Helps reuse clean logic across many services

  • 🧼 Avoids repeating profile logic in multiple places

  • 🧪 Makes it easy to test and customize per project

  • 📦 Ready for monorepos, SaaS platforms, or admin panels


🟢 Difficulty Level: Beginner → Intermediate

Requires understanding of:

  • Express middleware and routing

  • Modular folder structuring

  • Class-based architecture

  • Clean code separation (controller, service, validators)


✅ Tasks


1. 🧱 User Model (User.model.ts)

[ ] Define schema or model fields:

  • id: string

  • name: string

  • email: string

  • avatar?: string

  • role_id?: string

  • [ ] Make it DB-agnostic (can use Mongoose/Prisma/In-Memory)

2. 📜 Validators (User.validators.ts)

  • [ ] Create validation schemas for:

  • updateUser (e.g., name/email only)

  • changePassword (if applicable)

  • [ ] Use Zod or Joi

  • [ ] Export as middleware

3. 🔧 Service Layer (User.service.ts)

  • [ ] Business logic methods:

  • getUserById(id)

  • getAllUsers()

  • updateUser(id, payload)

  • deleteUser(id)

  • [ ] Should return plain objects or throw errors

  • No Express code


4. 🎮 Controller (User.controller.ts)

  • [ ] Class UserController with bind-safe methods:

  • getUserById(req, res)

  • getAllUsers(req, res)

  • updateUser(req, res)

  • deleteUser(req, res)

  • [ ] Use corresponding service methods

  • [ ] Handle HTTP response formatting

router.get('/user/:id', userController.getUserById);

5. 🛡️ Middleware (Optional) (User.middleware.ts)

  • [ ] Add auth middleware placeholder like:

  • verifyUserOwnsAccountOrIsAdmin


6. ❗ Constants (User.constant.ts)

  • [ ] Add standard messages and error codes:
export const USER_ERRORS = {
  NOT_FOUND: 'User not found.',
  UPDATE_FAILED: 'Failed to update user.',
  UNAUTHORIZED: 'You are not allowed to access this user.'
};

7. 📘 README.md

Show how to:

  • Import and use UserController

  • Add validators before controller

  • Customize the logic

  • Keep usage copy-paste-ready

8. 🧪 Demo (User.demo.ts)

  • [ ] Use all 4 main methods:

  • Get single user

  • Get all users

  • Update a user

  • Delete a user


📁 Final Folder Structure

src/
└── features/
    └── User/
        └── v1/
            ├── User.controller.ts         # Main class
            ├── User.service.ts            # Business logic
            ├── User.validators.ts         # Validation middleware
            ├── User.constant.ts           # Error/status constants
            ├── User.utils.ts         # Utility Class 
            ├── User.model.ts              # Mongoose/DB schema
            ├── User.middleware.ts         # Optional auth/match check
            ├── User.demo.ts               # Sample usage
            └── README.md                  # Docs & setup guide


🎯 Expected Outcome

[x] Reusable UserController class

[x] Standard get, update, delete operations

[x] Middleware-friendly (e.g. verifyToken)

[x] Clean separation of service and controller logic

[x] Easily integrated with any Express project


🔐 Example Route Usage

import { verifyToken } from '../auth/Auth.middleware';
import { UserController, validateUpdateUser } from 'fastkit-user';



router.get('/users/:id', verifyToken, userController.getUserById);
router.get('/users', verifyToken, UserController.getAllUsers);
router.put('/users/:id', verifyToken, validateUpdateUser, UserController.updateUser);
router.delete('/users/:id', verifyToken, userController.deleteUser);


🙋🏻‍♂️ Looking For

Contributions to:

  • Add password update/reset endpoint

  • Support profile image upload or avatar management

  • Add admin filters: search by role, status, etc.

  • Extend with pagination and soft

Guia do colaborador