Skip to content

Commit 155f917

Browse files
refactor(core) simplify getType usage
1 parent 886220d commit 155f917

7 files changed

Lines changed: 14 additions & 15 deletions

File tree

packages/common/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export {
1818
BeforeApplicationShutdown,
1919
CallHandler,
2020
CanActivate,
21+
ContextType,
2122
DynamicModule,
2223
ExceptionFilter,
2324
ExecutionContext,
@@ -39,12 +40,12 @@ export {
3940
Provider,
4041
RpcExceptionFilter,
4142
Scope,
43+
ScopeOptions,
4244
Type,
4345
ValidationError,
4446
WebSocketAdapter,
4547
WsExceptionFilter,
4648
WsMessageHandler,
47-
ScopeOptions,
4849
} from './interfaces';
4950
export * from './pipes';
5051
export * from './serializer';

packages/common/interfaces/features/arguments-host.interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export interface RpcArgumentsHost {
5252
*
5353
* @publicApi
5454
*/
55-
export interface ArgumentsHost<TContext extends string = ContextType> {
55+
export interface ArgumentsHost {
5656
/**
5757
* Returns the array of arguments being passed to the handler.
5858
*/
@@ -80,5 +80,5 @@ export interface ArgumentsHost<TContext extends string = ContextType> {
8080
/**
8181
* Returns the current execution context type (string)
8282
*/
83-
getType(): TContext;
83+
getType<TContext extends string = ContextType>(): TContext;
8484
}

packages/common/interfaces/features/execution-context.interface.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Type } from '../index';
2-
import { ArgumentsHost, ContextType } from './arguments-host.interface';
2+
import { ArgumentsHost } from './arguments-host.interface';
33

44
/**
55
* Interface describing details about the current request pipeline.
@@ -8,8 +8,7 @@ import { ArgumentsHost, ContextType } from './arguments-host.interface';
88
*
99
* @publicApi
1010
*/
11-
export interface ExecutionContext<TContext extends ContextType = ContextType>
12-
extends ArgumentsHost<TContext> {
11+
export interface ExecutionContext extends ArgumentsHost {
1312
/**
1413
* Returns the *type* of the controller class which the current handler belongs to.
1514
*/

packages/core/guards/guards-consumer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class GuardsConsumer {
1616
return true;
1717
}
1818
const context = this.createContext(args, instance, callback);
19-
context.setType(type);
19+
context.setType<TContext>(type);
2020

2121
for (const guard of guards) {
2222
const result = guard.canActivate(context);

packages/core/helpers/execution-context-host.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ import {
77
WsArgumentsHost,
88
} from '@nestjs/common/interfaces/features/arguments-host.interface';
99

10-
export class ExecutionContextHost<TContext extends ContextType = ContextType>
11-
implements ExecutionContext<TContext> {
12-
private contextType: TContext = 'http' as TContext;
10+
export class ExecutionContextHost implements ExecutionContext {
11+
private contextType = 'http';
1312

1413
constructor(
1514
private readonly args: any[],
1615
private readonly constructorRef: Type<any> = null,
1716
private readonly handler: Function = null,
1817
) {}
1918

20-
setType(type: TContext) {
19+
setType<TContext extends string = ContextType>(type: TContext) {
2120
type && (this.contextType = type);
2221
}
2322

24-
getType(): TContext {
25-
return this.contextType;
23+
getType<TContext extends string = ContextType>(): TContext {
24+
return this.contextType as TContext;
2625
}
2726

2827
getClass<T = any>(): Type<T> {

packages/core/helpers/external-proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class ExternalErrorProxy {
1313
return await targetCallback(...args);
1414
} catch (e) {
1515
const host = new ExecutionContextHost(args);
16-
host.setType(type);
16+
host.setType<TContext>(type);
1717
return exceptionsHandler.next(e, host);
1818
}
1919
};

packages/core/interceptors/interceptors-consumer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class InterceptorsConsumer {
2222
return next();
2323
}
2424
const context = this.createContext(args, instance, callback);
25-
context.setType(type);
25+
context.setType<TContext>(type);
2626

2727
const start$ = defer(() => this.transformDeffered(next));
2828
const nextFn = (i = 0) => async () => {

0 commit comments

Comments
 (0)