Compare commits

..

1 Commits

Author SHA1 Message Date
Kamil Myśliwiec
38bfa9a13a feat(core): allow overriding abort on error for select method 2024-11-07 12:18:12 +01:00
4 changed files with 25 additions and 24 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,
);

View File

@@ -39,36 +39,25 @@ export class ListenerMetadataExplorer {
const instancePrototype = Object.getPrototypeOf(instance);
return this.metadataScanner
.getAllMethodNames(instancePrototype)
.map(method =>
this.exploreMethodMetadata(instance, instancePrototype, method),
)
.map(method => this.exploreMethodMetadata(instancePrototype, method))
.filter(metadata => metadata);
}
public exploreMethodMetadata(
instance: Controller,
instancePrototype: object,
methodKey: string,
): EventOrMessageListenerDefinition {
const prototypeCallback = instancePrototype[methodKey];
const targetCallback = instancePrototype[methodKey];
const handlerType = Reflect.getMetadata(
PATTERN_HANDLER_METADATA,
prototypeCallback,
targetCallback,
);
if (isUndefined(handlerType)) {
return;
}
const patterns = Reflect.getMetadata(PATTERN_METADATA, prototypeCallback);
const transport = Reflect.getMetadata(
TRANSPORT_METADATA,
prototypeCallback,
);
const extras = Reflect.getMetadata(
PATTERN_EXTRAS_METADATA,
prototypeCallback,
);
const targetCallback = instance[methodKey];
const patterns = Reflect.getMetadata(PATTERN_METADATA, targetCallback);
const transport = Reflect.getMetadata(TRANSPORT_METADATA, targetCallback);
const extras = Reflect.getMetadata(PATTERN_EXTRAS_METADATA, targetCallback);
return {
methodKey,
targetCallback,

View File

@@ -71,7 +71,6 @@ describe('ListenerMetadataExplorer', () => {
});
it(`should return undefined when "handlerType" metadata is undefined`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'noPattern',
);
@@ -81,7 +80,6 @@ describe('ListenerMetadataExplorer', () => {
describe('@MessagePattern', () => {
it(`should return pattern properties when "handlerType" metadata is not undefined`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'testMessage',
);
@@ -98,7 +96,6 @@ describe('ListenerMetadataExplorer', () => {
});
it(`should return multiple patterns when more than one is declared`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'testMultipleMessage',
);
@@ -119,7 +116,6 @@ describe('ListenerMetadataExplorer', () => {
describe('@EventPattern', () => {
it(`should return pattern properties when "handlerType" metadata is not undefined`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'testEvent',
);
@@ -136,7 +132,6 @@ describe('ListenerMetadataExplorer', () => {
});
it(`should return multiple patterns when more than one is declared`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'testMultipleEvent',
);