// PRJ_009 · 2024
API de envíos NestJSNestJS Shipment API
Backend · NestJSBackend · NestJS
Una API para registrar envíos que calcula sola la tarifa según la distancia.An API to register shipments that calculates the fee from the distance on its own.
En detalleIn detailAPI NestJS para gestión de envíos: registro, cálculo automático de tarifas por distancia e historial completo. Arquitectura hexagonal (domain → use cases → controllers), validación con ValidationPipe, tests con Jest y CI en GitHub Actions. Dockerizado.NestJS API for shipment management: registration, automatic tariff calculation by distance and full history. Hexagonal architecture (domain → use cases → controllers), input validation with ValidationPipe, Jest tests and CI on GitHub Actions. Dockerized.
// IMPACTO// IMPACT
El problemaThe problem
Quería construir una API de gestión de envíos con buenas prácticas de NestJS: módulos, controllers, services bien separados, cálculo automático de tarifas y un entorno reproducible con Docker.I wanted to build a shipment management API showcasing NestJS best practices: clean module/controller/service separation, automatic tariff calculation and a reproducible Docker environment.
Cómo lo resolvíHow I solved it
API NestJS con arquitectura hexagonal que registra envíos, calcula la tarifa automáticamente según la distancia y expone el historial completo. Validación con ValidationPipe, tests con Jest y CI. Lista para producción con Docker.NestJS API with hexagonal architecture that registers shipments, automatically calculates the fee based on distance and exposes the full history. Input validation with ValidationPipe, Jest tests and CI. Production-ready with Docker.
Arquitectura y stackArchitecture & stack
- [01]Client
- [02]Controller
- [03]Use Cases
- [04]Domain
- [05]Docker
- NestJS
- TypeScript
- Jest
- Docker
// create-shipment.usecase.ts — fee calculation
async execute(request: CreateShipmentRequest): Promise<Shipment> {
const fee = this.calculateShipmentFee(request.distance);
return this.shipmentRepository.create(
new Shipment(null, request.recipient, request.sender,
request.content, new Date(), request.distance, fee),
);
}
calculateShipmentFee(distance: number): number {
return BASE_RATE + distance * RATE_PER_KM;
} Lo que aprendíWhat I learned
01 //
Módulos NestJS
La modularidad de NestJS fuerza buenas prácticas desde el inicio.
02 //
DTOs + class-validator
Validar en la entrada es más barato que manejar errores adentro.
03 //
Docker desde cero
Partir con un Dockerfile limpio define el entorno para siempre.
01 //
NestJS Modules
NestJS modularity enforces good practices from the start.
02 //
DTOs + class-validator
Validating at the boundary is cheaper than handling errors inside.
03 //
Docker from scratch
Starting with a clean Dockerfile defines the environment permanently.