mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
refactor: minor codebase tweaks
This commit is contained in:
@@ -77,9 +77,7 @@ export class ParseEnumPipe<T = any> implements PipeTransform<T> {
|
||||
}
|
||||
|
||||
protected isEnum(value: T): boolean {
|
||||
const enumValues = Object.keys(this.enumType as object).map(
|
||||
item => this.enumType[item],
|
||||
);
|
||||
const enumValues = Object.values(this.enumType as object);
|
||||
return enumValues.includes(value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export class GuardsConsumer {
|
||||
callback: (...args: unknown[]) => unknown,
|
||||
type?: TContext,
|
||||
): Promise<boolean> {
|
||||
if (!guards || isEmpty(guards)) {
|
||||
if (isEmpty(guards)) {
|
||||
return true;
|
||||
}
|
||||
const context = this.createContext(args, instance, callback);
|
||||
|
||||
@@ -809,7 +809,7 @@ export class Injector {
|
||||
properties: PropertyDependency[],
|
||||
): void {
|
||||
if (!isObject(instance)) {
|
||||
return undefined;
|
||||
return;
|
||||
}
|
||||
iterate(properties)
|
||||
.filter(item => !isNil(item.instance))
|
||||
|
||||
@@ -564,33 +564,37 @@ export class Module {
|
||||
}
|
||||
|
||||
public getProviderById<T = any>(id: string): InstanceWrapper<T> | undefined {
|
||||
return Array.from(this._providers.values()).find(
|
||||
item => item.id === id,
|
||||
) as InstanceWrapper<T>;
|
||||
for (const item of this._providers.values()) {
|
||||
if (item.id === id) return item as InstanceWrapper<T>;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getControllerById<T = any>(
|
||||
id: string,
|
||||
): InstanceWrapper<T> | undefined {
|
||||
return Array.from(this._controllers.values()).find(
|
||||
item => item.id === id,
|
||||
) as InstanceWrapper<T>;
|
||||
for (const item of this._controllers.values()) {
|
||||
if (item.id === id) return item as InstanceWrapper<T>;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getInjectableById<T = any>(
|
||||
id: string,
|
||||
): InstanceWrapper<T> | undefined {
|
||||
return Array.from(this._injectables.values()).find(
|
||||
item => item.id === id,
|
||||
) as InstanceWrapper<T>;
|
||||
for (const item of this._injectables.values()) {
|
||||
if (item.id === id) return item as InstanceWrapper<T>;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getMiddlewareById<T = any>(
|
||||
id: string,
|
||||
): InstanceWrapper<T> | undefined {
|
||||
return Array.from(this._middlewares.values()).find(
|
||||
item => item.id === id,
|
||||
) as InstanceWrapper<T>;
|
||||
for (const item of this._middlewares.values()) {
|
||||
if (item.id === id) return item as InstanceWrapper<T>;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
public getNonAliasProviders(): Array<
|
||||
|
||||
@@ -319,9 +319,7 @@ export class NestApplicationContext<
|
||||
options: ShutdownHooksOptions = {},
|
||||
): this {
|
||||
if (isEmpty(signals)) {
|
||||
signals = Object.keys(ShutdownSignal).map(
|
||||
(key: string) => ShutdownSignal[key],
|
||||
);
|
||||
signals = Object.values(ShutdownSignal);
|
||||
} else {
|
||||
// given signals array should be unique because
|
||||
// process shouldn't listen to the same signal more than once.
|
||||
|
||||
@@ -88,9 +88,9 @@ export class NestApplication
|
||||
}
|
||||
|
||||
protected async dispose(): Promise<void> {
|
||||
this.socketModule && (await this.socketModule.close());
|
||||
this.microservicesModule && (await this.microservicesModule.close());
|
||||
this.httpAdapter && (await this.httpAdapter.close());
|
||||
await this.socketModule?.close();
|
||||
await this.microservicesModule?.close();
|
||||
await this.httpAdapter?.close();
|
||||
|
||||
await Promise.all(
|
||||
iterate(this.microservices).map(async microservice => {
|
||||
@@ -444,19 +444,17 @@ export class NestApplication
|
||||
public useStaticAssets(options: any): this;
|
||||
public useStaticAssets(path: string, options?: any): this;
|
||||
public useStaticAssets(pathOrOptions: any, options?: any): this {
|
||||
this.httpAdapter.useStaticAssets &&
|
||||
this.httpAdapter.useStaticAssets(pathOrOptions, options);
|
||||
this.httpAdapter.useStaticAssets?.(pathOrOptions, options);
|
||||
return this;
|
||||
}
|
||||
|
||||
public setBaseViewsDir(path: string | string[]): this {
|
||||
this.httpAdapter.setBaseViewsDir && this.httpAdapter.setBaseViewsDir(path);
|
||||
this.httpAdapter.setBaseViewsDir?.(path);
|
||||
return this;
|
||||
}
|
||||
|
||||
public setViewEngine(engineOrOptions: any): this {
|
||||
this.httpAdapter.setViewEngine &&
|
||||
this.httpAdapter.setViewEngine(engineOrOptions);
|
||||
this.httpAdapter.setViewEngine?.(engineOrOptions);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -24,10 +24,10 @@ export class PipesConsumer {
|
||||
{ metatype, type, data }: { metatype: any; type?: any; data?: any },
|
||||
transforms: PipeTransform[],
|
||||
) {
|
||||
return transforms.reduce(async (deferredValue, pipe) => {
|
||||
const val = await deferredValue;
|
||||
const result = pipe.transform(val, { metatype, type, data });
|
||||
let result: unknown = value;
|
||||
for (const pipe of transforms) {
|
||||
result = await pipe.transform(result, { metatype, type, data });
|
||||
}
|
||||
return result;
|
||||
}, Promise.resolve(value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,9 +233,8 @@ export class RouterExecutionContext {
|
||||
);
|
||||
|
||||
const httpCode = this.reflectHttpStatusCode(callback);
|
||||
const httpStatusCode = httpCode
|
||||
? httpCode
|
||||
: this.responseController.getStatusByMethod(requestMethod);
|
||||
const httpStatusCode =
|
||||
httpCode ?? this.responseController.getStatusByMethod(requestMethod);
|
||||
|
||||
const responseHeaders = this.reflectResponseHeaders(callback);
|
||||
const hasCustomHeaders = !isEmpty(responseHeaders);
|
||||
|
||||
@@ -63,18 +63,14 @@ export class KafkaReplyPartitionAssigner {
|
||||
});
|
||||
|
||||
// build a collection of topics and partitions
|
||||
const topicsPartitions = group.topics
|
||||
.map(topic => {
|
||||
const topicsPartitions = group.topics.flatMap(topic => {
|
||||
const partitionMetadata =
|
||||
this.config.cluster.findTopicPartitionMetadata(topic);
|
||||
return partitionMetadata.map(m => {
|
||||
return {
|
||||
return partitionMetadata.map(m => ({
|
||||
topic,
|
||||
partitionId: m.partitionId,
|
||||
};
|
||||
}));
|
||||
});
|
||||
})
|
||||
.reduce((acc, val) => acc.concat(val), []);
|
||||
|
||||
// create the new assignment by populating the members with the first partition of the topics
|
||||
sortedMemberIds.forEach(assignee => {
|
||||
|
||||
@@ -84,12 +84,12 @@ export class ListenersController {
|
||||
isUndefined(serverInstance.transportId) ||
|
||||
transport === serverInstance.transportId,
|
||||
)
|
||||
.reduce((acc, handler) => {
|
||||
handler.patterns.forEach(pattern =>
|
||||
acc.push({ ...handler, patterns: [pattern] }),
|
||||
);
|
||||
return acc;
|
||||
}, [] as EventOrMessageListenerDefinition[])
|
||||
.flatMap(handler =>
|
||||
handler.patterns.map(pattern => ({
|
||||
...handler,
|
||||
patterns: [pattern],
|
||||
})),
|
||||
)
|
||||
.forEach((definition: EventOrMessageListenerDefinition) => {
|
||||
const {
|
||||
patterns: [pattern],
|
||||
|
||||
@@ -34,13 +34,10 @@ export class ClientsModule {
|
||||
|
||||
static registerAsync(options: ClientsModuleAsyncOptions): DynamicModule {
|
||||
const clientsOptions = !Array.isArray(options) ? options.clients : options;
|
||||
const providers: Provider[] = clientsOptions.reduce(
|
||||
(accProviders: Provider[], item) =>
|
||||
accProviders
|
||||
.concat(this.createAsyncProviders(item))
|
||||
.concat(item.extraProviders || []),
|
||||
[],
|
||||
);
|
||||
const providers: Provider[] = clientsOptions.flatMap(item => [
|
||||
...this.createAsyncProviders(item),
|
||||
...(item.extraProviders || []),
|
||||
]);
|
||||
const imports = clientsOptions.reduce(
|
||||
(accImports, option) => {
|
||||
if (!option.imports) {
|
||||
|
||||
Reference in New Issue
Block a user