framework-do-dede 2.0.18 → 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.
@@ -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
- const instanceDecorator = Registry.classLoader(useCaseDecorator);
10
- const context = request;
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
- useCaseDecoratorsInstances.push(useCaseDecorator);
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
- const context = request;
25
- const contextMetadata = Reflect.getMetadata('context', useCaseClass) || [];
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 await (const useCaseDecoratorsInstance of useCaseDecoratorsInstances) {
34
- await useCaseDecoratorsInstance.execute(input);
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framework-do-dede",
3
- "version": "2.0.18",
3
+ "version": "2.0.19",
4
4
  "type": "module",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",