api-gateway-cli-tools
v1.0.0
Published
CLI para generar configuraciones y codigo para el API Gateway
Maintainers
Readme
GlobalS1 API Gateway CLI
CLI para generar configuraciones y codigo para el API Gateway de GlobalS1.
Instalacion
# Global
npm install -g globalsone-api-gateway-cli
# O como devDependency
npm install -D globalsone-api-gateway-cliUso
# Ver ayuda
apigw --help
# Ver version
apigw --versionComandos
Generate Middleware
Genera middlewares personalizados para el API Gateway.
# Middleware basico
apigw generate middleware request-logger
apigw g mw request-logger
# Middleware JWT
apigw g mw custom-auth --type jwt
# Middleware Rate Limit con Redis
apigw g mw api-rate-limit --type redis
# Middleware Circuit Breaker
apigw g mw service-protection --type circuit-breaker
# Preview sin crear archivos
apigw g mw my-middleware --dry-runOpciones:
| Opcion | Descripcion | Default |
|--------|-------------|---------|
| -t, --type <type> | Tipo de middleware (basic, jwt, redis, circuit-breaker) | basic |
| --dry-run | Preview sin crear archivos | false |
| --force | Sobrescribir archivos existentes | false |
Generate Upstream
Configura servicios upstream para el proxy.
# Upstream basico
apigw generate upstream user-service --host localhost --port 3001
apigw g up user-service -h localhost -p 3001
# Con paths especificos
apigw g up order-service --port 3002 --paths "/orders,/checkout"
# Con health check
apigw g up payment-service --port 3003 --health "/health/live"
# Con load balancing
apigw g up api-service --instances "api-1:3000,api-2:3000" --strategy round-robin
# Con circuit breaker
apigw g up external-api --port 443 --protocol https --circuit-breaker
# Preview
apigw g up test-service --dry-runOpciones:
| Opcion | Descripcion | Default |
|--------|-------------|---------|
| -h, --host <host> | Host del servicio | localhost |
| -p, --port <port> | Puerto del servicio | 3001 |
| --protocol <protocol> | Protocolo (http, https) | http |
| --paths <paths> | Paths manejados (separados por coma) | |
| --health <path> | Endpoint de health check | /health |
| --timeout <ms> | Timeout en ms | 5000 |
| --retries <n> | Numero de reintentos | 3 |
| --circuit-breaker | Habilitar circuit breaker | false |
| --instances <instances> | Instancias para load balancing | |
| --strategy <strategy> | Estrategia de load balancing | round-robin |
| --dry-run | Preview sin crear archivos | false |
| --force | Sobrescribir configuracion | false |
Generate Route
Configura rutas para el proxy.
# Ruta simple
apigw generate route /api/users --upstream user-service
apigw g r /api/users -u user-service
# Ruta con metodos especificos
apigw g r /api/orders --upstream order-service --methods "GET,POST"
# Ruta con middlewares
apigw g r /api/payments --upstream payment-service --middlewares "auth,rate-limit"
# Ruta con rate limit custom
apigw g r /api/auth/login --upstream auth-service --rate-limit "5/min"
# Ruta con cache
apigw g r /api/products --upstream catalog-service --cache 300
# Preview
apigw g r /api/test --upstream test-service --dry-runOpciones:
| Opcion | Descripcion | Default |
|--------|-------------|---------|
| -u, --upstream <name> | Servicio upstream (requerido) | |
| -m, --methods <methods> | Metodos HTTP | GET,POST,PUT,DELETE |
| --middlewares <list> | Middlewares a aplicar | |
| --rate-limit <rate> | Rate limit (ej: 5/min) | |
| --timeout <ms> | Timeout especifico | |
| --strip-prefix | Eliminar prefijo del path | false |
| --cache <ttl> | TTL de cache en segundos | |
| --dry-run | Preview sin crear archivos | false |
| --force | Sobrescribir configuracion | false |
Generate Validator
Genera validadores de request con AJV.
# Validador basico
apigw generate validator create-user
apigw g v create-user
# Validador con metodo especifico
apigw g v update-order --method PUT
# Preview
apigw g v my-validator --dry-runOpciones:
| Opcion | Descripcion | Default |
|--------|-------------|---------|
| -m, --method <method> | Metodo HTTP | POST |
| -s, --schema <schema> | Nombre del schema | |
| --dry-run | Preview sin crear archivos | false |
| --force | Sobrescribir archivos | false |
Generate Health
Genera health checks personalizados.
# Health check basico
apigw generate health basic
apigw g h basic
# Health check con dependencias
apigw g h readiness --deps "redis,database"
# Health check para servicio especifico
apigw g h user-health --service user-service
# Preview
apigw g h custom --dry-runOpciones:
| Opcion | Descripcion | Default |
|--------|-------------|---------|
| -d, --deps <deps> | Dependencias a verificar | |
| -s, --service <service> | Servicio especifico | |
| --dry-run | Preview sin crear archivos | false |
| --force | Sobrescribir archivos | false |
Estructura Generada
src/
├── config/
│ ├── upstream-services.json # Servicios upstream
│ ├── rate-limits.json # Configuracion rate limiting
│ └── routes.json # Definicion de rutas
├── middlewares/
│ ├── {name}.middleware.ts # Middlewares generados
│ └── __tests__/
│ └── {name}.middleware.test.ts
├── validators/
│ ├── {name}.validator.ts # Validadores generados
│ └── __tests__/
│ └── {name}.validator.test.ts
└── services/
└── {name}-health.service.ts # Health checks generadosDesarrollo
# Clonar repositorio
git clone https://github.com/GlobalS1/globalsone-api-gateway-cli.git
cd globalsone-api-gateway-cli
# Instalar dependencias
npm install
# Build
npm run build
# Desarrollo (watch mode)
npm run dev
# Link local para testing
npm linkLicencia
MIT
