jeudi 2 mai 2019

NestJS: Get current method name using reflect, via reflect DI data?

I think it maybe possible to get the current method name while inside nestjs. I don't really mean in an interceptor but using reflect.getMetaData or something similar.

What am I trying to do ?

Well in each method where i need a logger I am executing the following line of code

  createTerminusOptions(): TerminusModuleOptions {
    const logger = this.loggerFactoryService.getLogger({ category: `${TerminusOptionsService.name}::createTerminusOptions` })

As you can see, the TerminusOptionsService.name is strongly typed so if I refactor the name then it would break and be caught by the compiler. The method name is createterminusOptions but I am placing this inside a string. I would like to try and get this programmatically or strongly typed so that if I refactor then it either automatically changes OR it would throw an error as the name of the method has changed.

I know it was possible to use arguments.callee but this is banned in strict mode.

I think there is some metadata saved in the nestjs type system so I was hoping to be able to use something here ?

Of course, this would be classed as reflection so I don't want to add a large overhead - if this is possible, do you think its a bad idea because it would of a performance hit ?

Any ideas really appreciated

Thanks





Aucun commentaire:

Enregistrer un commentaire