【发布时间】:2020-12-28 02:48:43
【问题描述】:
如何在 Nest.js 中实现嵌套拦截器?
我有两个拦截器:UsersInterceptor 和 PostsInterceptor
UsersInterceptor:
@Injectable()
export class UsersInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
email: data.email,
createdAt: data.createdAt
})));
}
}
PostsInterceptor:
@Injectable()
export class PostsInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
title: data.title,
content: data.content,
user: {}, // I want the UsersInterceptor result here. I have the user's data in data.user
createdAt: data.createdAt
})));
}
}
现在,我想在使用时将UserInterceptor 的结果放入PostsInterceptor 中的user 对象中。
【问题讨论】:
标签: node.js typescript nestjs