Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
434 stars 28 forks source link

Add @UseCls decorator #19

Closed Papooch closed 1 year ago

Papooch commented 2 years ago

(relates to #18)

Consider adding a @UseCls() method decorator to set up the context for a method automagically, when it is run outside of a request context.

// This is currently NOT supported

@Injectable()
export class CronController {
  constructor(
     private readonly cls: ClsService,
     private readonly someService: SomeService
  )

  @Cron('45 * * * * *')
  @UseCls()
  handleCron() {
     this.cls.set(CLS_ID, uuid())
     this.someService.doTheThing();
  }
}

The decorator would need to replace the method's implementation by wrapping the actual call with the cls.run() method. This would be the prefered way of entering the CLS context for invoking CLS-aware code that does not go through a controller, where a middleware or enhancer take care of setting up the context.

Papooch commented 2 years ago

Delaying this feature after I figure out #30 It should also seamlessly resolve Proxy Providers.

micalevisk commented 2 years ago

I guess you missed the declaration of cls field on CronController class