Skip to content

Instantly share code, notes, and snippets.

@micaiah-effiong
Created April 12, 2023 12:38
Show Gist options
  • Select an option

  • Save micaiah-effiong/59447b634718cba3b92e65136b0594bb to your computer and use it in GitHub Desktop.

Select an option

Save micaiah-effiong/59447b634718cba3b92e65136b0594bb to your computer and use it in GitHub Desktop.
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Response } from 'express';
import { map, Observable } from 'rxjs';
@Injectable()
export class ResponseTransformerInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const httpCtx = context.switchToHttp();
const response = httpCtx.getResponse<Response>();
const status = response.statusCode;
return next.handle().pipe(
map((data) => {
if (status < 400) {
return {
statusCode: status,
result: data,
};
} else {
return data;
}
}),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment