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:
22
README.md
22
README.md
@@ -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`
|
||||||
|
],
|
||||||
|
};
|
||||||
|
```
|
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ts-injex",
|
"name": "ts-injex",
|
||||||
"version": "0.0.5",
|
"version": "0.0.6",
|
||||||
"description": "",
|
"description": "",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "./dist/index.js",
|
"main": "./dist/index.js",
|
||||||
|
@@ -1,103 +1,103 @@
|
|||||||
import { ImplementsStatic } from './helper/ImplementsStatic';
|
import { ImplementsStatic } from './helper/ImplementsStatic';
|
||||||
import { IDependency } from './interfaces/IDependency';
|
import { IDependency } from './interfaces/IDependency';
|
||||||
import { ITSInjex, ITSInjex_ } from './interfaces/ITSInjex';
|
import { ITSinjex, ITSinjex_ } from './interfaces/ITSinjex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* **TSInjex**: Dependency Injection Container
|
* **TSInjex**: Dependency Injection Container
|
||||||
*/
|
*/
|
||||||
@ImplementsStatic<ITSInjex_>()
|
@ImplementsStatic<ITSinjex_>()
|
||||||
export class TSInjex implements ITSInjex {
|
export class TSinjex implements ITSinjex {
|
||||||
private static _instance: TSInjex;
|
private static _instance: TSinjex;
|
||||||
private readonly _dependencies = new Map<string, IDependency>();
|
private readonly _dependencies = new Map<string, IDependency>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private constructor to prevent direct instantiation.
|
* Private constructor to prevent direct instantiation.
|
||||||
*/
|
*/
|
||||||
private constructor() {}
|
private constructor() {}
|
||||||
|
|
||||||
//#region IDIContainer_
|
//#region IDIContainer_
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the singleton instance of DependencyRegistry.
|
* Retrieves the singleton instance of DependencyRegistry.
|
||||||
* @returns The singleton instance.
|
* @returns The singleton instance.
|
||||||
*/
|
*/
|
||||||
public static getInstance(): ITSInjex {
|
public static getInstance(): ITSinjex {
|
||||||
if (this._instance == null) {
|
if (this._instance == null) {
|
||||||
this._instance = new TSInjex();
|
this._instance = new TSinjex();
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._instance;
|
return this._instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
* @see {@link ITSInjexRegister.register}
|
* @see {@link ITSInjexRegister.register}
|
||||||
*/
|
*/
|
||||||
public static register<T>(
|
public static register<T>(
|
||||||
identifier: string,
|
identifier: string,
|
||||||
dependency: T,
|
dependency: T,
|
||||||
deprecated = false,
|
deprecated = false,
|
||||||
): void {
|
): void {
|
||||||
(TSInjex.getInstance() as TSInjex)._dependencies.set(identifier, {
|
(TSinjex.getInstance() as TSinjex)._dependencies.set(identifier, {
|
||||||
dependency: dependency,
|
dependency: dependency,
|
||||||
deprecated: deprecated,
|
deprecated: deprecated,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
* @see {@link ITSInjexResolve.resolve}
|
* @see {@link ITSInjexResolve.resolve}
|
||||||
*/
|
*/
|
||||||
public static resolve<T>(
|
public static resolve<T>(
|
||||||
identifier: string,
|
identifier: string,
|
||||||
necessary = true,
|
necessary = true,
|
||||||
): T | undefined {
|
): T | undefined {
|
||||||
return (TSInjex.getInstance() as TSInjex).resolve<T>(
|
return (TSinjex.getInstance() as TSinjex).resolve<T>(
|
||||||
identifier,
|
identifier,
|
||||||
necessary,
|
necessary,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
//#region IDIContainer
|
//#region IDIContainer
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
public register<T>(
|
public register<T>(
|
||||||
identifier: string,
|
identifier: string,
|
||||||
dependency: T,
|
dependency: T,
|
||||||
deprecated = false,
|
deprecated = false,
|
||||||
): void {
|
): void {
|
||||||
this._dependencies.set(identifier, {
|
this._dependencies.set(identifier, {
|
||||||
dependency: dependency,
|
dependency: dependency,
|
||||||
deprecated: deprecated,
|
deprecated: deprecated,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritdoc
|
* @inheritdoc
|
||||||
*/
|
*/
|
||||||
public resolve<T>(identifier: string, necessary = true): T | undefined {
|
public resolve<T>(identifier: string, necessary = true): T | undefined {
|
||||||
const dependency = this._dependencies.get(identifier);
|
const dependency = this._dependencies.get(identifier);
|
||||||
|
|
||||||
if (necessary && !dependency) {
|
if (necessary && !dependency) {
|
||||||
throw new Error(`Dependency ${identifier} not found`);
|
throw new Error(`Dependency ${identifier} not found`);
|
||||||
} else if (!dependency) {
|
} else if (!dependency) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dependency.deprecated) {
|
if (dependency.deprecated) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.warn(`Dependency ${identifier} is deprecated`);
|
console.warn(`Dependency ${identifier} is deprecated`);
|
||||||
|
|
||||||
// Remove the deprecation warning; it should only be logged once.
|
// Remove the deprecation warning; it should only be logged once.
|
||||||
dependency.deprecated = false;
|
dependency.deprecated = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return dependency.dependency as T;
|
return dependency.dependency as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#endregion
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
import { test_IDIContainer } from './IDIContainer.spec';
|
import { test_IDIContainer } from './IDIContainer.spec';
|
||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
|
|
||||||
test_IDIContainer(TSInjex);
|
test_IDIContainer(TSinjex);
|
||||||
|
@@ -1,13 +1,13 @@
|
|||||||
import { ITSInjex_, ITSInjex } from '../interfaces/ITSInjex';
|
import { ITSinjex_, ITSinjex } from '../interfaces/ITSinjex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test the implementation of a DIContainer
|
* Test the implementation of a DIContainer
|
||||||
* @param Container The DIContainer implementation to test.
|
* @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', () => {
|
describe('IDIContainer Implementation Tests', () => {
|
||||||
let container: ITSInjex;
|
let container: ITSinjex;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
container = Container.getInstance();
|
container = Container.getInstance();
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
import { InitDelegate } from '../types/InitDelegate';
|
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 `true`, an error will be thrown if the dependency cannot be resolved.
|
||||||
* - If `false`, `undefined` will be returned 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.
|
* @returns A decorator function to be applied on the class property.
|
||||||
* @see {@link TSInjex}
|
* @see {@link TSinjex}
|
||||||
* @example
|
* @example
|
||||||
* ```ts
|
* ```ts
|
||||||
* class MyClass {
|
* class MyClass {
|
||||||
@@ -37,7 +37,7 @@ export function Inject<T, U>(
|
|||||||
// Unique symbol to store the private property
|
// Unique symbol to store the private property
|
||||||
const privatePropertyKey: unique symbol = Symbol();
|
const privatePropertyKey: unique symbol = Symbol();
|
||||||
// Get the DI container instance
|
// Get the DI container instance
|
||||||
const diContainer = TSInjex.getInstance();
|
const diContainer = TSinjex.getInstance();
|
||||||
|
|
||||||
// Function to evaluate the dependency lazily
|
// Function to evaluate the dependency lazily
|
||||||
// to avoid circular dependencies, not found dependencies, etc.
|
// to avoid circular dependencies, not found dependencies, etc.
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A decorator to register a class in the DI (Dependency Injection) container.
|
* A decorator to register a class in the DI (Dependency Injection) container.
|
||||||
@@ -20,7 +20,7 @@ export function Register<
|
|||||||
>(identifier: string, deprecated?: boolean) {
|
>(identifier: string, deprecated?: boolean) {
|
||||||
return function (constructor: TargetType, ...args: unknown[]): void {
|
return function (constructor: TargetType, ...args: unknown[]): void {
|
||||||
// Get the instance of the DI container
|
// Get the instance of the DI container
|
||||||
const diContainer = TSInjex.getInstance();
|
const diContainer = TSinjex.getInstance();
|
||||||
|
|
||||||
// Register the class in the DI container
|
// Register the class in the DI container
|
||||||
diContainer.register(identifier, constructor, deprecated);
|
diContainer.register(identifier, constructor, deprecated);
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
import { InitDelegate } from '../types/InitDelegate';
|
import { InitDelegate } from '../types/InitDelegate';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -28,7 +28,7 @@ export function RegisterInstance<
|
|||||||
) {
|
) {
|
||||||
return function (constructor: TargetType, ...args: unknown[]): void {
|
return function (constructor: TargetType, ...args: unknown[]): void {
|
||||||
// Get the instance of the DI container
|
// 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)
|
// Create a proxy to instantiate the class when needed (Lazy Initialization)
|
||||||
let lazyProxy: unknown = new Proxy(
|
let lazyProxy: unknown = new Proxy(
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a dependency.
|
* Register a dependency.
|
||||||
@@ -31,5 +31,5 @@ export function register<T>(
|
|||||||
dependency: T,
|
dependency: T,
|
||||||
deprecated?: boolean,
|
deprecated?: boolean,
|
||||||
): void {
|
): void {
|
||||||
TSInjex.getInstance().register(identifier, dependency, deprecated);
|
TSinjex.getInstance().register(identifier, dependency, deprecated);
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
import { DependencyResolutionError } from '../interfaces/Exceptions';
|
import { DependencyResolutionError } from '../interfaces/Exceptions';
|
||||||
import { TSInjex } from '../TSInjex';
|
import { TSinjex } from '../TSinjex';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve a dependency.
|
* Resolve a dependency.
|
||||||
@@ -29,5 +29,5 @@ export function resolve<T>(
|
|||||||
identifier: string,
|
identifier: string,
|
||||||
necessary?: boolean,
|
necessary?: boolean,
|
||||||
): T | undefined {
|
): T | undefined {
|
||||||
return TSInjex.getInstance().resolve<T>(identifier, necessary);
|
return TSinjex.getInstance().resolve<T>(identifier, necessary);
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
export * from './TSInjex';
|
export * from './TSinjex';
|
||||||
export * from './decorators/Inject';
|
export * from './decorators/Inject';
|
||||||
export * from './decorators/Register';
|
export * from './decorators/Register';
|
||||||
export * from './decorators/RegisterInstance';
|
export * from './decorators/RegisterInstance';
|
||||||
export * from './functions/resolve';
|
export * from './functions/resolve';
|
||||||
export * from './functions/register';
|
export * from './functions/register';
|
||||||
export type * from './interfaces/ITSInjex';
|
export type * from './interfaces/ITSinjex';
|
||||||
|
@@ -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**
|
* @param message **The error message**
|
||||||
*/
|
*/
|
||||||
constructor(message: string) {
|
constructor(message: string) {
|
||||||
@@ -15,10 +15,10 @@ export class TSInjexError extends Error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Error class for dependency resolution errors in {@link ITSInjex}.
|
* Error class for dependency resolution errors in {@link ITSinjex}.
|
||||||
* @see {@link ITSInjex.resolve}
|
* @see {@link ITSinjex.resolve}
|
||||||
*/
|
*/
|
||||||
export class DependencyResolutionError extends TSInjexError {
|
export class DependencyResolutionError extends TSinjexError {
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of {@link DependencyResolutionError}
|
* Creates a new instance of {@link DependencyResolutionError}
|
||||||
* @param identifier **The identifier of the dependency**
|
* @param identifier **The identifier of the dependency**
|
||||||
|
@@ -1,70 +1,70 @@
|
|||||||
/**
|
/**
|
||||||
* Static TSInjex Interface
|
* Static TSInjex Interface
|
||||||
*/
|
*/
|
||||||
export interface ITSInjex_ extends ITSInjexRegister, ITSInjexResolve {
|
export interface ITSinjex_ extends ITSinjexRegister, ITSinjexResolve {
|
||||||
/**
|
/**
|
||||||
* Get the **singleton** TSInjex instance.
|
* Get the **singleton** TSInjex instance.
|
||||||
*/
|
*/
|
||||||
getInstance(): ITSInjex;
|
getInstance(): ITSinjex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register method for static and instance Dependency Injection Container.
|
* Register method for static and instance Dependency Injection Container.
|
||||||
*/
|
*/
|
||||||
export interface ITSInjexRegister {
|
export interface ITSinjexRegister {
|
||||||
/**
|
/**
|
||||||
* Register a dependency.
|
* Register a dependency.
|
||||||
* @param identifier The identifier of the dependency.
|
* @param identifier The identifier of the dependency.
|
||||||
* @param dependency The dependency to register.
|
* @param dependency The dependency to register.
|
||||||
* @param deprecated If true, the dependency is deprecated => a warning
|
* @param deprecated If true, the dependency is deprecated => a warning
|
||||||
* is logged when the dependency is resolved.
|
* is logged when the dependency is resolved.
|
||||||
*/
|
*/
|
||||||
register<T>(identifier: string, dependency: T, deprecated?: boolean): void;
|
register<T>(identifier: string, dependency: T, deprecated?: boolean): void;
|
||||||
/**
|
/**
|
||||||
* Register a deprecated dependency.
|
* Register a deprecated dependency.
|
||||||
* @param identifier The identifier of the dependency.
|
* @param identifier The identifier of the dependency.
|
||||||
* @param dependency The dependency to register.
|
* @param dependency The dependency to register.
|
||||||
* @param deprecated A warning is logged when the dependency is resolved.
|
* @param deprecated A warning is logged when the dependency is resolved.
|
||||||
*/
|
*/
|
||||||
register<T>(identifier: string, dependency: T, deprecated?: true): void;
|
register<T>(identifier: string, dependency: T, deprecated?: true): void;
|
||||||
/**
|
/**
|
||||||
* Register a dependency.
|
* Register a dependency.
|
||||||
* @param identifier The identifier of the dependency.
|
* @param identifier The identifier of the dependency.
|
||||||
* @param dependency The dependency to register.
|
* @param dependency The dependency to register.
|
||||||
* @param deprecated No warning is logged when the dependency is resolved.
|
* @param deprecated No warning is logged when the dependency is resolved.
|
||||||
*/
|
*/
|
||||||
register<T>(identifier: string, dependency: T, deprecated?: false): void;
|
register<T>(identifier: string, 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 {
|
export interface ITSinjexResolve {
|
||||||
/**
|
/**
|
||||||
* Resolve a dependency
|
* Resolve a dependency
|
||||||
* @param identifier The identifier of the dependency
|
* @param identifier The identifier of the dependency
|
||||||
* @param necessary If true, throws an error if the dependency is not found
|
* @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
|
* @returns The resolved dependency or undefined if the dependency is not found
|
||||||
*/
|
*/
|
||||||
resolve<T>(identifier: string, necessary?: boolean): T | undefined;
|
resolve<T>(identifier: string, necessary?: boolean): T | undefined;
|
||||||
/**
|
/**
|
||||||
* Resolve a necessary dependency.
|
* Resolve a necessary dependency.
|
||||||
* @param identifier The identifier of the dependency.
|
* @param identifier The identifier of the dependency.
|
||||||
* @param necessary If true, throws an error if the dependency is not found.
|
* @param necessary If true, throws an error if the dependency is not found.
|
||||||
* @returns The resolved dependency.
|
* @returns The resolved dependency.
|
||||||
* @throws Error if the dependency is not found.
|
* @throws Error if the dependency is not found.
|
||||||
*/
|
*/
|
||||||
resolve<T>(identifier: string, necessary?: true): T;
|
resolve<T>(identifier: string, necessary?: true): T;
|
||||||
/**
|
/**
|
||||||
* Resolve a non necessary dependency
|
* Resolve a non necessary dependency
|
||||||
* @param identifier The identifier of the dependency
|
* @param identifier The identifier of the dependency
|
||||||
* @param necessary Not necessary, does not throw an error if the dependency is not found.
|
* @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
|
* @returns The resolved dependency or undefined if the dependency is not found
|
||||||
*/
|
*/
|
||||||
resolve<T>(identifier: string, necessary?: false): T | undefined;
|
resolve<T>(identifier: string, necessary?: false): T | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TSInjex Interface
|
* TSInjex Interface
|
||||||
*/
|
*/
|
||||||
export interface ITSInjex extends ITSInjexRegister, ITSInjexResolve {}
|
export interface ITSinjex extends ITSinjexRegister, ITSinjexResolve {}
|
Reference in New Issue
Block a user