feat(core): allow overriding abort on error for select method

This commit is contained in:
Kamil Myśliwiec
2024-11-07 12:18:12 +01:00
parent bc4667c15a
commit 38bfa9a13a
2 changed files with 19 additions and 2 deletions

View File

@@ -1,8 +1,11 @@
import { ShutdownSignal } from '../enums/shutdown-signal.enum';
import { LoggerService, LogLevel } from '../services/logger.service';
import { DynamicModule } from './modules';
import { NestApplicationContextOptions } from './nest-application-context-options.interface';
import { Type } from './type.interface';
export type SelectOptions = Pick<NestApplicationContextOptions, 'abortOnError'>;
export interface GetOrResolveOptions {
/**
* If enabled, lookup will only be performed in the host module.
@@ -27,7 +30,10 @@ export interface INestApplicationContext {
* Allows navigating through the modules tree, for example, to pull out a specific instance from the selected module.
* @returns {INestApplicationContext}
*/
select<T>(module: Type<T> | DynamicModule): INestApplicationContext;
select<T>(
module: Type<T> | DynamicModule,
options?: SelectOptions,
): INestApplicationContext;
/**
* Retrieves an instance of either injectable or controller, otherwise, throws exception.

View File

@@ -9,6 +9,7 @@ import {
Abstract,
DynamicModule,
GetOrResolveOptions,
SelectOptions,
Type,
} from '@nestjs/common/interfaces';
import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
@@ -87,6 +88,7 @@ export class NestApplicationContext<
*/
public select<T>(
moduleType: Type<T> | DynamicModule,
selectOptions?: SelectOptions,
): INestApplicationContext {
const modulesContainer = this.container.getModules();
const contextModuleCtor = this.contextModule.metatype;
@@ -101,9 +103,18 @@ export class NestApplicationContext<
if (!selectedModule) {
throw new UnknownModuleException(type.name);
}
const options =
typeof selectOptions?.abortOnError !== 'undefined'
? {
...this.appOptions,
...selectOptions,
}
: this.appOptions;
return new NestApplicationContext(
this.container,
this.appOptions,
options,
selectedModule,
scope,
);