From 0f0176aa0c065920c6797d61de9aa9e5ca301922 Mon Sep 17 00:00:00 2001 From: "Micael Levi L. Cavalcante" Date: Thu, 20 Jun 2024 22:31:47 -0400 Subject: [PATCH] feat: less verbose module referencing on errors/debug messages --- lib/conditional.module.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/conditional.module.ts b/lib/conditional.module.ts index c7e10047..a67494a0 100644 --- a/lib/conditional.module.ts +++ b/lib/conditional.module.ts @@ -1,6 +1,23 @@ -import { DynamicModule, Logger, ModuleMetadata } from '@nestjs/common'; +import { DynamicModule, ForwardReference, Logger, ModuleMetadata, Type } from '@nestjs/common'; import { ConfigModule } from './config.module'; +/** + * Same logic as in `@nestjs/core` package. + * @param instance The instance which should get the name from + * @returns The name of an instance or `undefined` + */ +const getInstanceName = (instance: unknown): string | undefined => { + if ((instance as ForwardReference)?.forwardRef) { + return (instance as ForwardReference).forwardRef()?.name; + } + + if ((instance as DynamicModule).module) { + return (instance as DynamicModule).module?.name; + } + + return (instance as Type).name; +}; + /** * @publicApi */ @@ -14,10 +31,11 @@ export class ConditionalModule { options?: { timeout?: number; debug?: boolean }, ) { const { timeout = 5000, debug = true } = options ?? {}; + const moduleName = getInstanceName(module) || module.toString(); const timer = setTimeout(() => { throw new Error( - `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${module.toString()} should be registered or not`, + `Nest was not able to resolve the config variables within ${timeout} milliseconds. Bause of this, the ConditionalModule was not able to determine if ${moduleName} should be registered or not`, ); }, timeout); timer.unref(); @@ -40,7 +58,7 @@ export class ConditionalModule { } else { if (debug) { Logger.debug( - `${condition.toString()} evaluated to false. Skipping the registration of ${module.toString()}`, + `${condition.toString()} evaluated to false. Skipping the registration of ${moduleName}`, ConditionalModule.name, ); }