@zanobijs/core 1.0.0-beta.2 → 1.0.0-beta.4
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.
- package/CHANGELOG.md +1 -1
- package/README.md +199 -5
- package/exceptions/constant.message.d.ts +1 -0
- package/exceptions/constant.message.js +8 -1
- package/exceptions/resolution.exception.d.ts +13 -0
- package/exceptions/resolution.exception.js +25 -0
- package/factory.d.ts +5 -1
- package/factory.js +22 -2
- package/index.d.ts +1 -0
- package/index.js +1 -0
- package/injector/injector.d.ts +6 -0
- package/injector/injector.js +25 -0
- package/injector/module.d.ts +1 -0
- package/injector/module.js +21 -3
- package/interfaces/factory.interface.d.ts +4 -0
- package/interfaces/factory.interface.js +2 -0
- package/interfaces/index.d.ts +1 -0
- package/{__test__/mocks → interfaces}/index.js +1 -1
- package/package.json +3 -4
- package/tsconfig.build.json +1 -1
- package/__test__/factory.spec.d.ts +0 -1
- package/__test__/factory.spec.js +0 -33
- package/__test__/factory.spec.ts +0 -37
- package/__test__/injector/injector.spec.d.ts +0 -1
- package/__test__/injector/injector.spec.js +0 -32
- package/__test__/injector/injector.spec.ts +0 -32
- package/__test__/injector/module.spec.d.ts +0 -1
- package/__test__/injector/module.spec.js +0 -60
- package/__test__/injector/module.spec.ts +0 -66
- package/__test__/metadata.spec.d.ts +0 -1
- package/__test__/metadata.spec.js +0 -70
- package/__test__/metadata.spec.ts +0 -71
- package/__test__/mocks/classModule.mock.d.ts +0 -10
- package/__test__/mocks/classModule.mock.js +0 -74
- package/__test__/mocks/classModule.mock.ts +0 -55
- package/__test__/mocks/classWithDependeciesClass.mock.d.ts +0 -13
- package/__test__/mocks/classWithDependeciesClass.mock.js +0 -40
- package/__test__/mocks/classWithDependeciesClass.mock.ts +0 -21
- package/__test__/mocks/classWithDependeciesInject.mock.d.ts +0 -24
- package/__test__/mocks/classWithDependeciesInject.mock.js +0 -74
- package/__test__/mocks/classWithDependeciesInject.mock.ts +0 -47
- package/__test__/mocks/index.d.ts +0 -1
- package/__test__/mocks/index.ts +0 -1
- package/exceptions/constant.message.ts +0 -1
- package/exceptions/index.ts +0 -2
- package/exceptions/invalid.module.exception.ts +0 -16
- package/factory.ts +0 -69
- package/index.ts +0 -2
- package/injector/index.ts +0 -2
- package/injector/injector.ts +0 -89
- package/injector/module.ts +0 -149
- package/interface/index.d.ts +0 -0
- package/interface/index.js +0 -1
- package/interface/index.ts +0 -1
- package/metadata.ts +0 -178
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -11
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { Controller, Inject, Injectable } from "@zanobijs/common";
|
|
2
|
-
|
|
3
|
-
@Injectable()
|
|
4
|
-
export class ServiceToController2 {
|
|
5
|
-
constructor() {}
|
|
6
|
-
getHello(){
|
|
7
|
-
return "Hello ServiceToController2";
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
@Injectable()
|
|
13
|
-
export class ServiceWithDepenParam {
|
|
14
|
-
constructor(
|
|
15
|
-
private servicio: ServiceToController2,
|
|
16
|
-
@Inject("USER_NAME") private userName: string,
|
|
17
|
-
@Inject("USER_AGE") private userAge: number,
|
|
18
|
-
@Inject("USER_LOGIN") private userLogin: boolean,
|
|
19
|
-
@Inject("USER_LIKE") private userHobbis: string[],
|
|
20
|
-
) {}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@Controller()
|
|
25
|
-
export class ControllerWithDepen2 {
|
|
26
|
-
constructor(
|
|
27
|
-
private sContro2: ServiceToController2,
|
|
28
|
-
@Inject("API_KEY") private apiKey: string,
|
|
29
|
-
) {
|
|
30
|
-
this.apiKey = apiKey;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
getApiKey() {
|
|
34
|
-
return this.apiKey;
|
|
35
|
-
}
|
|
36
|
-
getHelloService() {
|
|
37
|
-
return this.sContro2.getHello();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
@Injectable()
|
|
42
|
-
export class ServiceWithDepen2 {
|
|
43
|
-
constructor(
|
|
44
|
-
private serviceWithDepenParam: ServiceWithDepenParam,
|
|
45
|
-
@Inject("API_KEY") private api_key: string,
|
|
46
|
-
) {}
|
|
47
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./classModule.mock";
|
package/__test__/mocks/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./classModule.mock"
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const MODULE_INVALID_ANNOTATION_ERROR = () => `The class must have an annotation @Module()`;
|
package/exceptions/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { RuntimeException } from "@zanobijs/common/exceptions/runtime.exception";
|
|
2
|
-
import { MODULE_INVALID_ANNOTATION_ERROR } from "./constant.message";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Excepción lanzada cuando una clase Modulo no tiene el decorador `@Module`
|
|
6
|
-
* de @zanobijs/common
|
|
7
|
-
*
|
|
8
|
-
* @remarks
|
|
9
|
-
* Esta clase extiende la base `RuntimeException`de @zanobijs/common
|
|
10
|
-
* para proporcionar detalles adicionales específicos a esquemas de módulos inválidos.
|
|
11
|
-
*/
|
|
12
|
-
export class InvalidModuleAnnotationException extends RuntimeException {
|
|
13
|
-
constructor(detail: any = ``) {
|
|
14
|
-
super(MODULE_INVALID_ANNOTATION_ERROR(), detail);
|
|
15
|
-
}
|
|
16
|
-
}
|
package/factory.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import {AwilixContainer, InjectionMode, createContainer } from "awilix";
|
|
3
|
-
import { Module } from "./injector/module";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Factory es una clase que facilita la creación y configuración de
|
|
7
|
-
* contenedores de inyección de dependencias utilizando metadatos y
|
|
8
|
-
* la librería `awilix` para registrar y resolver controladores y
|
|
9
|
-
* servicios.
|
|
10
|
-
*/
|
|
11
|
-
export class Factory {
|
|
12
|
-
private moduleHandler: Module;
|
|
13
|
-
private registeredClasses = {};
|
|
14
|
-
private container: AwilixContainer<any>;
|
|
15
|
-
|
|
16
|
-
constructor(appModule: any) {
|
|
17
|
-
this.moduleHandler = new Module();
|
|
18
|
-
this.registerClassesFromModule(appModule);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Este método registra clases desde el módulo proporcionado
|
|
23
|
-
* y recorre sus importaciones de forma recursiva para registrar
|
|
24
|
-
* las clases necesarias de importación.
|
|
25
|
-
* @private
|
|
26
|
-
*/
|
|
27
|
-
private registerClassesFromModule(module: any): void {
|
|
28
|
-
this.registerFromModule(module);
|
|
29
|
-
const importedModules = this.moduleHandler.getImports();
|
|
30
|
-
if (importedModules && importedModules.length) {
|
|
31
|
-
importedModules.forEach((moduleImport) => {
|
|
32
|
-
this.registerClassesFromModule(moduleImport);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Registra clases desde un módulo específico.
|
|
39
|
-
* @param {any} module - Módulo desde el que se registrarán las clases.
|
|
40
|
-
* @private
|
|
41
|
-
*/
|
|
42
|
-
private registerFromModule(module: any): void {
|
|
43
|
-
this.moduleHandler.setup(module);
|
|
44
|
-
this.moduleHandler.initialize();
|
|
45
|
-
Object.assign(
|
|
46
|
-
this.registeredClasses,
|
|
47
|
-
this.moduleHandler.getRegisterClass(),
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Crea el contenedor de inyección de dependencias y registra las clases.
|
|
53
|
-
* @returns {Factory} - Instancia actual de la fábrica.
|
|
54
|
-
*/
|
|
55
|
-
create(): Factory {
|
|
56
|
-
this.container = createContainer({ injectionMode: InjectionMode.CLASSIC });
|
|
57
|
-
this.container.register(this.registeredClasses);
|
|
58
|
-
return this;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Resuelve y devuelve una instancia del contenedor basado en la entidad proporcionada.
|
|
63
|
-
* @param {string} entity - Nombre de la entidad a resolver.
|
|
64
|
-
* @returns {any} - Instancia resuelta.
|
|
65
|
-
*/
|
|
66
|
-
get<T>(entity: string): T {
|
|
67
|
-
return this.container.resolve(entity)
|
|
68
|
-
}
|
|
69
|
-
}
|
package/index.ts
DELETED
package/injector/index.ts
DELETED
package/injector/injector.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { ILoggerService, IModuleConfig } from "@zanobijs/common";
|
|
2
|
-
import { Metadata } from "../metadata";
|
|
3
|
-
import { Logger } from "@zanobijs/common/utils";
|
|
4
|
-
import { isEmpty } from "@zanobijs/common/utils/shared.utils";
|
|
5
|
-
import { asClass } from "awilix";
|
|
6
|
-
|
|
7
|
-
export type Constructor<T> = { new (...args: any[]): T }
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* La clase `Injector` es la encargada de manejar la inyección de dependencias
|
|
11
|
-
* solo para parametros tipo objecto { provider, useValue }
|
|
12
|
-
*/
|
|
13
|
-
export class Injector {
|
|
14
|
-
private module: IModuleConfig;
|
|
15
|
-
private listProviders = new Map();
|
|
16
|
-
private metadata: Metadata;
|
|
17
|
-
private logger: ILoggerService;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* En Constructor de la clase Injector obtenermos las instancias
|
|
21
|
-
* de Metadata y Logger e inciamos el escaneo de proveedores.
|
|
22
|
-
* @param {Module} module - El módulo debe tener el decorador `@Module`
|
|
23
|
-
* para poderlo procesar.
|
|
24
|
-
*/
|
|
25
|
-
constructor(module: any) {
|
|
26
|
-
this.metadata = Metadata.getInstance();
|
|
27
|
-
this.logger = Logger();
|
|
28
|
-
this.module = module;
|
|
29
|
-
this.scanProviders();
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Este método privado recorre el array de los servicios del modulo para
|
|
34
|
-
* buscar los proveedores tipo objeto a injectar y los almacena en una
|
|
35
|
-
* lista de proveedores.
|
|
36
|
-
* @private
|
|
37
|
-
*/
|
|
38
|
-
private scanProviders() {
|
|
39
|
-
const { services } = this.metadata.getMetadataModule(this.module);
|
|
40
|
-
services.forEach((service) => {
|
|
41
|
-
if (typeof service === "object")
|
|
42
|
-
this.listProviders.set(service.provider, service.useValue);
|
|
43
|
-
});
|
|
44
|
-
this.logger.debug("Injector - list provider", this.listProviders);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Método para obtener un objeto con los parámetros y valores
|
|
49
|
-
* que se inyectarán en la clase (target).
|
|
50
|
-
*
|
|
51
|
-
* @param {Class} target - La clase objetivo.
|
|
52
|
-
* @returns {object} - Objeto con datos a inyectar.
|
|
53
|
-
*/
|
|
54
|
-
getInjectData(target: Function): object {
|
|
55
|
-
const injectData = {};
|
|
56
|
-
const dInject = this.metadata.getInjectionDependencies(target);
|
|
57
|
-
if (dInject.size > 0) {
|
|
58
|
-
for (const key of dInject.keys()) {
|
|
59
|
-
if (this.listProviders.has(key)) {
|
|
60
|
-
this.logger.info(`"${key}" is on providers list.`);
|
|
61
|
-
const paramName = dInject.get(key);
|
|
62
|
-
const useValue = this.listProviders.get(key);
|
|
63
|
-
injectData[paramName] = useValue;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return injectData;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Método para obtener el injector apropiado para la clase objetivo.
|
|
72
|
-
*
|
|
73
|
-
* @param {any} target - La clase objetivo.
|
|
74
|
-
* @returns - El injector configurado.
|
|
75
|
-
*/
|
|
76
|
-
getInjector(target) {
|
|
77
|
-
const injectData = this.getInjectData(target);
|
|
78
|
-
let injector = asClass(target).scoped();
|
|
79
|
-
|
|
80
|
-
if (!isEmpty(injectData)) {
|
|
81
|
-
injector = injector.inject(() => injectData);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
...injector,
|
|
86
|
-
interface: target,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
package/injector/module.ts
DELETED
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import { aliasTo } from "awilix";
|
|
3
|
-
import { IModuleConfig, ILoggerService } from "@zanobijs/common";
|
|
4
|
-
import { unCapitalize, isEmpty, isClass } from "@zanobijs/common/utils/shared.utils";
|
|
5
|
-
import { Logger } from "@zanobijs/common/utils";
|
|
6
|
-
import { Injector } from "./injector";
|
|
7
|
-
import { Metadata } from "../metadata";
|
|
8
|
-
import { InvalidModuleAnnotationException } from "../exceptions";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Módulo para gestionar la configuración y el registro de controladores, servicios y dependencias.
|
|
12
|
-
*/
|
|
13
|
-
export class Module {
|
|
14
|
-
private config: IModuleConfig;
|
|
15
|
-
private module: any;
|
|
16
|
-
private logger: ILoggerService;
|
|
17
|
-
private injector: Injector;
|
|
18
|
-
private registerClass = {};
|
|
19
|
-
private dependenciesClass: any[] = [];
|
|
20
|
-
private metadata: Metadata;
|
|
21
|
-
private types: string[] = ["controller", "service"];
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Constructor del módulo.
|
|
25
|
-
*/
|
|
26
|
-
constructor() {
|
|
27
|
-
this.logger = Logger();
|
|
28
|
-
this.metadata = Metadata.getInstance();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Configura el módulo con la información proporcionada.
|
|
33
|
-
* @param {any} module - Módulo a configurar.
|
|
34
|
-
*/
|
|
35
|
-
setup(module: any): void {
|
|
36
|
-
if (this.metadata.isTypeModule(module)) {
|
|
37
|
-
this.module = module;
|
|
38
|
-
this.injector = new Injector(module);
|
|
39
|
-
} else {
|
|
40
|
-
throw new InvalidModuleAnnotationException();
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Inicializa el módulo extrayendo metadatos y registrando las entidades.
|
|
46
|
-
*/
|
|
47
|
-
initialize(): void {
|
|
48
|
-
this.getMetadataModule();
|
|
49
|
-
this.registerDependencies();
|
|
50
|
-
this.registerDependenciesToAlias();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Extrae los metadatos del módulo usando reflect-metadata.
|
|
55
|
-
* @private
|
|
56
|
-
*/
|
|
57
|
-
private getMetadataModule(): void {
|
|
58
|
-
this.config = this.metadata.getMetadataModule(this.module);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Registra las entidades de configuración en el módulo.
|
|
63
|
-
* @private
|
|
64
|
-
*/
|
|
65
|
-
private registerDependencies(): void {
|
|
66
|
-
this.registerEntities("controllers");
|
|
67
|
-
this.registerEntities("services");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Registra entidades de configuración (controladores o servicios) del módulo.
|
|
72
|
-
* @param {('controllers' | 'services')} entityType - Tipo de entidad a registrar.
|
|
73
|
-
* @private
|
|
74
|
-
*/
|
|
75
|
-
private registerEntities(entityType: "controllers" | "services"): void {
|
|
76
|
-
const entities = this.config[entityType];
|
|
77
|
-
|
|
78
|
-
if (entities && entities.length > 0) {
|
|
79
|
-
const registeredEntities = entities
|
|
80
|
-
.filter(
|
|
81
|
-
(target) =>
|
|
82
|
-
isClass(target) &&
|
|
83
|
-
this.types.includes(this.metadata.determineType(target)),
|
|
84
|
-
)
|
|
85
|
-
.map((target) => {
|
|
86
|
-
this.groupDependenciesForAlias(target);
|
|
87
|
-
const targetName = unCapitalize(target.name);
|
|
88
|
-
return { [targetName]: this.injector.getInjector(target) };
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
Object.assign(this.registerClass, ...registeredEntities);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Agrupación de dependencias para alias
|
|
97
|
-
*
|
|
98
|
-
* Este método agrupa las dependencias de la clase utilizando el método
|
|
99
|
-
* `getClassDependencies` de la instancia `metadata`. Si la clase
|
|
100
|
-
* tiene dependencias y estas no están vacías, las añade a la propiedad
|
|
101
|
-
* `dependenciesClass` de la instancia actual para luego validar si existe
|
|
102
|
-
* alguna dependencia con un nombre diferente y asiganar un alias.
|
|
103
|
-
*
|
|
104
|
-
* @private
|
|
105
|
-
* @param {Function} target - La clase objetivo de la cual se quieren obtener las dependencias.
|
|
106
|
-
*/
|
|
107
|
-
private groupDependenciesForAlias(target: Function): void {
|
|
108
|
-
const dependencies: any[] = this.metadata.getClassDependencies(target);
|
|
109
|
-
if (dependencies && !isEmpty(dependencies)) {
|
|
110
|
-
this.dependenciesClass = [...this.dependenciesClass, ...dependencies];
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* recorre las dependencias agrupadas y registra con un alias
|
|
116
|
-
* aquellas que tienen nombres diferente a la que esta registrada.
|
|
117
|
-
*
|
|
118
|
-
* @private
|
|
119
|
-
* @example
|
|
120
|
-
* contructor(private serviceA: ServiceA) // parametro con nombre igual
|
|
121
|
-
* contructor(private sA: ServiceA) // parametro con nombre diferente
|
|
122
|
-
*/
|
|
123
|
-
private registerDependenciesToAlias(): void {
|
|
124
|
-
this.dependenciesClass.forEach((dependency) => {
|
|
125
|
-
if (!this.registerClass[dependency.nameParameter]) {
|
|
126
|
-
this.registerClass[dependency.nameParameter] = aliasTo(
|
|
127
|
-
dependency.nameClassContainer,
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Devuelve las importaciones del módulo.
|
|
135
|
-
* @returns {any[] | undefined} - Importaciones del módulo.
|
|
136
|
-
*/
|
|
137
|
-
getImports(): any[] | undefined {
|
|
138
|
-
return this.config.imports;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Devuelve las clases registradas en el módulo.
|
|
143
|
-
* @returns {any} - Clases registradas.
|
|
144
|
-
*/
|
|
145
|
-
getRegisterClass(): any {
|
|
146
|
-
this.logger.debug("Module - register class", this.registerClass);
|
|
147
|
-
return this.registerClass;
|
|
148
|
-
}
|
|
149
|
-
}
|
package/interface/index.d.ts
DELETED
|
File without changes
|
package/interface/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/interface/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
package/metadata.ts
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
import { IModuleConfig } from "@zanobijs/common";
|
|
2
|
-
import {
|
|
3
|
-
DEPENDENCIES_CLASS,
|
|
4
|
-
DEPENDENCIES_INJECT,
|
|
5
|
-
DEPENDENCIES_PARAMETERS,
|
|
6
|
-
IS_CONTROLLER,
|
|
7
|
-
IS_EXPORT,
|
|
8
|
-
IS_IMPORTS,
|
|
9
|
-
IS_MODULE,
|
|
10
|
-
IS_SERVICE,
|
|
11
|
-
MODULE_CONTROLLERS,
|
|
12
|
-
MODULE_EXPORTS,
|
|
13
|
-
MODULE_IMPORTS,
|
|
14
|
-
MODULE_SERVICES,
|
|
15
|
-
} from "@zanobijs/common/utils/constants";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* La clase `Metadata` proporciona métodos para acceder y manipular
|
|
19
|
-
* metadatos relacionados con diversos componentes y módulos.
|
|
20
|
-
*/
|
|
21
|
-
export class Metadata {
|
|
22
|
-
private static instance: Metadata;
|
|
23
|
-
private readonly metadataMap: { [key: string]: string } = {
|
|
24
|
-
[IS_MODULE]: "module",
|
|
25
|
-
[IS_IMPORTS]: "import",
|
|
26
|
-
[IS_CONTROLLER]: "controller",
|
|
27
|
-
[IS_SERVICE]: "service",
|
|
28
|
-
[IS_EXPORT]: "export",
|
|
29
|
-
};
|
|
30
|
-
private constructor() {}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Obtiene la instancia única (singleton) de `Metadata`.
|
|
34
|
-
*
|
|
35
|
-
* @returns La instancia única de `Metadata`.
|
|
36
|
-
*/
|
|
37
|
-
static getInstance(): Metadata {
|
|
38
|
-
if (!this.instance) {
|
|
39
|
-
this.instance = new Metadata();
|
|
40
|
-
}
|
|
41
|
-
return this.instance;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Obtiene los metadatos de un módulo específico.
|
|
46
|
-
*
|
|
47
|
-
* @param module - El módulo del cual obtener los metadatos.
|
|
48
|
-
* @returns Un objeto con los metadatos del módulo {imports, controllers ,services, exports }.
|
|
49
|
-
*/
|
|
50
|
-
getMetadataModule(module: any): IModuleConfig {
|
|
51
|
-
return {
|
|
52
|
-
imports: Reflect.getMetadata(MODULE_IMPORTS, module),
|
|
53
|
-
controllers: Reflect.getMetadata(MODULE_CONTROLLERS, module),
|
|
54
|
-
services: Reflect.getMetadata(MODULE_SERVICES, module),
|
|
55
|
-
exports: Reflect.getMetadata(MODULE_EXPORTS, module),
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Obtiene todas las dependencias asociadas con una clase.
|
|
61
|
-
*
|
|
62
|
-
* @param target - La función/clase objetivo.
|
|
63
|
-
* @returns Un objeto con las dependencias del target.
|
|
64
|
-
*/
|
|
65
|
-
getAllDependencies(target: Function) {
|
|
66
|
-
return {
|
|
67
|
-
dClass: this.getClassDependencies(target),
|
|
68
|
-
dParam: this.getParameterDependencies(target),
|
|
69
|
-
dInject: this.getInjectionDependencies(target),
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Obtiene las dependencias de clase asociadas con una clase.
|
|
75
|
-
*
|
|
76
|
-
* @param target - La función/clase objetivo.
|
|
77
|
-
* @returns Las dependencias de clase de la clase.
|
|
78
|
-
*/
|
|
79
|
-
getClassDependencies(target: Function) {
|
|
80
|
-
return Reflect.getMetadata(DEPENDENCIES_CLASS, target);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Obtiene las dependencias de parámetro asociadas con una clase.
|
|
85
|
-
*
|
|
86
|
-
* @param target - La función/clase objetivo.
|
|
87
|
-
* @returns Las dependencias de parámetro de la clase.
|
|
88
|
-
*/
|
|
89
|
-
getParameterDependencies(target: Function) {
|
|
90
|
-
return Reflect.getMetadata(DEPENDENCIES_PARAMETERS, target);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Obtiene las dependencias a inyectar asociadas con una clase.
|
|
95
|
-
*
|
|
96
|
-
* @param target - La función/clase objetivo.
|
|
97
|
-
* @returns Un Map con las dependencias a inyectar de la clase.
|
|
98
|
-
*/
|
|
99
|
-
getInjectionDependencies(target: Function): Map<string, string> {
|
|
100
|
-
return Reflect.getMetadata(DEPENDENCIES_INJECT, target) || new Map();
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Determina el tipo de una clase basado en sus metadatos.
|
|
105
|
-
*
|
|
106
|
-
* @param target - La función/clase objetivo.
|
|
107
|
-
* @returns Una cadena de texto que indica el tipo del clase segun el mapa de metadata.
|
|
108
|
-
* @throws {Error} Lanza un error si el tipo de la clase es desconocido.
|
|
109
|
-
*/
|
|
110
|
-
determineType(target: Function): string {
|
|
111
|
-
for (const key in this.metadataMap) {
|
|
112
|
-
if (this.hasMetadata(key, target)) {
|
|
113
|
-
return this.metadataMap[key];
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
throw new Error(`${target.name} type is unknown`);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Verifica si una clase tiene un metadato específico.
|
|
121
|
-
*
|
|
122
|
-
* @param metadataKey - La llave del metadato a verificar.
|
|
123
|
-
* @param target - La función/clase objetivo.
|
|
124
|
-
* @returns Verdadero si el target tiene el metadato, falso en caso contrario.
|
|
125
|
-
*/
|
|
126
|
-
private hasMetadata(metadataKey: string, target: Function): boolean {
|
|
127
|
-
return !!Reflect.getMetadata(metadataKey, target);
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Verifica si una clase es de tipo "module".
|
|
131
|
-
*
|
|
132
|
-
* @param target - La función/clase objetivo.
|
|
133
|
-
* @returns Verdadero si el target es de tipo "module", falso en caso contrario.
|
|
134
|
-
*/
|
|
135
|
-
isTypeModule(target: Function): boolean {
|
|
136
|
-
return this.hasMetadata(IS_MODULE, target);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* Verifica si una clase es de tipo "import".
|
|
141
|
-
*
|
|
142
|
-
* @param target - La función/clase objetivo.
|
|
143
|
-
* @returns Verdadero si el target es de tipo "import", falso en caso contrario.
|
|
144
|
-
*/
|
|
145
|
-
isTypeImport(target: Function): boolean {
|
|
146
|
-
return this.hasMetadata(IS_IMPORTS, target);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Verifica si una clase es de tipo "controller".
|
|
151
|
-
*
|
|
152
|
-
* @param target - La función/clase objetivo.
|
|
153
|
-
* @returns Verdadero si el target es de tipo "controller", falso en caso contrario.
|
|
154
|
-
*/
|
|
155
|
-
isTypeController(target: Function): boolean {
|
|
156
|
-
return this.hasMetadata(IS_CONTROLLER, target);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Verifica si una clase es de tipo "service".
|
|
161
|
-
*
|
|
162
|
-
* @param target - La función/clase objetivo.
|
|
163
|
-
* @returns Verdadero si el target es de tipo "service", falso en caso contrario.
|
|
164
|
-
*/
|
|
165
|
-
isTypeService(target: Function): boolean {
|
|
166
|
-
return this.hasMetadata(IS_SERVICE, target);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
/**
|
|
170
|
-
* Verifica si una clase es de tipo "export".
|
|
171
|
-
*
|
|
172
|
-
* @param target - La función/clase objetivo.
|
|
173
|
-
* @returns Verdadero si el target es de tipo "export", falso en caso contrario.
|
|
174
|
-
*/
|
|
175
|
-
isTypeExports(target: Function): boolean {
|
|
176
|
-
return this.hasMetadata(IS_EXPORT, target);
|
|
177
|
-
}
|
|
178
|
-
}
|