Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interceptor on service method #1923

Closed
nguyeda opened this issue Apr 2, 2019 · 2 comments
Closed

interceptor on service method #1923

nguyeda opened this issue Apr 2, 2019 · 2 comments

Comments

@nguyeda
Copy link

nguyeda commented Apr 2, 2019

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ ] Feature request
[x] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Hello, I'm discovering Nestjs and got a question about interceptors: is it possible to implement an interceptor on something that is not a controller ? I want to implement a custom cache using a decorator. I have a FooController that calls a FooService that calls a FooRepository. FooRepository calls a REST endpoint that give me limited rate and latency so I want to be able to cache that result. I can't do the caching a FooController level because FooService is creating a JWT token.

@Controller('foo')
export class FooController {
  constructor(private readonly fooService: FooService) {}
  @Get(':whatever')
  public async getFoo(@Param('whatever') whatever: string) {
    return this.fooService.getFoo(whatever);
  }
}

@Injectable()
export class FooService {
  constructor(private readonly fooRepository: FooRepository) {}
  public async getFoo(whatever: string) {
    const res = this.fooRepository.getFoo(whatever);
    // do some other things
    return res;
  }
}

@Injectable()
export class FooRepository {
  @Cached()
  public async getFoo(whatever: string) {
    return http.get(...)
  }
}

I currently use my own @cached typescript decorator.

export function Cached() {
  return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
    const origin = descriptor.value;

    descriptor.value = async function(...args: any[]) {
      // check cache or call origin
      return origin.apply(this, args);
    }
  }
}

But what if I want to use nestjs resources there? (for instance an injectable redis client). I'm looking for something similar to the NestInterceptor implementation (https://docs.nestjs.com/interceptors).

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Apr 3, 2019

Is it possible to implement an interceptor on something that is not a controller

I believe that it was discussed a couple of times already. I'd suggest looking through existing, closed issues, you will surely find a more detailed answer.

TL;DR - it's impossible

@lock
Copy link

lock bot commented Sep 23, 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.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants