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,13 +1,13 @@
|
|||||||
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>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,9 +21,9 @@ export class TSInjex implements ITSInjex {
|
|||||||
* 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;
|
||||||
@@ -38,7 +38,7 @@ export class TSInjex implements ITSInjex {
|
|||||||
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,
|
||||||
});
|
});
|
||||||
@@ -52,7 +52,7 @@ export class TSInjex implements ITSInjex {
|
|||||||
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,
|
||||||
);
|
);
|
@@ -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,17 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* 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.
|
||||||
@@ -39,7 +39,7 @@ export interface ITSInjexRegister {
|
|||||||
/**
|
/**
|
||||||
* 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
|
||||||
@@ -67,4 +67,4 @@ export interface ITSInjexResolve {
|
|||||||
/**
|
/**
|
||||||
* TSInjex Interface
|
* TSInjex Interface
|
||||||
*/
|
*/
|
||||||
export interface ITSInjex extends ITSInjexRegister, ITSInjexResolve {}
|
export interface ITSinjex extends ITSinjexRegister, ITSinjexResolve {}
|
Reference in New Issue
Block a user