Rename TSInjex to TSinjex and Add Jest Setup in README

Renamed all instances of 'TSInjex' to 'TSinjex' for consistency across the codebase, including interfaces, classes, and test files. Updated the version in `package.json` from 0.0.5 to 0.0.6. Added Jest setup example to the `README.md` to guide developers on initial configuration for testing.
This commit is contained in:
2024-08-14 23:10:44 +02:00
parent ad4a8fd8c6
commit 771b810419
13 changed files with 222 additions and 200 deletions

View File

@@ -0,0 +1,22 @@
## Jest
### Example jest setup
```ts
module.exports = {
setupFilesAfterEnv: ['./scripts/jest.setup.js'],
preset: 'ts-jest',
testEnvironment: 'node',
testMatch: ['**/__tests__/**/*.test.ts', '**/?(*.)+(test).ts'],
moduleDirectories: ['node_modules', 'src'],
moduleNameMapper: {
'^src/(.*)$': '<rootDir>/src/$1', // Map src to the source folder
'^ts-injex$': '<rootDir>/node_modules/ts-injex/src', // Map ts-injex to the source folder
},
transformIgnorePatterns: [
'node_modules/(?!ts-injex)' // **Dont** ignore ts-injex on preset `ts-jest`
],
};
```

View File

@@ -1,6 +1,6 @@
{
"name": "ts-injex",
"version": "0.0.5",
"version": "0.0.6",
"description": "",
"type": "module",
"main": "./dist/index.js",

View File

@@ -1,103 +1,103 @@
import { ImplementsStatic } from './helper/ImplementsStatic';
import { IDependency } from './interfaces/IDependency';
import { ITSInjex, ITSInjex_ } from './interfaces/ITSInjex';
/**
* **TSInjex**: Dependency Injection Container
*/
@ImplementsStatic<ITSInjex_>()
export class TSInjex implements ITSInjex {
private static _instance: TSInjex;
private readonly _dependencies = new Map<string, IDependency>();
/**
* Private constructor to prevent direct instantiation.
*/
private constructor() {}
//#region IDIContainer_
/**
* Retrieves the singleton instance of DependencyRegistry.
* @returns The singleton instance.
*/
public static getInstance(): ITSInjex {
if (this._instance == null) {
this._instance = new TSInjex();
}
return this._instance;
}
/**
* @inheritdoc
* @see {@link ITSInjexRegister.register}
*/
public static register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
(TSInjex.getInstance() as TSInjex)._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
* @see {@link ITSInjexResolve.resolve}
*/
public static resolve<T>(
identifier: string,
necessary = true,
): T | undefined {
return (TSInjex.getInstance() as TSInjex).resolve<T>(
identifier,
necessary,
);
}
//#endregion
//#region IDIContainer
/**
* @inheritdoc
*/
public register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
this._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
*/
public resolve<T>(identifier: string, necessary = true): T | undefined {
const dependency = this._dependencies.get(identifier);
if (necessary && !dependency) {
throw new Error(`Dependency ${identifier} not found`);
} else if (!dependency) {
return undefined;
}
if (dependency.deprecated) {
// eslint-disable-next-line no-console
console.warn(`Dependency ${identifier} is deprecated`);
// Remove the deprecation warning; it should only be logged once.
dependency.deprecated = false;
}
return dependency.dependency as T;
}
//#endregion
}
import { ImplementsStatic } from './helper/ImplementsStatic';
import { IDependency } from './interfaces/IDependency';
import { ITSinjex, ITSinjex_ } from './interfaces/ITSinjex';
/**
* **TSInjex**: Dependency Injection Container
*/
@ImplementsStatic<ITSinjex_>()
export class TSinjex implements ITSinjex {
private static _instance: TSinjex;
private readonly _dependencies = new Map<string, IDependency>();
/**
* Private constructor to prevent direct instantiation.
*/
private constructor() {}
//#region IDIContainer_
/**
* Retrieves the singleton instance of DependencyRegistry.
* @returns The singleton instance.
*/
public static getInstance(): ITSinjex {
if (this._instance == null) {
this._instance = new TSinjex();
}
return this._instance;
}
/**
* @inheritdoc
* @see {@link ITSInjexRegister.register}
*/
public static register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
(TSinjex.getInstance() as TSinjex)._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
* @see {@link ITSInjexResolve.resolve}
*/
public static resolve<T>(
identifier: string,
necessary = true,
): T | undefined {
return (TSinjex.getInstance() as TSinjex).resolve<T>(
identifier,
necessary,
);
}
//#endregion
//#region IDIContainer
/**
* @inheritdoc
*/
public register<T>(
identifier: string,
dependency: T,
deprecated = false,
): void {
this._dependencies.set(identifier, {
dependency: dependency,
deprecated: deprecated,
});
}
/**
* @inheritdoc
*/
public resolve<T>(identifier: string, necessary = true): T | undefined {
const dependency = this._dependencies.get(identifier);
if (necessary && !dependency) {
throw new Error(`Dependency ${identifier} not found`);
} else if (!dependency) {
return undefined;
}
if (dependency.deprecated) {
// eslint-disable-next-line no-console
console.warn(`Dependency ${identifier} is deprecated`);
// Remove the deprecation warning; it should only be logged once.
dependency.deprecated = false;
}
return dependency.dependency as T;
}
//#endregion
}

View File

@@ -1,4 +1,4 @@
import { test_IDIContainer } from './IDIContainer.spec';
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
test_IDIContainer(TSInjex);
test_IDIContainer(TSinjex);

View File

@@ -1,13 +1,13 @@
import { ITSInjex_, ITSInjex } from '../interfaces/ITSInjex';
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
/**
* Test the implementation of a DIContainer
* @param Container The DIContainer implementation to test.
* Must implement {@link ITSInjex}, {@link ITSInjex_}
* Must implement {@link ITSinjex}, {@link ITSinjex_}
*/
export function test_IDIContainer(Container: ITSInjex_): void {
export function test_IDIContainer(Container: ITSinjex_): void {
describe('IDIContainer Implementation Tests', () => {
let container: ITSInjex;
let container: ITSinjex;
beforeEach(() => {
container = Container.getInstance();

View File

@@ -1,4 +1,4 @@
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
import { InitDelegate } from '../types/InitDelegate';
/**
@@ -12,7 +12,7 @@ import { InitDelegate } from '../types/InitDelegate';
* - If `true`, an error will be thrown if the dependency cannot be resolved.
* - If `false`, `undefined` will be returned if the dependency cannot be resolved.
* @returns A decorator function to be applied on the class property.
* @see {@link TSInjex}
* @see {@link TSinjex}
* @example
* ```ts
* class MyClass {
@@ -37,7 +37,7 @@ export function Inject<T, U>(
// Unique symbol to store the private property
const privatePropertyKey: unique symbol = Symbol();
// Get the DI container instance
const diContainer = TSInjex.getInstance();
const diContainer = TSinjex.getInstance();
// Function to evaluate the dependency lazily
// to avoid circular dependencies, not found dependencies, etc.

View File

@@ -1,4 +1,4 @@
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
/**
* A decorator to register a class in the DI (Dependency Injection) container.
@@ -20,7 +20,7 @@ export function Register<
>(identifier: string, deprecated?: boolean) {
return function (constructor: TargetType, ...args: unknown[]): void {
// Get the instance of the DI container
const diContainer = TSInjex.getInstance();
const diContainer = TSinjex.getInstance();
// Register the class in the DI container
diContainer.register(identifier, constructor, deprecated);

View File

@@ -1,4 +1,4 @@
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
import { InitDelegate } from '../types/InitDelegate';
/**
@@ -28,7 +28,7 @@ export function RegisterInstance<
) {
return function (constructor: TargetType, ...args: unknown[]): void {
// Get the instance of the DI container
const diContainer = TSInjex.getInstance();
const diContainer = TSinjex.getInstance();
// Create a proxy to instantiate the class when needed (Lazy Initialization)
let lazyProxy: unknown = new Proxy(

View File

@@ -1,4 +1,4 @@
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
/**
* Register a dependency.
@@ -31,5 +31,5 @@ export function register<T>(
dependency: T,
deprecated?: boolean,
): void {
TSInjex.getInstance().register(identifier, dependency, deprecated);
TSinjex.getInstance().register(identifier, dependency, deprecated);
}

View File

@@ -1,5 +1,5 @@
import { DependencyResolutionError } from '../interfaces/Exceptions';
import { TSInjex } from '../TSInjex';
import { TSinjex } from '../TSinjex';
/**
* Resolve a dependency.
@@ -29,5 +29,5 @@ export function resolve<T>(
identifier: string,
necessary?: boolean,
): T | undefined {
return TSInjex.getInstance().resolve<T>(identifier, necessary);
return TSinjex.getInstance().resolve<T>(identifier, necessary);
}

View File

@@ -1,7 +1,7 @@
export * from './TSInjex';
export * from './TSinjex';
export * from './decorators/Inject';
export * from './decorators/Register';
export * from './decorators/RegisterInstance';
export * from './functions/resolve';
export * from './functions/register';
export type * from './interfaces/ITSInjex';
export type * from './interfaces/ITSinjex';

View File

@@ -1,11 +1,11 @@
import { ITSInjex } from './ITSInjex';
import { ITSinjex } from './ITSinjex';
/**
* General error class for {@link ITSInjex} interface.
* General error class for {@link ITSinjex} interface.
*/
export class TSInjexError extends Error {
export class TSinjexError extends Error {
/**
* Creates a new instance of {@link TSInjexError}
* Creates a new instance of {@link TSinjexError}
* @param message **The error message**
*/
constructor(message: string) {
@@ -15,10 +15,10 @@ export class TSInjexError extends Error {
}
/**
* Error class for dependency resolution errors in {@link ITSInjex}.
* @see {@link ITSInjex.resolve}
* Error class for dependency resolution errors in {@link ITSinjex}.
* @see {@link ITSinjex.resolve}
*/
export class DependencyResolutionError extends TSInjexError {
export class DependencyResolutionError extends TSinjexError {
/**
* Creates a new instance of {@link DependencyResolutionError}
* @param identifier **The identifier of the dependency**

View File

@@ -1,70 +1,70 @@
/**
* Static TSInjex Interface
*/
export interface ITSInjex_ extends ITSInjexRegister, ITSInjexResolve {
/**
* Get the **singleton** TSInjex instance.
*/
getInstance(): ITSInjex;
}
/**
* Register method for static and instance Dependency Injection Container.
*/
export interface ITSInjexRegister {
/**
* Register a dependency.
* @param identifier The identifier of the dependency.
* @param dependency The dependency to register.
* @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 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 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;
}
/**
* Resolve method for static and instance Dependency Injection Container.
*/
export interface ITSInjexResolve {
/**
* Resolve a 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 or undefined if the dependency is not found
*/
resolve<T>(identifier: string, 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.
*/
resolve<T>(identifier: string, 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;
}
/**
* TSInjex Interface
*/
export interface ITSInjex extends ITSInjexRegister, ITSInjexResolve {}
/**
* Static TSInjex Interface
*/
export interface ITSinjex_ extends ITSinjexRegister, ITSinjexResolve {
/**
* Get the **singleton** TSInjex instance.
*/
getInstance(): ITSinjex;
}
/**
* Register method for static and instance Dependency Injection Container.
*/
export interface ITSinjexRegister {
/**
* Register a dependency.
* @param identifier The identifier of the dependency.
* @param dependency The dependency to register.
* @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 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 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;
}
/**
* Resolve method for static and instance Dependency Injection Container.
*/
export interface ITSinjexResolve {
/**
* Resolve a 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 or undefined if the dependency is not found
*/
resolve<T>(identifier: string, 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.
*/
resolve<T>(identifier: string, 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;
}
/**
* TSInjex Interface
*/
export interface ITSinjex extends ITSinjexRegister, ITSinjexResolve {}