question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Suggestion] - add simplified class for interceptor

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
vlio20commented, Nov 11, 2017
0reactions
lock[bot]commented, Sep 25, 2019

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

simple interceptor help - Google Groups
I was trying to create some simple interceptor code, but have ... public class ExampleInterceptor implements MethodInterceptor {.
Read more >
10 Using Service Interceptors to Manipulate Requests
Custom interceptors that you develop can be designed to access and modify certain elements of the data in RequestContext. Interceptors can set the...
Read more >
How to Create and Use Interceptors in Angular - Scaler
Interceptors are an excellent way to modify the HTTP request or response to make it more simple and understandable.
Read more >
Spring MVC Interceptor HandlerInterceptorAdapter ...
HandlerInterceptorAdapter is abstract adapter class for the HandlerInterceptor interface, for simplified implementation of ...
Read more >
Is there a way to composite Interceptors - java - Stack Overflow
You would need to write your own generic dispatcher. I would suggest you to create some form of generic dispatcher which then delegate ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found