diff --git a/example/modules/users/users.module.ts b/example/modules/users/users.module.ts index 9f0eb92f4d4..fa130afdc59 100644 --- a/example/modules/users/users.module.ts +++ b/example/modules/users/users.module.ts @@ -7,7 +7,10 @@ import { ChatGateway } from './chat.gateway'; @Module({ controllers: [ UsersController ], - components: [ UsersService, ChatGateway ], + components: [ + { provide: 'UsersService', useClass: UsersService }, + ChatGateway, + ], }) export class UsersModule implements NestModule { public configure(consumer: MiddlewaresConsumer) { diff --git a/package.json b/package.json index ae414d3d586..aa4d276394c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nest.js", - "version": "1.0.2", + "version": "1.0.3", "description": "Modern, fast, powerful node.js web framework", "main": "index.js", "scripts": { diff --git a/src/core/injector/module.ts b/src/core/injector/module.ts index 6401bc0d5ee..2d2a8474235 100644 --- a/src/core/injector/module.ts +++ b/src/core/injector/module.ts @@ -9,6 +9,7 @@ import { RuntimeException } from '../../errors/exceptions/runtime.exception'; export interface CustomComponent { provide: any; + name: string; } export type OpaqueToken = string | symbol | object | Metatype; export type CustomClass = CustomComponent & { useClass: Metatype }; @@ -91,10 +92,17 @@ export class Module { return !isNil((component as CustomComponent).provide); } - public addCustomComponent(component: ComponentMetatype) { - if (this.isCustomClass(component)) this.addCustomClass(component); - else if (this.isCustomValue(component)) this.addCustomValue(component); - else if (this.isCustomFactory(component)) this.addCustomFactory(component); + public addCustomComponent(component: CustomFactory | CustomValue | CustomClass) { + const { provide } = component; + const name = isFunction(provide) ? provide.name : provide; + const comp = { + ...component, + name, + }; + + if (this.isCustomClass(comp)) this.addCustomClass(comp); + else if (this.isCustomValue(comp)) this.addCustomValue(comp); + else if (this.isCustomFactory(comp)) this.addCustomFactory(comp); } public isCustomClass(component): component is CustomClass { @@ -110,9 +118,9 @@ export class Module { } public addCustomClass(component: CustomClass) { - const { provide: metatype, useClass } = component; - this._components.set(metatype.name, { - name: metatype.name, + const { provide, name, useClass } = component; + this._components.set(name, { + name, metatype: useClass, instance: null, isResolved: false, @@ -120,9 +128,7 @@ export class Module { } public addCustomValue(component: CustomValue) { - const { provide, useValue: value } = component; - const name = isFunction(provide) ? provide.name : provide; - + const { provide, name, useValue: value } = component; this._components.set(name, { name, metatype: null, @@ -133,7 +139,7 @@ export class Module { } public addCustomFactory(component: CustomFactory){ - const { provide: name, useFactory: factory, inject } = component; + const { provide, name, useFactory: factory, inject } = component; this._components.set(name, { name, metatype: factory as any, diff --git a/src/core/test/injector/module.spec.ts b/src/core/test/injector/module.spec.ts index e5dd715310d..2f0b2f24c75 100644 --- a/src/core/test/injector/module.spec.ts +++ b/src/core/test/injector/module.spec.ts @@ -93,7 +93,7 @@ describe('Module', () => { describe('addCustomClass', () => { const type = { name: 'TypeTest' }; - const component = { provide: type, useClass: type }; + const component = { provide: type, useClass: type, name: 'test' }; let setSpy; beforeEach(() => { const collection = new Map(); @@ -102,8 +102,8 @@ describe('Module', () => { }); it('should store component', () => { module.addCustomClass(component as any); - expect(setSpy.calledWith(type.name, { - name: type.name, + expect(setSpy.calledWith(component.name, { + name: component.name, metatype: type, instance: null, isResolved: false, @@ -113,47 +113,32 @@ describe('Module', () => { describe('addCustomValue', () => { let setSpy; + const value = () => ({}); + const name = 'test'; + const component = { provide: value, name, useValue: value }; + beforeEach(() => { const collection = new Map(); setSpy = sinon.spy(collection, 'set'); (module as any)._components = collection; }); - describe('when value is a function', () => { - const value = () => ({}); - const component = { provide: value, useValue: value }; - - it('should store component', () => { - module.addCustomValue(component as any); - expect(setSpy.calledWith(value.name, { - name: value.name, - metatype: null, - instance: value, - isResolved: true, - isNotMetatype: true, - })).to.be.true; - }); - }); - describe('when value is not a function', () => { - const value = 'Test'; - const component = { provide: value, useValue: value }; - - it('should store component', () => { - module.addCustomValue(component as any); - expect(setSpy.calledWith(value, { - name: value, - metatype: null, - instance: value, - isResolved: true, - isNotMetatype: true, - })).to.be.true; - }); + + it('should store component', () => { + module.addCustomValue(component as any); + expect(setSpy.calledWith(name, { + name, + metatype: null, + instance: value, + isResolved: true, + isNotMetatype: true, + })).to.be.true; }); }); describe('addCustomFactory', () => { const type = { name: 'TypeTest' }; const inject = [1, 2, 3]; - const component = { provide: type, useFactory: type, inject }; + const component = { provide: type, useFactory: type, name: 'test', inject }; let setSpy; beforeEach(() => { @@ -163,8 +148,8 @@ describe('Module', () => { }); it('should store component', () => { module.addCustomFactory(component as any); - expect(setSpy.getCall(0).args).to.deep.equal([type, { - name: type, + expect(setSpy.getCall(0).args).to.deep.equal([component.name, { + name: component.name, metatype: type, instance: null, isResolved: false,