Hi,
I think it would be great to have a simplified version of the interceptor mechanism, which will use the current implementation.
I suggest to create a base interceptor class, which will look like so:
export abstract class BaseInterceptor implements NestInterceptor {
protected onBefore(request?: any) {
}
protected onAfter(data?: any) {
}
intercept(dataOrRequest: any, context: ExecutionContext, stream$: Observable<any>): Observable<any> {
this.onBefore(dataOrRequest);
return stream$.map((data) => {
return this.onAfter(data);
});
}
}
And here is how the LoggingInterceptor
will look:
@Interceptor()
export class LoggingInterceptor extends BaseInterceptor {
onBefore(request: any) {
console.log('Before...');
}
onAfter(data) {
console.log('After...');
return data;
}
}
The benefit of this implementation is the simplified (in my opinion) API that it brings, WDYT?
Note: it would be even better if there we different mechanism for interception when it will require an interceptor to implement the onBefore and onAfter interfaces. This way inheritance won't be needed.
Would be more than happy to submit a PR for that if you think this is valuable.
This would require some refactoring on the internals of Nest, but I like this API. It is important, however, to keep the intercept
method intact, for more advanced users.
Hi @vlio20,
I know that the current interceptor API may be confusing a little, but the reason why we're using rxjs streams are their capabilities. The interceptors have a lot of use cases, not only the aspects things like after / before execution hooks so I'm not going to change them. What I think is that it would be great to have a base class (same as in your example) for less advanced users, so they can extend and override methods (onBefore
& onAfter
), but the best place for this class would be a utils
repository (more here). Thanks!
@kamilmysliwiec can you please leave issues open if they鈥檙e active discussions/bugs?
@wbhob opening
What do you think about adding this class to the common
package, instead of creating the new one?
@kamilmysliwiec, if you asking me, than I will be happy!
Wait wait wait - see PR #212. I know it's a lot of changes, but to get ready for that change I recommend putting it in core
.
@wbhob, @kamilmysliwiec can I PR?
Hi @vlio20,
Sorry, I have missed your message 馃檨 For sure, every time!
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
@wbhob, @kamilmysliwiec can I PR?