Skip to content

Commit 42adbab

Browse files
Merge branch '5.4.1' of https://github.com/nestjs/nest into 5.4.1
2 parents df958c9 + bc9aa8c commit 42adbab

7 files changed

Lines changed: 49 additions & 46 deletions

File tree

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,22 @@
11
import { Observable, of } from 'rxjs';
22
import { tap } from 'rxjs/operators';
33
import { Inject, Injectable, Optional } from '../../decorators';
4-
import {
5-
ExecutionContext,
6-
HttpServer,
7-
NestInterceptor,
8-
} from '../../interfaces';
4+
import { ExecutionContext, NestInterceptor } from '../../interfaces';
95
import { CACHE_KEY_METADATA, CACHE_MANAGER } from '../cache.constants';
106

11-
// NOTE (external)
12-
// We need to deduplicate them here due to the circular dependency
13-
// between core and common packages
14-
const HTTP_SERVER_REF = 'HTTP_SERVER_REF';
7+
const APPLICATION_REFERENCE_HOST = 'ApplicationReferenceHost';
158
const REFLECTOR = 'Reflector';
169

1710
@Injectable()
1811
export class CacheInterceptor implements NestInterceptor {
19-
protected readonly isHttpApp: boolean;
12+
@Optional()
13+
@Inject(APPLICATION_REFERENCE_HOST)
14+
protected readonly applicationRefHost: any;
2015

2116
constructor(
22-
@Optional()
23-
@Inject(HTTP_SERVER_REF)
24-
protected readonly httpServer: HttpServer,
2517
@Inject(CACHE_MANAGER) protected readonly cacheManager: any,
2618
@Inject(REFLECTOR) protected readonly reflector,
27-
) {
28-
this.isHttpApp = httpServer && !!httpServer.getRequestMethod;
29-
}
19+
) {}
3020

3121
async intercept(
3222
context: ExecutionContext,
@@ -48,13 +38,16 @@ export class CacheInterceptor implements NestInterceptor {
4838
}
4939

5040
trackBy(context: ExecutionContext): string | undefined {
51-
if (!this.isHttpApp) {
41+
const httpServer = this.applicationRefHost.applicationRef;
42+
const isHttpApp = httpServer && !!httpServer.getRequestMethod;
43+
44+
if (!isHttpApp) {
5245
return this.reflector.get(CACHE_KEY_METADATA, context.getHandler());
5346
}
5447
const request = context.getArgByIndex(0);
55-
if (this.httpServer.getRequestMethod(request) !== 'GET') {
48+
if (httpServer.getRequestMethod(request) !== 'GET') {
5649
return undefined;
5750
}
58-
return this.httpServer.getRequestUrl(request);
51+
return httpServer.getRequestUrl(request);
5952
}
6053
}

packages/common/utils/shared.utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ export const validatePath = (path): string =>
88
path.charAt(0) !== '/' ? '/' + path : path;
99
export const isNil = (obj): boolean => isUndefined(obj) || obj === null;
1010
export const isEmpty = (array): boolean => !(array && array.length > 0);
11-
export const isSymbol = (fn): boolean => typeof fn === 'symbol';
11+
export const isSymbol = (fn): fn is symbol => typeof fn === 'symbol';

packages/core/exceptions/base-exception-filter.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,34 @@ import {
44
HttpException,
55
HttpServer,
66
HttpStatus,
7+
Inject,
78
Logger,
9+
Optional,
810
} from '@nestjs/common';
911
import { isObject } from '@nestjs/common/utils/shared.utils';
1012
import { MESSAGES } from '../constants';
13+
import { ApplicationReferenceHost } from './../helpers/application-ref-host';
1114

1215
export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
1316
private static readonly logger = new Logger('ExceptionsHandler');
1417

15-
constructor(protected readonly applicationRef: HttpServer) {}
18+
@Optional()
19+
@Inject()
20+
protected readonly applicationRefHost?: ApplicationReferenceHost;
21+
22+
constructor(protected readonly applicationRef?: HttpServer) {}
1623

1724
catch(exception: T, host: ArgumentsHost) {
25+
const applicationRef =
26+
this.applicationRef ||
27+
(this.applicationRefHost && this.applicationRefHost.applicationRef);
28+
1829
if (!(exception instanceof HttpException)) {
1930
const body = {
2031
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
2132
message: MESSAGES.UNKNOWN_EXCEPTION_MESSAGE,
2233
};
23-
this.applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
34+
applicationRef.reply(host.getArgByIndex(1), body, body.statusCode);
2435
if (this.isExceptionObject(exception)) {
2536
return BaseExceptionFilter.logger.error(
2637
exception.message,
@@ -37,11 +48,7 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
3748
message: res,
3849
};
3950

40-
this.applicationRef.reply(
41-
host.getArgByIndex(1),
42-
message,
43-
exception.getStatus(),
44-
);
51+
applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
4552
}
4653

4754
public isExceptionObject(err): err is Error {

packages/core/exceptions/exceptions-handler.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpException, HttpServer } from '@nestjs/common';
1+
import { HttpException } from '@nestjs/common';
22
import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions/exception-filter-metadata.interface';
33
import { ArgumentsHost } from '@nestjs/common/interfaces/features/arguments-host.interface';
44
import { isEmpty } from '@nestjs/common/utils/shared.utils';
@@ -8,10 +8,6 @@ import { BaseExceptionFilter } from './base-exception-filter';
88
export class ExceptionsHandler extends BaseExceptionFilter {
99
private filters: ExceptionFilterMetadata[] = [];
1010

11-
constructor(applicationRef: HttpServer) {
12-
super(applicationRef);
13-
}
14-
1511
public next(exception: Error | HttpException | any, ctx: ArgumentsHost) {
1612
if (this.invokeCustomFilters(exception, ctx)) {
1713
return;

packages/core/injector/injector.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,11 @@ export class Injector {
227227
module: Module,
228228
) {
229229
if (isUndefined(param)) {
230-
throw new UndefinedDependencyException(wrapper.name, dependencyContext, module);
230+
throw new UndefinedDependencyException(
231+
wrapper.name,
232+
dependencyContext,
233+
module,
234+
);
231235
}
232236
const token = this.resolveParamToken(wrapper, param);
233237
return this.resolveComponentInstance<T>(
@@ -293,7 +297,11 @@ export class Injector {
293297
dependencyContext.name,
294298
);
295299
if (isNil(instanceWrapper)) {
296-
throw new UnknownDependenciesException(wrapper.name, dependencyContext, module);
300+
throw new UnknownDependenciesException(
301+
wrapper.name,
302+
dependencyContext,
303+
module,
304+
);
297305
}
298306
return instanceWrapper;
299307
}
@@ -401,10 +409,9 @@ export class Injector {
401409
): Promise<T> {
402410
const { metatype, inject } = wrapper;
403411
if (isNil(inject)) {
404-
targetMetatype.instance = Object.assign(
405-
targetMetatype.instance,
406-
new metatype(...instances),
407-
);
412+
targetMetatype.instance = wrapper.forwardRef
413+
? Object.assign(targetMetatype.instance, new metatype(...instances))
414+
: new metatype(...instances);
408415
} else {
409416
const factoryResult = ((targetMetatype.metatype as any) as Function)(
410417
...instances,

packages/core/injector/module.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Module {
4747
private readonly _components = new Map<any, InstanceWrapper<Injectable>>();
4848
private readonly _injectables = new Map<any, InstanceWrapper<Injectable>>();
4949
private readonly _routes = new Map<string, InstanceWrapper<Controller>>();
50-
private readonly _exports = new Set<string>();
50+
private readonly _exports = new Set<string | symbol>();
5151

5252
constructor(
5353
private readonly _metatype: Type<any>,
@@ -82,7 +82,7 @@ export class Module {
8282
return this._routes;
8383
}
8484

85-
get exports(): Set<string> {
85+
get exports(): Set<string | symbol> {
8686
return this._exports;
8787
}
8888

@@ -279,14 +279,14 @@ export class Module {
279279
}
280280

281281
public addExportedComponent(
282-
exportedComponent: ComponentMetatype | string | DynamicModule,
282+
exportedComponent: ComponentMetatype | string | symbol | DynamicModule,
283283
) {
284-
const addExportedUnit = (token: string) =>
284+
const addExportedUnit = (token: string | symbol) =>
285285
this._exports.add(this.validateExportedProvider(token));
286286

287287
if (this.isCustomProvider(exportedComponent as any)) {
288288
return this.addCustomExportedComponent(exportedComponent as any);
289-
} else if (isString(exportedComponent)) {
289+
} else if (isString(exportedComponent) || isSymbol(exportedComponent)) {
290290
return addExportedUnit(exportedComponent);
291291
} else if (this.isDynamicModule(exportedComponent)) {
292292
const { module } = exportedComponent;
@@ -305,7 +305,7 @@ export class Module {
305305
this._exports.add(this.validateExportedProvider(provide.name));
306306
}
307307

308-
public validateExportedProvider(token: string) {
308+
public validateExportedProvider(token: string | symbol) {
309309
if (this._components.has(token)) {
310310
return token;
311311
}
@@ -316,7 +316,7 @@ export class Module {
316316
.filter(metatype => metatype)
317317
.map(({ name }) => name);
318318

319-
if (!importedRefNames.includes(token)) {
319+
if (!importedRefNames.includes(token as any)) {
320320
const { name } = this.metatype;
321321
throw new UnknownExportException(name);
322322
}

packages/core/test/injector/injector.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ describe('Injector', () => {
6969
) as InstanceWrapper<MainTest>;
7070

7171
expect(instance.depOne).instanceof(DependencyOne);
72-
expect(instance.depTwo).instanceof(DependencyOne);
72+
expect(instance.depTwo).instanceof(DependencyTwo);
7373
expect(instance).instanceof(MainTest);
7474
});
7575

0 commit comments

Comments
 (0)