nestjs-cls icon indicating copy to clipboard operation
nestjs-cls copied to clipboard

Add @UseCls decorator

Open Papooch opened this issue 3 years ago • 2 comments

(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 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 avatar Feb 14 '22 13:02 Papooch

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

Papooch avatar Jun 19 '22 14:06 Papooch

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

micalevisk avatar Jul 30 '22 23:07 micalevisk