Refactor DI container for better structure and clarity

- Moved `TSinjex` class to `classes` directory for better organization.
- Updated imports across the codebase to reflect the new location of `TSinjex`.
- Introduced `Identifier` type to standardize dependency identifiers.
- Enhanced JSDoc comments for improved clarity and consistency.
- Adjusted error messages for `DependencyResolutionError` to provide clearer information.
- Updated and expanded decorator and function types to use `Identifier` type.
This commit is contained in:
2024-08-16 16:13:17 +02:00
committed by Max P.
parent 5277f93df1
commit 41be08e02e
12 changed files with 221 additions and 157 deletions

View File

@@ -10,7 +10,7 @@ export class TSinjexError extends Error {
*/
constructor(message: string) {
super(message);
this.name = 'TSInjex';
this.name = 'TSinjex';
}
}
@@ -24,7 +24,7 @@ export class DependencyResolutionError extends TSinjexError {
* @param identifier **The identifier of the dependency**
*/
constructor(identifier: string) {
super(`Dependency ${identifier} not found.`);
this.name = 'TSInjexResolutionError';
super(`Dependency ${identifier} could not be resolved.`);
this.name = 'TSinjexResolutionError';
}
}

View File

@@ -1,15 +1,19 @@
import { Identifier } from 'src/types/Identifier';
import { DependencyResolutionError } from './Exceptions';
/**
* Static TSInjex Interface
*/
export interface ITSinjex_ extends ITSinjexRegister, ITSinjexResolve {
/**
* Get the **singleton** TSInjex instance.
* @returns The singleton instance.
*/
getInstance(): ITSinjex;
}
/**
* Register method for static and instance Dependency Injection Container.
* `Register` method for static and instance Dependency Injection Container.
*/
export interface ITSinjexRegister {
/**
@@ -19,25 +23,33 @@ export interface ITSinjexRegister {
* @param deprecated If true, the dependency is deprecated => a warning
* is logged when the dependency is resolved.
*/
register<T>(identifier: string, dependency: T, deprecated?: boolean): void;
register<T>(
identifier: Identifier,
dependency: T,
deprecated?: boolean,
): void;
/**
* Register a deprecated dependency.
* @param identifier The identifier of the dependency.
* @param dependency The dependency to register.
* @param deprecated A warning is logged when the dependency is resolved.
*/
register<T>(identifier: string, dependency: T, deprecated?: true): void;
register<T>(identifier: Identifier, dependency: T, deprecated?: true): void;
/**
* Register a dependency.
* @param identifier The identifier of the dependency.
* @param dependency The dependency to register.
* @param deprecated No warning is logged when the dependency is resolved.
*/
register<T>(identifier: string, dependency: T, deprecated?: false): void;
register<T>(
identifier: Identifier,
dependency: T,
deprecated?: false,
): void;
}
/**
* Resolve method for static and instance Dependency Injection Container.
* `Resolve` method for static and instance Dependency Injection Container.
*/
export interface ITSinjexResolve {
/**
@@ -45,26 +57,27 @@ export interface ITSinjexResolve {
* @param identifier The identifier of the dependency
* @param necessary If true, throws an error if the dependency is not found
* @returns The resolved dependency or undefined if the dependency is not found
* @throws A {@link DependencyResolutionError} if the dependency is not found and necessary.
*/
resolve<T>(identifier: string, necessary?: boolean): T | undefined;
resolve<T>(identifier: Identifier, necessary?: boolean): T | undefined;
/**
* Resolve a necessary dependency.
* @param identifier The identifier of the dependency.
* @param necessary If true, throws an error if the dependency is not found.
* @returns The resolved dependency.
* @throws Error if the dependency is not found.
* @throws A {@link DependencyResolutionError} if the dependency is not found.
*/
resolve<T>(identifier: string, necessary?: true): T;
resolve<T>(identifier: Identifier, necessary?: true): T;
/**
* Resolve a non necessary dependency
* @param identifier The identifier of the dependency
* @param necessary Not necessary, does not throw an error if the dependency is not found.
* @returns The resolved dependency or undefined if the dependency is not found
*/
resolve<T>(identifier: string, necessary?: false): T | undefined;
resolve<T>(identifier: Identifier, necessary?: false): T | undefined;
}
/**
* TSInjex Interface
* Instance TSinjex Interface
*/
export interface ITSinjex extends ITSinjexRegister, ITSinjexResolve {}