@@ -3,6 +3,8 @@ import { copyMethodMetadata } from '../../utils/copy-method-metadata';
33import { ClsServiceManager } from '../cls-service-manager' ;
44import { CLS_ID } from '../cls.constants' ;
55import { ClsDecoratorOptions } from '../cls.options' ;
6+ import { ClsDecoratorInitContext } from '../plugin/cls-plugin.interface' ;
7+ import { ClsPluginsHooksHost } from '../plugin/cls-plugin-hooks-host' ;
68
79/**
810 * Wraps the decorated method in a CLS context.
@@ -42,24 +44,37 @@ export function UseCls<TArgs extends any[]>(
4244 `The @UseCls decorator can be only used on functions, but ${ propertyKey . toString ( ) } is not a function.` ,
4345 ) ;
4446 }
45- descriptor . value = function ( ...args : TArgs ) {
46- return cls . run ( options . runOptions ?? { } , async ( ) => {
47- if ( options . generateId ) {
48- const id = await options . idGenerator ?. apply ( this , args ) ;
49- cls . set < string > ( CLS_ID , id ) ;
50- }
51- if ( options . setup ) {
52- await options . setup . apply ( this , [ cls , ...args ] ) ;
53- }
54- if ( options . initializePlugins ) {
55- await cls . initializePlugins ( ) ;
56- }
57- if ( options . resolveProxyProviders ) {
58- await cls . resolveProxyProviders ( ) ;
59- }
60- return original . apply ( this , args ) ;
61- } ) ;
62- } ;
47+ descriptor . value = new Proxy ( original , {
48+ apply : function ( _ , outerThis , args : TArgs [ ] ) {
49+ const pluginHooks = ClsPluginsHooksHost . getInstance ( ) ;
50+ return cls . run ( options . runOptions ?? { } , async ( ) => {
51+ const pluginCtx : ClsDecoratorInitContext = {
52+ kind : 'decorator' ,
53+ args,
54+ } ;
55+ if ( options . initializePlugins ) {
56+ await pluginHooks . beforeSetup ( pluginCtx ) ;
57+ }
58+ if ( options . generateId ) {
59+ const id = await options . idGenerator ?. apply (
60+ outerThis ,
61+ args ,
62+ ) ;
63+ cls . set < string > ( CLS_ID , id ) ;
64+ }
65+ if ( options . setup ) {
66+ await options . setup . apply ( outerThis , [ cls , ...args ] ) ;
67+ }
68+ if ( options . initializePlugins ) {
69+ await pluginHooks . afterSetup ( pluginCtx ) ;
70+ }
71+ if ( options . resolveProxyProviders ) {
72+ await cls . resolveProxyProviders ( ) ;
73+ }
74+ return original . apply ( outerThis , args ) ;
75+ } ) ;
76+ } ,
77+ } ) ;
6378 copyMethodMetadata ( original , descriptor . value ) ;
6479 } ;
6580}
0 commit comments