All files / src/libs/DependencyInjection/decorators Register.ts

100% Statements 5/5
100% Branches 0/0
100% Functions 2/2
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 2914x                                 14x     14x   14x     14x      
import { DIContainer } from '../DIContainer';
 
/**
 * A decorator to register a class in the DI (Dependency Injection) container.
 * @template TargetType The type of the class to be registered.
 * @param identifier The identifier used to register the class in the DI container.
 * @param deprecated If true, the dependency is deprecated => a warning
 * is logged when the dependency is resolved.
 * @returns A function that is applied as a decorator to the class.
 * @example
 * ```ts
 * \@Register('MyClassIdentifier')
 * class MyClass {
 *   // ...
 * }
 * ```
 */
export function Register<
    TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
>(identifier: string, deprecated?: boolean) {
    return function (constructor: TargetType, ...args: unknown[]): void {
        // Get the instance of the DI container
        const diContainer = DIContainer.getInstance();
 
        // Register the class in the DI container
        diContainer.register(identifier, constructor, deprecated);
    };
}
 
Zur TypeDoc-Dokumentation