Created
September 10, 2019 14:52
-
-
Save ahmadarif/94f287213eb1b0cec68df3297ef3924f to your computer and use it in GitHub Desktop.
Nest - UndefinedInterceptor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
import { map } from 'rxjs/operators'; | |
@Injectable() | |
export class UndefinedInterceptor implements NestInterceptor { | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
return next | |
.handle() | |
.pipe(map(value => this.traverse(value))); | |
} | |
traverse(data: any) { | |
for (const key in data) { | |
if (typeof data[key] === 'undefined') { | |
data[key] = null; | |
} | |
if (data[key] !== null && typeof(data[key]) === 'object') { | |
this.traverse(data[key]); | |
} | |
} | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this line to
main.ts
file