I Have created Interceptor and Decorator for TransformingResponses in NestJS. Interceptors looks like this:
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { map, Observable } from 'rxjs';
@Injectable()
export class TransformInterceptor implements NestInterceptor {
constructor(private reflector: Reflector) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const metadata = this.reflector.get<string[]>('TRANSFORM_RESPONSE', context.getHandler());
if (metadata) {
return next.handle().pipe(map((data) => ({ result: data })));
}
return next.handle();
}
}
Issue is that when the service returns null ( entity do not exists in the db or whatever ) this transform response is trying to map the response from the service to some particular DTO. And then in this DTO we are destructing or what ever but when is null you know what is happening...
My question is how to catch this null which is coming from the service in the interceptor before passing it to the DTO and prevent going that route if there is no response from the service