Interceptors
Microservice interceptors function no differently from normal API interceptors. The only difference is that the interceptor is passed the data object sent to the microservice handler instead of an API request object. This means you can actually write interceptors once and use them in both contexts. Just like API interceptors, microservice interceptors are executed before the microservice handler and must return an Observable. To secure our rpcShow
microservice endpoint, we will create a new interceptor that will expect a User
database object and remove the password
field.
@
Injectable
()
export
class
CleanUserInterceptor
implements
NestInterceptor
{
intercept
(
context
:ExecutionContext
,
stream$
:Observable
<
any
>
)
:
Observable
<
any
>
{
return
stream$
.
pipe
(
map
(
user
=>
JSON
.
parse
(
JSON
.
stringify
(
user
))),
map
(
user
=>
{
return
{
...
user
,
password
:undefined
...