From 771b810419849e67ee12ebcdd70888f900c413d0 Mon Sep 17 00:00:00 2001 From: Max P Date: Wed, 14 Aug 2024 23:10:44 +0200 Subject: [PATCH] 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. --- README.md | 22 +++ package.json | 2 +- src/{TSInjex.ts => TSinjex.ts} | 206 ++++++++++---------- src/__tests__/DIContainer.test.ts | 4 +- src/__tests__/IDIContainer.spec.ts | 8 +- src/decorators/Inject.ts | 6 +- src/decorators/Register.ts | 4 +- src/decorators/RegisterInstance.ts | 4 +- src/functions/register.ts | 4 +- src/functions/resolve.ts | 4 +- src/index.ts | 4 +- src/interfaces/Exceptions.ts | 14 +- src/interfaces/{ITSInjex.ts => ITSinjex.ts} | 140 ++++++------- 13 files changed, 222 insertions(+), 200 deletions(-) rename src/{TSInjex.ts => TSinjex.ts} (81%) rename src/interfaces/{ITSInjex.ts => ITSinjex.ts} (88%) diff --git a/README.md b/README.md index e69de29..386bc2f 100644 --- a/README.md +++ b/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/(.*)$': '/src/$1', // Map src to the source folder + '^ts-injex$': '/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` + ], +}; +``` \ No newline at end of file diff --git a/package.json b/package.json index 39c747a..3f8b630 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-injex", - "version": "0.0.5", + "version": "0.0.6", "description": "", "type": "module", "main": "./dist/index.js", diff --git a/src/TSInjex.ts b/src/TSinjex.ts similarity index 81% rename from src/TSInjex.ts rename to src/TSinjex.ts index 50bb795..6cbc1f1 100644 --- a/src/TSInjex.ts +++ b/src/TSinjex.ts @@ -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() -export class TSInjex implements ITSInjex { - private static _instance: TSInjex; - private readonly _dependencies = new Map(); - - /** - * 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( - 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( - identifier: string, - necessary = true, - ): T | undefined { - return (TSInjex.getInstance() as TSInjex).resolve( - identifier, - necessary, - ); - } - - //#endregion - - //#region IDIContainer - - /** - * @inheritdoc - */ - public register( - identifier: string, - dependency: T, - deprecated = false, - ): void { - this._dependencies.set(identifier, { - dependency: dependency, - deprecated: deprecated, - }); - } - - /** - * @inheritdoc - */ - public resolve(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() +export class TSinjex implements ITSinjex { + private static _instance: TSinjex; + private readonly _dependencies = new Map(); + + /** + * 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( + 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( + identifier: string, + necessary = true, + ): T | undefined { + return (TSinjex.getInstance() as TSinjex).resolve( + identifier, + necessary, + ); + } + + //#endregion + + //#region IDIContainer + + /** + * @inheritdoc + */ + public register( + identifier: string, + dependency: T, + deprecated = false, + ): void { + this._dependencies.set(identifier, { + dependency: dependency, + deprecated: deprecated, + }); + } + + /** + * @inheritdoc + */ + public resolve(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 +} diff --git a/src/__tests__/DIContainer.test.ts b/src/__tests__/DIContainer.test.ts index 6d9f7d0..23878f8 100644 --- a/src/__tests__/DIContainer.test.ts +++ b/src/__tests__/DIContainer.test.ts @@ -1,4 +1,4 @@ import { test_IDIContainer } from './IDIContainer.spec'; -import { TSInjex } from '../TSInjex'; +import { TSinjex } from '../TSinjex'; -test_IDIContainer(TSInjex); +test_IDIContainer(TSinjex); diff --git a/src/__tests__/IDIContainer.spec.ts b/src/__tests__/IDIContainer.spec.ts index 4f2eebe..bc5c40f 100644 --- a/src/__tests__/IDIContainer.spec.ts +++ b/src/__tests__/IDIContainer.spec.ts @@ -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(); diff --git a/src/decorators/Inject.ts b/src/decorators/Inject.ts index 9f5a7a7..7022e1c 100644 --- a/src/decorators/Inject.ts +++ b/src/decorators/Inject.ts @@ -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( // 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. diff --git a/src/decorators/Register.ts b/src/decorators/Register.ts index eb29e5f..797bf1c 100644 --- a/src/decorators/Register.ts +++ b/src/decorators/Register.ts @@ -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); diff --git a/src/decorators/RegisterInstance.ts b/src/decorators/RegisterInstance.ts index 37fadbd..ffc72f6 100644 --- a/src/decorators/RegisterInstance.ts +++ b/src/decorators/RegisterInstance.ts @@ -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( diff --git a/src/functions/register.ts b/src/functions/register.ts index afe7b12..00f435c 100644 --- a/src/functions/register.ts +++ b/src/functions/register.ts @@ -1,4 +1,4 @@ -import { TSInjex } from '../TSInjex'; +import { TSinjex } from '../TSinjex'; /** * Register a dependency. @@ -31,5 +31,5 @@ export function register( dependency: T, deprecated?: boolean, ): void { - TSInjex.getInstance().register(identifier, dependency, deprecated); + TSinjex.getInstance().register(identifier, dependency, deprecated); } diff --git a/src/functions/resolve.ts b/src/functions/resolve.ts index 13dbb6a..23d5cf7 100644 --- a/src/functions/resolve.ts +++ b/src/functions/resolve.ts @@ -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( identifier: string, necessary?: boolean, ): T | undefined { - return TSInjex.getInstance().resolve(identifier, necessary); + return TSinjex.getInstance().resolve(identifier, necessary); } diff --git a/src/index.ts b/src/index.ts index b73a416..93dc0a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; diff --git a/src/interfaces/Exceptions.ts b/src/interfaces/Exceptions.ts index e777358..9faa9e3 100644 --- a/src/interfaces/Exceptions.ts +++ b/src/interfaces/Exceptions.ts @@ -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** diff --git a/src/interfaces/ITSInjex.ts b/src/interfaces/ITSinjex.ts similarity index 88% rename from src/interfaces/ITSInjex.ts rename to src/interfaces/ITSinjex.ts index 79588b0..427f47b 100644 --- a/src/interfaces/ITSInjex.ts +++ b/src/interfaces/ITSinjex.ts @@ -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(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(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(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(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(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(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(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(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(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(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(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(identifier: string, necessary?: false): T | undefined; +} + +/** + * TSInjex Interface + */ +export interface ITSinjex extends ITSinjexRegister, ITSinjexResolve {}