mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import type { ExecutionContext } from '@nestjs/common';
|
|
import type { Type, ContextType } from '@nestjs/common';
|
|
import type {
|
|
HttpArgumentsHost,
|
|
RpcArgumentsHost,
|
|
WsArgumentsHost,
|
|
} from '@nestjs/common/internal';
|
|
|
|
export class ExecutionContextHost implements ExecutionContext {
|
|
private contextType = 'http';
|
|
|
|
constructor(
|
|
private readonly args: any[],
|
|
private readonly constructorRef: Type<any> | null = null,
|
|
private readonly handler: Function | null = null,
|
|
) {}
|
|
|
|
setType<TContext extends string = ContextType>(type: TContext) {
|
|
type && (this.contextType = type);
|
|
}
|
|
|
|
getType<TContext extends string = ContextType>(): TContext {
|
|
return this.contextType as TContext;
|
|
}
|
|
|
|
getClass<T = any>(): Type<T> {
|
|
return this.constructorRef!;
|
|
}
|
|
|
|
getHandler(): Function {
|
|
return this.handler!;
|
|
}
|
|
|
|
getArgs<T extends Array<any> = any[]>(): T {
|
|
return this.args as T;
|
|
}
|
|
|
|
getArgByIndex<T = any>(index: number): T {
|
|
return this.args[index] as T;
|
|
}
|
|
|
|
switchToRpc(): RpcArgumentsHost {
|
|
return Object.assign(this, {
|
|
getData: () => this.getArgByIndex(0),
|
|
getContext: () => this.getArgByIndex(1),
|
|
});
|
|
}
|
|
|
|
switchToHttp(): HttpArgumentsHost {
|
|
return Object.assign(this, {
|
|
getRequest: () => this.getArgByIndex(0),
|
|
getResponse: () => this.getArgByIndex(1),
|
|
getNext: () => this.getArgByIndex(2),
|
|
});
|
|
}
|
|
|
|
switchToWs(): WsArgumentsHost {
|
|
return Object.assign(this, {
|
|
getClient: () => this.getArgByIndex(0),
|
|
getData: () => this.getArgByIndex(1),
|
|
getPattern: () => this.getArgByIndex(this.getArgs().length - 1),
|
|
});
|
|
}
|
|
}
|