All files / decorators RegisterInstance.ts

100% Statements 6/6
100% Branches 4/4
100% Functions 1/1
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 31 32 33 34 35 36 37 38 39 401x                                               1x                   4x   4x 2x    
import { Register } from './Register.js';
import { Identifier } from '../types/Identifier.js';
import { InitDelegate } from '../types/InitDelegate.js';
 
/**
 * A decorator to register an instance of a class in the DI (Dependency Injection) container.
 * @template TargetType The type of the class whose instance is to be registered.
 * @param identifier The identifier used to register the instance in the DI container.
 * @see {@link Identifier} for more information on identifiers.
 * @param init An optional initializer function which get the constructor of the class
 * as input and returns an instance of the class.
 * @param deprecated If true, the dependency is deprecated and a warning
 * is logged only once upon the first resolution of the dependency.
 * @see {@link InitDelegate} for more information on initializer functions.
 * @returns The decorator function to be applied on the class.
 * @example
 * ```ts
 * \@RegisterInstance('MyClassInstanceIdentifier', (constructor) => new constructor())
 * class MyClass {
 *   // ...
 * }
 * ```
 * @deprecated Use {@link Register} instead. This decorator already uses the {@link Register} decorator internally.
 */
export function RegisterInstance<
    TargetType extends new (..._args: unknown[]) => InstanceType<TargetType>,
>(
    identifier: Identifier,
    init?: InitDelegate<
        TargetType & { new (..._args: unknown[]): InstanceType<TargetType> },
        InstanceType<TargetType>
    >,
    deprecated?: boolean,
): (constructor: TargetType, ...args: unknown[]) => void {
    const initDelegate = typeof init === 'function' ? init : undefined;
 
    if (initDelegate) return Register(identifier, initDelegate, deprecated);
    else return Register(identifier, 'instance', deprecated);
}
 
Zur TypeDoc-Dokumentation