@xcelsior/nest-http-module
v1.0.2
Published
HTTP Module
Downloads
60
Keywords
Readme
@xcelsior/nest-http-module
HTTP utilities and middleware for NestJS applications in the Xcelsior ecosystem.
Installation
pnpm add @xcelsior/nest-http-moduleFeatures
- Global exception handling with structured error responses
- Request logging middleware with context tracking
- Response caching middleware
- Integration with @xcelsior/monitoring for error tracking
Usage
Module Setup
import { ExcelsiorHttpCoreModule } from '@xcelsior/nest-http-module';
@Module({
imports: [
ExcelsiorHttpCoreModule
],
})
export class AppModule {}The module automatically sets up:
- Logging middleware for all routes
- Cache middleware for all routes
- Global exception filter (needs to be registered separately)
Exception Filter
Register the global exception filter in your main.ts:
import { GlobalExceptionFilter } from '@xcelsior/nest-http-module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalFilters(new GlobalExceptionFilter());
await app.listen(3000);
}The exception filter:
- Handles all exceptions in a consistent way
- Provides structured error responses
- Integrates with @xcelsior/monitoring for error tracking
- Includes stack traces in development mode
- Logs errors appropriately based on status code
Logging Middleware
The logging middleware is automatically applied to all routes and:
- Logs request/response details
- Includes timing information
- Adds request context for correlation
- Integrates with the core module's context service
Cache Middleware
The cache middleware is automatically applied to all routes and:
- Provides response caching
- Respects cache headers
- Can be configured per-route
Configuration
Environment Variables
The module respects these environment variables:
NODE_ENV: Determines error response detail level- Other configuration is inherited from @xcelsior/nest-core-module
Error Handling
The exception filter provides structured error responses:
// For HTTP exceptions
throw new HttpException('Not found', HttpStatus.NOT_FOUND);
// Response:
{
error: {
message: 'Not found',
statusCode: 404
},
data: null
}
// For internal errors (500)
// These are automatically captured by @xcelsior/monitoring
// Stack trace included in development mode only
{
error: {
message: 'Internal server error',
stack: '...' // development only
},
data: null
}License
MIT
