feat!: Update Register
Decorator for stable decorator api of typescript
This commit is contained in:
@@ -1,14 +1,14 @@
|
|||||||
|
import { IdentifierRequiredError } from 'src/interfaces/Exceptions';
|
||||||
import { TSinjex } from '../classes/TSinjex';
|
import { TSinjex } from '../classes/TSinjex';
|
||||||
import { Identifier } from '../types/Identifier';
|
import { Identifier } from '../types/Identifier';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A decorator to register a class in the **TSinjex** DI (Dependency Injection) container.
|
* A decorator to register a class in the **TSinjex** DI (Dependency Injection) container.
|
||||||
* @template TargetType The type of the class to be registered.
|
* @template TargetType The type of the class to be registered.
|
||||||
* @param identifier The identifier used to register the class in the DI container.
|
* @param identifier The {@link Identifier|identifier} used to register the class in the DI container or the class name if not provided.
|
||||||
* @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.
|
||||||
* @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.
|
* @returns The decorator function to be applied on the class.
|
||||||
|
* @throws An {@link IdentifierRequiredError} if the identifier is not provided and the class name is not available.
|
||||||
* @example
|
* @example
|
||||||
* ```ts
|
* ```ts
|
||||||
* \@Register('MyClassIdentifier')
|
* \@Register('MyClassIdentifier')
|
||||||
@@ -19,12 +19,16 @@ import { Identifier } from '../types/Identifier';
|
|||||||
*/
|
*/
|
||||||
export function Register<
|
export function Register<
|
||||||
TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
|
TargetType extends new (...args: unknown[]) => InstanceType<TargetType>,
|
||||||
>(identifier: Identifier, deprecated?: boolean) {
|
>(identifier?: Identifier, deprecated?: boolean) {
|
||||||
return function (constructor: TargetType, ...args: unknown[]): void {
|
return function (
|
||||||
// Get the instance of the DI container
|
constructor: TargetType,
|
||||||
const diContainer = TSinjex.getInstance();
|
context: ClassDecoratorContext<TargetType>,
|
||||||
|
) {
|
||||||
|
const _identifier = identifier ?? context.name;
|
||||||
|
|
||||||
// Register the class in the DI container
|
if (_identifier == null) throw new IdentifierRequiredError();
|
||||||
diContainer.register(identifier, constructor, deprecated);
|
|
||||||
|
const diContainer = TSinjex.getInstance();
|
||||||
|
diContainer.register(_identifier, constructor, deprecated);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user