All files / 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 29 30 311x                                     1x     1x   1x     1x      
import { TSinjex } from '../classes/TSinjex';
import { Identifier } from '../types/Identifier';
 
/**
 * A decorator to register a class in the **TSinjex** 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.
 * @see {@link Identifier} for more information on identifiers.
 * @param deprecated If true, the dependency is deprecated and a warning
 * is logged only once upon the first resolution of the dependency.
 * @returns The decorator function to be applied on the class.
 * @example
 * ```ts
 * \@Register('MyClassIdentifier')
 * class MyClass {
 *   // ...
 * }
 * ```
 */
export function Register<
    TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
>(identifier: Identifier, deprecated?: boolean) {
    return function (constructor: TargetType, ...args: unknown[]): void {
        // Get the instance of the DI container
        const diContainer = TSinjex.getInstance();
 
        // Register the class in the DI container
        diContainer.register(identifier, constructor, deprecated);
    };
}
 
Zur TypeDoc-Dokumentation