Skip to content

Commit 9a1a69b

Browse files
Merge branch 'anizozina-fix/request-scoped-in-lazy-load'
2 parents 1e412cb + 64ec1b5 commit 9a1a69b

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { INestApplication } from '@nestjs/common';
2+
import { Test } from '@nestjs/testing';
3+
import { expect } from 'chai';
4+
import * as request from 'supertest';
5+
import { LazyController } from '../src/lazy.controller';
6+
7+
describe('Lazy Requested Scoped providers', () => {
8+
let app: INestApplication;
9+
10+
beforeEach(async () => {
11+
const module = await Test.createTestingModule({
12+
controllers: [LazyController],
13+
}).compile();
14+
15+
app = module.createNestApplication();
16+
await app.init();
17+
});
18+
19+
it('should not recreate dependencies for default scope', async () => {
20+
const resultOne = await request(app.getHttpServer()).get('/lazy/request');
21+
22+
expect(resultOne.text).to.be.equal('Hi! Counter is 1');
23+
expect(resultOne.statusCode).to.be.equal(200);
24+
25+
const resultTwo = await request(app.getHttpServer()).get('/lazy/request');
26+
27+
expect(resultTwo.text).to.be.equal('Hi! Counter is 2');
28+
expect(resultTwo.statusCode).to.be.equal(200);
29+
});
30+
});

integration/lazy-modules/src/lazy.controller.ts

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ export class LazyController {
1313
const { TransientService } = await import('./transient.service');
1414
const _service = await moduleRef.resolve(TransientService);
1515

16+
return _service.eager();
17+
}
18+
@Get('request')
19+
async execRequestScope() {
20+
const { RequestLazyModule } = await import('./request.module');
21+
const moduleRef = await this.lazyLoadModule.load(() => RequestLazyModule);
22+
23+
const { RequestService } = await import('./request.service');
24+
const _service = await moduleRef.resolve(RequestService);
25+
1626
return _service.eager();
1727
}
1828
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Module } from '@nestjs/common';
2+
import { EagerService } from './eager.module';
3+
import { GlobalService } from './global.module';
4+
import { RequestService } from './request.service';
5+
6+
@Module({
7+
imports: [],
8+
providers: [RequestService, GlobalService, EagerService],
9+
exports: [RequestService],
10+
})
11+
export class RequestLazyModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Injectable, Scope } from '@nestjs/common';
2+
import { EagerService } from './eager.module';
3+
4+
@Injectable({ scope: Scope.REQUEST })
5+
export class RequestService {
6+
constructor(private eagerService: EagerService) {}
7+
8+
eager() {
9+
return this.eagerService.sayHello();
10+
}
11+
}

packages/core/injector/module.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,11 @@ export class Module {
255255
}
256256

257257
const isAlreadyDeclared = this._providers.has(provider);
258-
if (this.isTransientProvider(provider) && isAlreadyDeclared) {
258+
if (
259+
(this.isTransientProvider(provider) ||
260+
this.isRequestScopeProvider(provider)) &&
261+
isAlreadyDeclared
262+
) {
259263
return provider;
260264
}
261265

@@ -664,4 +668,8 @@ export class Module {
664668
private isTransientProvider(provider: Type<any>): boolean {
665669
return getClassScope(provider) === Scope.TRANSIENT;
666670
}
671+
672+
private isRequestScopeProvider(provider: Type<any>): boolean {
673+
return getClassScope(provider) === Scope.REQUEST;
674+
}
667675
}

0 commit comments

Comments
 (0)