Skip to content

Commit 6f85ea4

Browse files
improvement(core) use application ref host instead of http ref
1 parent 385071b commit 6f85ea4

3 files changed

Lines changed: 27 additions & 31 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/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;

0 commit comments

Comments
 (0)