@zanobijs/core 1.0.0-beta.1 → 1.0.0-beta.3
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 +173 -5
- package/__test__/factory.spec.js +33 -3
- package/__test__/mocks/classModule.mock.js +6 -6
- package/__test__/mocks/classWithDependeciesClass.mock.js +5 -5
- package/__test__/mocks/classWithDependeciesInject.mock.js +11 -11
- package/exceptions/constant.message.d.ts +1 -0
- package/exceptions/constant.message.js +3 -1
- package/exceptions/invalid.module.exception.d.ts +3 -3
- package/exceptions/invalid.module.exception.js +3 -3
- package/exceptions/resolution.exception.d.ts +13 -0
- package/exceptions/resolution.exception.js +20 -0
- package/factory.js +9 -1
- package/injector/injector.js +2 -2
- package/injector/module.js +2 -2
- package/metadata.d.ts +1 -1
- package/metadata.js +1 -1
- package/package.json +3 -3
- package/tsconfig.build.json +2 -2
- package/__test__/factory.spec.ts +0 -37
- package/__test__/injector/injector.spec.ts +0 -32
- package/__test__/injector/module.spec.ts +0 -66
- package/__test__/metadata.spec.ts +0 -71
- package/__test__/mocks/classModule.mock.ts +0 -55
- package/__test__/mocks/classWithDependeciesClass.mock.ts +0 -21
- package/__test__/mocks/classWithDependeciesInject.mock.ts +0 -47
- 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.ts +0 -1
- package/metadata.ts +0 -178
- package/tsconfig.build.tsbuildinfo +0 -1
- package/tsconfig.json +0 -11
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import "reflect-metadata";
|
|
2
|
-
import { expect } from "chai";
|
|
3
|
-
import { Module } from "../../injector";
|
|
4
|
-
import { ModuleTestWithImports, ModuleTestWithInjector } from "../mocks/classModule.mock";
|
|
5
|
-
|
|
6
|
-
describe("Core - Injector - module", () => {
|
|
7
|
-
let moduleInstance: Module;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
moduleInstance = new Module();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
describe("setup", () => {
|
|
14
|
-
it("should respond that setup no is a module", () => {
|
|
15
|
-
try {
|
|
16
|
-
const mockModule = {};
|
|
17
|
-
moduleInstance.setup(mockModule);
|
|
18
|
-
} catch (error) {
|
|
19
|
-
expect(error.message).to.equal(
|
|
20
|
-
"The class must have an annotation @Module()",
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
it("should respond no problem with the setup.", () => {
|
|
25
|
-
moduleInstance.setup(ModuleTestWithInjector);
|
|
26
|
-
expect(moduleInstance.getRegisterClass()).to.is.empty;
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("Module initialize", () => {
|
|
31
|
-
it("should respond that call getMetadataModule and register on Module", () => {
|
|
32
|
-
let metadataCalled = false;
|
|
33
|
-
let registerDependencies = false;
|
|
34
|
-
let registerDependenciesToAlias = false;
|
|
35
|
-
|
|
36
|
-
moduleInstance["getMetadataModule"] = () => {
|
|
37
|
-
metadataCalled = true;
|
|
38
|
-
};
|
|
39
|
-
moduleInstance["registerDependencies"] = () => {
|
|
40
|
-
registerDependencies = true;
|
|
41
|
-
};
|
|
42
|
-
moduleInstance["registerDependenciesToAlias"] = () => {
|
|
43
|
-
registerDependenciesToAlias = true;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
moduleInstance.initialize();
|
|
47
|
-
|
|
48
|
-
expect(metadataCalled).to.be.true;
|
|
49
|
-
expect(registerDependencies).to.be.true;
|
|
50
|
-
expect(registerDependenciesToAlias).to.be.true;
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
describe("Module get information", () => {
|
|
54
|
-
it("Should respond apiKey and sContro2 in registered providers", () => {
|
|
55
|
-
moduleInstance.setup(ModuleTestWithInjector);
|
|
56
|
-
moduleInstance.initialize();
|
|
57
|
-
expect(moduleInstance.getRegisterClass()).to.have.property("sContro2");
|
|
58
|
-
expect(moduleInstance.getRegisterClass()).to.have.property("apiKey");
|
|
59
|
-
});
|
|
60
|
-
it("Should respond imports of module", () => {
|
|
61
|
-
moduleInstance.setup(ModuleTestWithImports);
|
|
62
|
-
moduleInstance.initialize();
|
|
63
|
-
expect(moduleInstance.getImports()).to.not.empty
|
|
64
|
-
});
|
|
65
|
-
});
|
|
66
|
-
});
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import { expect } from "chai";
|
|
2
|
-
import { Metadata } from "../metadata";
|
|
3
|
-
import { ModuleTestEmpty, ModuleTest } from "./mocks/classModule.mock";
|
|
4
|
-
import { ServiceWithDepenParam } from "./mocks/classWithDependeciesInject.mock";
|
|
5
|
-
import { ControllerWithDepenClass } from "./mocks/classWithDependeciesClass.mock";
|
|
6
|
-
|
|
7
|
-
describe("Core - metadata", () => {
|
|
8
|
-
class genericClassForTesting {}
|
|
9
|
-
const metadata = Metadata.getInstance();
|
|
10
|
-
|
|
11
|
-
describe("Is Type", () => {
|
|
12
|
-
it("should respond false to is module", () => {
|
|
13
|
-
expect(metadata.isTypeModule(genericClassForTesting)).to.be.false;
|
|
14
|
-
});
|
|
15
|
-
it("should respond false to is imports", () => {
|
|
16
|
-
expect(metadata.isTypeImport(genericClassForTesting)).to.be.false;
|
|
17
|
-
});
|
|
18
|
-
it("should respond false to is controllers", () => {
|
|
19
|
-
expect(metadata.isTypeController(genericClassForTesting)).to.be.false;
|
|
20
|
-
});
|
|
21
|
-
it("should respond false to is services", () => {
|
|
22
|
-
expect(metadata.isTypeService(genericClassForTesting)).to.be.false;
|
|
23
|
-
});
|
|
24
|
-
it("should respond false to is exports", () => {
|
|
25
|
-
expect(metadata.isTypeExports(genericClassForTesting)).to.be.false;
|
|
26
|
-
});
|
|
27
|
-
it("should respond true to is module", () => {
|
|
28
|
-
expect(metadata.isTypeModule(ModuleTestEmpty)).to.be.true;
|
|
29
|
-
});
|
|
30
|
-
it("should respond determine type of service", () => {
|
|
31
|
-
expect(metadata.determineType(ServiceWithDepenParam)).to.be.equal(
|
|
32
|
-
"service",
|
|
33
|
-
);
|
|
34
|
-
});
|
|
35
|
-
it("should respond determine type of unknown", () => {
|
|
36
|
-
class ServiceTest {}
|
|
37
|
-
try {
|
|
38
|
-
metadata.determineType(ServiceTest);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
expect(error.message).to.be.equal("ServiceTest type is unknown");
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
describe("Get Metadata", () => {
|
|
45
|
-
it("Should respond the metadata of the decorator module", () => {
|
|
46
|
-
const metadata2 = Metadata.getInstance();
|
|
47
|
-
const resultMetadata = metadata2.getMetadataModule(ModuleTest);
|
|
48
|
-
expect(resultMetadata).to.have.property("imports");
|
|
49
|
-
expect(resultMetadata).to.have.property("controllers");
|
|
50
|
-
expect(resultMetadata).to.have.property("services");
|
|
51
|
-
expect(resultMetadata).to.have.property("exports");
|
|
52
|
-
});
|
|
53
|
-
it("should respond the dependency metadata of a controller", () => {
|
|
54
|
-
const resultMetadata = metadata.getAllDependencies(
|
|
55
|
-
ControllerWithDepenClass,
|
|
56
|
-
);
|
|
57
|
-
expect(resultMetadata).to.have.property("dClass");
|
|
58
|
-
expect(resultMetadata).to.have.property("dParam");
|
|
59
|
-
expect(resultMetadata).to.have.property("dInject");
|
|
60
|
-
expect(resultMetadata.dInject).to.be.empty;
|
|
61
|
-
});
|
|
62
|
-
it("should respond the dependency metadata of a service", () => {
|
|
63
|
-
const resultMetadata = metadata.getAllDependencies(ServiceWithDepenParam);
|
|
64
|
-
expect(resultMetadata).to.have.property("dClass");
|
|
65
|
-
expect(resultMetadata).to.have.property("dParam");
|
|
66
|
-
expect(resultMetadata).to.have.property("dInject");
|
|
67
|
-
expect(resultMetadata.dInject).to.not.be.empty;
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
describe("", () => {});
|
|
71
|
-
});
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Module } from "zanobijs-common";
|
|
2
|
-
import { ControllerWithDepenClass, ServiceToController } from "./classWithDependeciesClass.mock";
|
|
3
|
-
import { ControllerWithDepen2, ServiceToController2 } from "./classWithDependeciesInject.mock";
|
|
4
|
-
|
|
5
|
-
@Module({
|
|
6
|
-
imports: [],
|
|
7
|
-
controllers: [],
|
|
8
|
-
services: [],
|
|
9
|
-
exports: [],
|
|
10
|
-
})
|
|
11
|
-
export class ModuleTestEmpty {}
|
|
12
|
-
|
|
13
|
-
@Module({
|
|
14
|
-
imports: [],
|
|
15
|
-
controllers: [ControllerWithDepenClass],
|
|
16
|
-
services: [ServiceToController],
|
|
17
|
-
exports: [],
|
|
18
|
-
})
|
|
19
|
-
export class ModuleTest {}
|
|
20
|
-
|
|
21
|
-
@Module({
|
|
22
|
-
imports: [],
|
|
23
|
-
controllers: [ControllerWithDepen2],
|
|
24
|
-
services: [
|
|
25
|
-
ServiceToController2,
|
|
26
|
-
{
|
|
27
|
-
provider: "API_KEY",
|
|
28
|
-
useValue: "isApiKey_qwerty12345"
|
|
29
|
-
},
|
|
30
|
-
],
|
|
31
|
-
exports: [],
|
|
32
|
-
})
|
|
33
|
-
export class ModuleTestWithInjector {}
|
|
34
|
-
|
|
35
|
-
@Module({
|
|
36
|
-
imports: [ModuleTestWithInjector],
|
|
37
|
-
controllers: [],
|
|
38
|
-
services: [],
|
|
39
|
-
exports: [],
|
|
40
|
-
})
|
|
41
|
-
export class ModuleTestWithImports {}
|
|
42
|
-
|
|
43
|
-
@Module({
|
|
44
|
-
imports: [ModuleTestWithInjector],
|
|
45
|
-
controllers: [ControllerWithDepen2],
|
|
46
|
-
services: [
|
|
47
|
-
ServiceToController2,
|
|
48
|
-
{
|
|
49
|
-
provider: "API_SECRET",
|
|
50
|
-
useValue: "isApiSecret_cvbdfgert"
|
|
51
|
-
},
|
|
52
|
-
],
|
|
53
|
-
exports: [],
|
|
54
|
-
})
|
|
55
|
-
export class ModuleTestAll {}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Controller, Injectable } from "zanobijs-common";
|
|
2
|
-
|
|
3
|
-
@Injectable()
|
|
4
|
-
export class ServiceToService {
|
|
5
|
-
constructor() {}
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
@Injectable()
|
|
9
|
-
export class Service {
|
|
10
|
-
constructor() {}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
@Injectable()
|
|
14
|
-
export class ServiceToController {
|
|
15
|
-
constructor() {}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
@Controller()
|
|
19
|
-
export class ControllerWithDepenClass {
|
|
20
|
-
constructor(private servicio: ServiceToController) {}
|
|
21
|
-
}
|
|
@@ -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
|
-
}
|
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, asClass } 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.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|