Skip to content

Latest commit

 

History

History
70 lines (46 loc) · 3.29 KB

File metadata and controls

70 lines (46 loc) · 3.29 KB

Typescript REST API

This repository was developed while following Fernando Herrera's Node.js course. It focuses on the implementation of a REST API using Express, MongoDB, and JSON Web Tokens (JWT), covering authentication, authorization, input validation, and CRUD operations.

Although the original course uses JavaScript, this project was later migrated to TypeScript as a personal extension to improve type safety and maintainability while preserving the original API behavior.

The API includes users, roles, categories, products, and search endpoints, providing a practical backend architecture and a solid foundation for REST API development.

Key Features

  • Authentication with JSON Web Tokens (JWT).
  • Role-based authorization for protected endpoints.
  • Input validation with express-validator.
  • CRUD operations for users, categories, and products.
  • Search across users, categories, and products.
  • Pagination for list endpoints.

API Documentation

The complete OpenAPI 3.0 specification is available in docs/openapi.yaml. It documents every endpoint, request body, response, validation rule, and authentication requirement implemented by the API.

You can inspect or import the specification with any OpenAPI-compatible tool, including Swagger Editor, Postman, and Insomnia.

Configuration

Prerequisites: Node.js 20+, npm, and MongoDB (local or Atlas).

  1. Clone this repository and install dependencies with npm ci.
  2. Copy .env.example to .env and set all variables:
    • PORT: HTTP port (defaults to 3000 when omitted).
    • MONGODB_CNN: MongoDB connection string.
    • SECRETORPRIVATEKEY: long, random secret used to sign JWTs.
  3. Create role documents in MongoDB before registering users. The API expects roles named admin_rol and/or ventas_rol; use each role document's _id as the rol field when creating a user.
  4. Run the application with npm run dev.

The server is available at http://localhost:3000 by default. Never commit .env or real credentials.

Usage

API Endpoints

Resource Endpoint Description
Authentication POST /api/auth/login Authenticates a user and returns a JWT.
Users /api/usuarios Lists, creates, updates, and soft-deletes users.
Categories /api/categorias Lists, creates, updates, and soft-deletes categories.
Products /api/productos Lists, creates, updates, and soft-deletes products.
Search GET /api/buscar/:coleccion/:termino Searches users, categories, or products.

Pagination

List endpoints support pagination through query parameters:

  • Users and categories: ?limite=5&desde=0
  • Products: ?limit=10&from=0

The parameter names are currently inconsistent between resources and will be standardised in a future iteration.

Testing status

This project does not currently have a maintained automated test suite. The existing test is a legacy integration test that depends on pre-existing MongoDB data and may write a product, so it is not a reliable verification step for a fresh clone. It should not be treated as evidence of test coverage until it is isolated with dedicated fixtures and a test database.

Contact

If you have any questions or suggestions, please don't hesitate to contact me at calvarez.brc@gmail.com