import { CallHandler , ExecutionContext , Injectable , NestInterceptor } from '@nestjs/common' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { isObject , isArray } from 'lodash' ; import { classToPlain } from 'class-transformer' ; export interface Response < T > { data : T ; } @ Injectable ( ) export class ResponseInterceptor < T > implements NestInterceptor < T , Response < T > > { intercept ( context , next ) { if ( context . getType ( ) === 'graphql' ) return next . handle ( ) ; return next . handle ( ) . pipe ( map ( ( data ) => { if ( data ?. data ) { return { ... data , data : isObject ( data . data ) ? this . transformResponse ( data . data ) : data . data , } ; } return { data : isObject ( data ) ? this . transformResponse ( data ) : data , } ; } ) ) ; } private transformResponse ( response ) { if ( isArray ( response ) ) { return response . map ( ( item ) => this . transformToPlain ( item ) ) ; } return this . transformToPlain ( response ) ; } private transformToPlain ( plainOrClass ) { return plainOrClass && plainOrClass . constructor !== Object ? classToPlain ( plainOrClass ) : plainOrClass ; } }