framework-do-dede 2.0.17 → 2.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { UseCase } from "../protocols";
|
|
2
2
|
export declare const USE_CASE_DECORATORS: unique symbol;
|
|
3
3
|
export declare function Context(middlewareKey: string): (target: any, propertyKey: string) => void;
|
|
4
|
-
export declare function DecorateUseCase(useCases: (new (...args: any[]) => UseCase<any, any> | UseCase<any, any>
|
|
4
|
+
export declare function DecorateUseCase(useCases: UseCase<any, any> | (new (...args: any[]) => UseCase<any, any>) | Array<UseCase<any, any> | (new (...args: any[]) => UseCase<any, any>)>): ClassDecorator;
|
|
@@ -4,37 +4,40 @@ export default class UseCaseHandler {
|
|
|
4
4
|
static load(useCaseClass, request) {
|
|
5
5
|
const useCaseDecorators = Reflect.getMetadata(USE_CASE_DECORATORS, useCaseClass) || [];
|
|
6
6
|
const useCaseDecoratorsInstances = [];
|
|
7
|
+
// Processa decorators
|
|
7
8
|
for (const useCaseDecorator of useCaseDecorators) {
|
|
9
|
+
let instance;
|
|
8
10
|
if (typeof useCaseDecorator === 'function') {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const contextDecoratorsMetadata = Reflect.getMetadata('context', useCaseDecorator) || [];
|
|
12
|
-
contextDecoratorsMetadata.forEach(({ propertyKey, middlewareKey }) => {
|
|
13
|
-
if (context?.middlewareData?.[middlewareKey]) {
|
|
14
|
-
instanceDecorator[propertyKey] = context.middlewareData[middlewareKey];
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
useCaseDecoratorsInstances.push(instanceDecorator);
|
|
11
|
+
// Cria instância via DI
|
|
12
|
+
instance = Registry.classLoader(useCaseDecorator);
|
|
18
13
|
}
|
|
19
14
|
else {
|
|
20
|
-
|
|
15
|
+
// Usa instância existente
|
|
16
|
+
instance = useCaseDecorator;
|
|
21
17
|
}
|
|
18
|
+
// Injeta contexto na instância (seja nova ou existente)
|
|
19
|
+
injectContext(instance, request);
|
|
20
|
+
useCaseDecoratorsInstances.push(instance);
|
|
22
21
|
}
|
|
22
|
+
// Cria e configura a instância principal
|
|
23
23
|
const instance = Registry.classLoader(useCaseClass);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
contextMetadata.forEach(({ propertyKey, middlewareKey }) => {
|
|
27
|
-
if (context?.middlewareData?.[middlewareKey]) {
|
|
28
|
-
instance[propertyKey] = context.middlewareData[middlewareKey];
|
|
29
|
-
}
|
|
30
|
-
});
|
|
24
|
+
injectContext(instance, request);
|
|
25
|
+
// Decora o método execute
|
|
31
26
|
const originalExecute = instance.execute.bind(instance);
|
|
32
27
|
instance.execute = async (input) => {
|
|
33
|
-
for
|
|
34
|
-
await
|
|
28
|
+
for (const decoratorInstance of useCaseDecoratorsInstances) {
|
|
29
|
+
await decoratorInstance.execute(input);
|
|
35
30
|
}
|
|
36
31
|
return originalExecute(input);
|
|
37
32
|
};
|
|
38
33
|
return instance;
|
|
39
34
|
}
|
|
40
35
|
}
|
|
36
|
+
function injectContext(instance, request) {
|
|
37
|
+
const contextMetadata = Reflect.getMetadata('context', instance.constructor) || [];
|
|
38
|
+
contextMetadata.forEach(({ propertyKey, middlewareKey }) => {
|
|
39
|
+
if (request?.middlewareData?.[middlewareKey]) {
|
|
40
|
+
instance[propertyKey] = request.middlewareData[middlewareKey];
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|