diff --git a/packages/core/injector/instance-loader.ts b/packages/core/injector/instance-loader.ts index 14635af14bc..da1314ee5df 100644 --- a/packages/core/injector/instance-loader.ts +++ b/packages/core/injector/instance-loader.ts @@ -1,12 +1,10 @@ -import iterate from 'iterare'; +import { Logger } from '@nestjs/common'; +import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; +import { Injectable } from '@nestjs/common/interfaces/injectable.interface'; +import { moduleInitMessage } from '../helpers/messages'; import { NestContainer } from './container'; import { Injector } from './injector'; -import { Injectable } from '@nestjs/common/interfaces/injectable.interface'; -import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface'; import { Module } from './module'; -import { Logger, OnModuleInit } from '@nestjs/common'; -import { moduleInitMessage } from '../helpers/messages'; -import { isUndefined, isNil } from '@nestjs/common/utils/shared.utils'; export class InstanceLoader { private readonly injector = new Injector(); diff --git a/packages/core/scanner.ts b/packages/core/scanner.ts index 06a8ce4959c..1f9968f7640 100644 --- a/packages/core/scanner.ts +++ b/packages/core/scanner.ts @@ -24,8 +24,9 @@ import { CircularDependencyException } from './errors/exceptions/circular-depend import { NestContainer } from './injector/container'; interface ApplicationProviderWrapper { - moduleToken: string; - providerToken: string; + moduleKey: string; + providerKey: string; + type: string; } export class DependenciesScanner { @@ -237,15 +238,26 @@ export class DependenciesScanner { } const applyProvidersMap = this.getApplyProvidersMap(); const providersKeys = Object.keys(applyProvidersMap); - const providerToken = component.provide; - if (providersKeys.indexOf(providerToken) < 0) { + const type = component.provide; + if (providersKeys.indexOf(type) < 0) { return this.container.addComponent(component, token); } + const providerToken = Math.random() + .toString(36) + .substring(2, 32); + this.applicationProvidersApplyMap.push({ - moduleToken: token, - providerToken, + type, + moduleKey: token, + providerKey: providerToken, }); - this.container.addComponent(component, token); + this.container.addComponent( + { + ...component, + provide: providerToken, + }, + token, + ); } public storeInjectable(component: Type, token: string) { @@ -270,12 +282,12 @@ export class DependenciesScanner { public applyApplicationProviders() { const applyProvidersMap = this.getApplyProvidersMap(); this.applicationProvidersApplyMap.forEach( - ({ moduleToken, providerToken }) => { + ({ moduleKey, providerKey, type }) => { const modules = this.container.getModules(); - const { components } = modules.get(moduleToken); - const { instance } = components.get(providerToken); + const { components } = modules.get(moduleKey); + const { instance } = components.get(providerKey); - applyProvidersMap[providerToken](instance); + applyProvidersMap[type](instance); }, ); } diff --git a/packages/core/test/scanner.spec.ts b/packages/core/test/scanner.spec.ts index 3537f7d1250..9c609fbb520 100644 --- a/packages/core/test/scanner.spec.ts +++ b/packages/core/test/scanner.spec.ts @@ -52,7 +52,7 @@ describe('DependenciesScanner', () => { mockContainer.restore(); }); - it('should "storeModule" call twice (2 modules) container method "addModule"', async () => { + it('should "storeModule" call twice (2 modules) container method "addModule"', async () => { const expectation = mockContainer.expects('addModule').twice(); await scanner.scan(TestModule as any); expectation.verify(); @@ -155,7 +155,7 @@ describe('DependenciesScanner', () => { }); describe('storeRelatedModule', () => { - it('should call forwardRef() when forwardRef property exists', async () => { + it('should call forwardRef() when forwardRef property exists', async () => { const module = { forwardRef: sinon.stub().returns({}) }; sinon.stub(container, 'addRelatedModule').returns({}); @@ -195,9 +195,7 @@ describe('DependenciesScanner', () => { }; it('should call container "addComponent" with expected args', () => { - const expectation = mockContainer - .expects('addComponent') - .withArgs(component, token); + const expectation = mockContainer.expects('addComponent').atLeast(1); mockContainer.expects('addComponent').callsFake(() => false); scanner.storeComponent(component, token); @@ -210,7 +208,7 @@ describe('DependenciesScanner', () => { const applyMap = (scanner as any).applicationProvidersApplyMap; expect(applyMap).to.have.length(1); - expect(applyMap[0].moduleToken).to.be.eql(token); + expect(applyMap[0].moduleKey).to.be.eql(token); }); }); describe('and is not global', () => { @@ -245,8 +243,9 @@ describe('DependenciesScanner', () => { describe('applyApplicationProviders', () => { it('should apply each provider', () => { const provider = { - moduleToken: 'moduleToken', - providerToken: 'providerToken', + moduleKey: 'moduleToken', + providerKey: 'providerToken', + type: APP_GUARD, }; (scanner as any).applicationProvidersApplyMap = [provider]; @@ -258,7 +257,7 @@ describe('DependenciesScanner', () => { })); const applySpy = sinon.spy(); sinon.stub(scanner, 'getApplyProvidersMap').callsFake(() => ({ - [provider.providerToken]: applySpy, + [provider.type]: applySpy, })); scanner.applyApplicationProviders(); expect(applySpy.called).to.be.true; diff --git a/packages/websockets/adapters/ws-adapter.ts b/packages/websockets/adapters/ws-adapter.ts index 5f32939aa90..a67e7f5a5a2 100644 --- a/packages/websockets/adapters/ws-adapter.ts +++ b/packages/websockets/adapters/ws-adapter.ts @@ -47,7 +47,7 @@ export class WsAdapter implements WebSocketAdapter { } public bindMessageHandlers( - client: WebSocket, + client: any, handlers: MessageMappingProperties[], transform: (data: any) => Observable, ) {