Nest.js中的嵌套拦截器

时间:2020-09-09 16:11:41

标签: node.js typescript nestjs

如何在Nest.js中实现嵌套拦截器?

我有两个拦截器:UsersInterceptorPostsInterceptor

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时将user的结果放入PostsInterceptor中的Binding<Bool>对象中。

1 个答案:

答案 0 :(得分:0)

As described in the request lifecycle docs,拦截器按照绑定的顺序处理传入的请求,并按照绑定的相反顺序处理传出的响应。这意味着,如果您像Item { id:main_container width: parent.width height: parent.height //////////////////////js/////////////////////////////////////// function test(){ var foo = new Array(2); for(var i = 0; i < foo.length; i++){ console.log("i am here"); var newObject = Qt.createQmlObject('import QtQuick 2.0; Rectangle { color: "red"; border.color: "black"; border.width: 5; width: 150; height: 150}',body);//i noticed Layout.preferedWidth & height here occurs error so i have to use width and height } } //////////////////////////////// Rectangle{ id: home_main_container anchors.fill: parent Rectangle{ id:header width: parent.width height: parent.height/10 anchors.top: parent.top color: b1 } Rectangle{ id:body width: parent.width height: parent.height*(8/10) anchors.top: header.bottom anchors.horizontalCenter: parent.horizontalCenter ColumnLayout{ id:body_part1 width: parent.width height: parent.height/4 anchors.top: parent.bottom Component.onCompleted: test(); } } Rectangle{ id:footer width: parent.width height: parent.height/10 anchors.bottom: parent.bottom color: b1 } } } 这样绑定拦截器,那么您应该能够在@UseInterceptors(PostInterceptor, UserInterceptor)

中看到UserInterceptor的返回