A NestJS package that globally standardizes HTTP responses with consistent success and error formats.
npm install nestjs-response-wrapperThis package has the following peer dependencies:
npm install @nestjs/common @nestjs/core rxjs- Global Response Interceptor: Wraps all successful REST responses in a consistent format
- Global Exception Filter: Catches and transforms all thrown errors into a standardized format
- Configurable: Customize timestamp inclusion, custom formatters, excluded routes, and error logging
- TypeScript Support: Full TypeScript support with type definitions
- NestJS v10+ Compatible: Works with NestJS v10 and above
- Global Module: Automatically available across all modules without explicit imports
Import ResponseWrapperModule in your root module:
import { Module } from '@nestjs/common';
import { ResponseWrapperModule } from 'nestjs-response-wrapper';
@Module({
imports: [ResponseWrapperModule.forRoot()],
})
export class AppModule {}import { Module } from '@nestjs/common';
import { ResponseWrapperModule } from 'nestjs-response-wrapper';
@Module({
imports: [
ResponseWrapperModule.forRoot({
includeTimestamp: true,
excludeRoutes: ['/health', '/health*'],
logErrors: true,
customSuccessFormatter: (data) => ({
success: true,
data,
timestamp: new Date().toISOString(),
}),
customErrorFormatter: (error) => ({
message: error instanceof Error ? error.message : 'Unknown error',
code: 'ERROR_CODE',
}),
}),
],
})
export class AppModule {}{
"success": true,
"data": <original_response>,
"timestamp": "2024-01-01T00:00:00.000Z"
}{
"success": false,
"error": "Error message",
"timestamp": "2024-01-01T00:00:00.000Z"
}| Option | Type | Default | Description |
|---|---|---|---|
includeTimestamp |
boolean |
true |
Include ISO timestamp in responses |
customSuccessFormatter |
function |
undefined |
Custom formatter for success responses |
customErrorFormatter |
function |
undefined |
Custom formatter for error responses |
excludeRoutes |
string[] |
[] |
Routes to exclude from wrapping |
logErrors |
boolean |
false |
Enable error logging |
The excludeRoutes option supports exact matches and wildcards:
ResponseWrapperModule.forRoot({
excludeRoutes: [
'/health', // Exact match
'/health*', // Prefix match (wildcard)
],
})The package exports the following types:
import {
ApiResponse,
ApiErrorResponse,
ResponseWrapperOptions,
} from 'nestjs-response-wrapper';MIT