Compare commits

..

3 Commits

Author SHA1 Message Date
f653983140 docs: reflect changes to changelog 2025-04-02 22:00:16 +02:00
3441bb21b0 docs: Push version to 1.1.0 2025-04-02 22:00:10 +02:00
1841ab2f92 feat(api): expose inject() function via index.ts 2025-04-02 21:58:26 +02:00
3 changed files with 18 additions and 133 deletions

View File

@@ -17,13 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security ### Security
## [1.2.0]
### Added
- docs: added complete Typedoc-style documentation for all `inject()` overloads
Includes parameter descriptions, usage examples, and detailed error annotations for each variant.
## [1.1.0] ## [1.1.0]
### Added ### Added
@@ -147,7 +140,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
--- ---
[unreleased]: https://github.com/20Max01/TSinjex/compare/v1.0.0...HEAD [unreleased]: https://github.com/20Max01/TSinjex/compare/v1.0.0...HEAD
[1.1.0]: https://github.com/20Max01/TSinjex/compare/v1.1.0...v1.2.0
[1.1.0]: https://github.com/20Max01/TSinjex/compare/v1.0.0...v1.1.0 [1.1.0]: https://github.com/20Max01/TSinjex/compare/v1.0.0...v1.1.0
[1.0.0]: https://github.com/20Max01/TSinjex/compare/v0.4.0...v1.0.0 [1.0.0]: https://github.com/20Max01/TSinjex/compare/v0.4.0...v1.0.0
[0.4.0]: https://github.com/20Max01/TSinjex/compare/v0.3.0...v0.4.0 [0.4.0]: https://github.com/20Max01/TSinjex/compare/v0.3.0...v0.4.0

View File

@@ -1,6 +1,6 @@
{ {
"name": "ts-injex", "name": "ts-injex",
"version": "1.2.0", "version": "1.1.0",
"description": "Simple boilerplate code free dependency injection system for TypeScript.", "description": "Simple boilerplate code free dependency injection system for TypeScript.",
"type": "module", "type": "module",
"main": "./dist/index.js", "main": "./dist/index.js",

View File

@@ -8,142 +8,35 @@ import {
import { Identifier } from '../types/Identifier.js'; import { Identifier } from '../types/Identifier.js';
import { InitDelegate } from '../types/InitDelegate.js'; import { InitDelegate } from '../types/InitDelegate.js';
/**
* Resolves a dependency by its identifier without initialization or instantiation.
* @template T The expected type of the dependency.
* @param identifier The identifier used to resolve the dependency from the container.
* @returns The resolved dependency.
* @throws A {@link DependencyResolutionError} if the dependency is not found.
* @example
* ```ts
* const logger = inject<Logger>('Logger');
* ```
*/
export function inject<T>(identifier: Identifier): T;
/**
* Resolves and instantiates a dependency using its constructor.
* @template T The expected class type of the dependency.
* @param identifier The identifier used to resolve the dependency from the container.
* @param shouldInit Set to `true` to instantiate the dependency after resolution.
* @returns The resolved and instantiated dependency.
* @throws A {@link DependencyResolutionError} if the dependency is not found.
* @throws A {@link NoInstantiationMethodError} if the dependency has no constructor.
* @throws An {@link InitializationError} if instantiation fails.
* @example
* ```ts
* const instance = inject<Service>('Service', true);
* ```
*/
export function inject<T>(identifier: Identifier, shouldInit: true): T;
/**
* Resolves and instantiates a dependency using its constructor, optionally failing silently.
* @template T The expected class type of the dependency.
* @param identifier The identifier used to resolve the dependency from the container.
* @param shouldInit Set to `true` to instantiate the dependency.
* @param isNecessary If `false`, resolution or instantiation errors return `undefined` instead of throwing.
* @returns The resolved and instantiated dependency, or `undefined` if resolution or instantiation fails.
* @example
* ```ts
* const instance = inject<Service>('OptionalService', true, false);
* if (instance) instance.doSomething();
* ```
*/
export function inject<T>(
identifier: Identifier,
shouldInit: true,
isNecessary: false,
): T | undefined;
/**
* Resolves a dependency without instantiating it, optionally failing silently.
* @template T The expected type of the dependency.
* @param identifier The identifier used to resolve the dependency from the container.
* @param shouldInit Set to `false` to skip instantiation.
* @param isNecessary If `false`, resolution errors return `undefined` instead of throwing.
* @returns The resolved dependency, or `undefined` if not found.
* @example
* ```ts
* const config = inject<Config>('Config', false, false) ?? getDefaultConfig();
* ```
*/
export function inject<T>(
identifier: Identifier,
shouldInit: false,
isNecessary: false,
): T | undefined;
/**
* Resolves a dependency and applies a custom initializer function to transform the result.
* @template T The original dependency type.
* @template U The final return type after initialization.
* @param identifier The identifier used to resolve the dependency.
* @param init A function to transform or initialize the dependency.
* @returns The transformed dependency.
* @throws A {@link DependencyResolutionError} if the dependency is not found.
* @throws An {@link InitializationError} if the initializer throws.
* @example
* ```ts
* const client = inject<Api>('Api', (api) => api.getClient());
* ```
*/
export function inject<T, U>(
identifier: Identifier,
init: InitDelegate<T, U>,
): U;
/**
* Resolves a dependency and applies a custom initializer function, optionally failing silently.
* @template T The original dependency type.
* @template U The final return type after initialization.
* @param identifier The identifier used to resolve the dependency.
* @param init A function to transform or initialize the dependency.
* @param isNecessary If `false`, resolution or initializer errors return `undefined` instead of throwing.
* @returns The transformed dependency, or `undefined` if resolution or initialization fails.
* @example
* ```ts
* const db = inject<Database, Pool>('Database', (d) => d.getPool(), false);
* if (db) db.query('SELECT * FROM users');
* ```
*/
export function inject<T, U>(
identifier: Identifier,
init: InitDelegate<T, U>,
isNecessary: false,
): U | undefined;
/** /**
* A function to inject a dependency from a DI (Dependency Injection) container into a variable. * A function to inject a dependency from a DI (Dependency Injection) container into a variable.
* This is the actual implementation that handles all overload variants. * @template T The type of the dependency to be injected.
* @template T The original dependency type. * @template U The type of the property to be injected.
* @template U The final return type after optional initialization or transformation. * @param identifier The identifier used to resolve the class in the DI container.
* @param identifier The identifier used to resolve the dependency.
* @see {@link Identifier} for more information on identifiers. * @see {@link Identifier} for more information on identifiers.
* @param init Optional: either `true` to instantiate via constructor, `false` to skip, or a function to transform the dependency. * @param init Optional an initializer function to transform the dependency before injection
* or true to instantiate the dependency if it has a constructor.
* @see {@link InitDelegate} for more information on initializer functions. * @see {@link InitDelegate} for more information on initializer functions.
* @param isNecessary If `true`, throws on failure; if `false`, returns `undefined` on resolution or initialization errors. * @param isNecessary If true, throws an error if the dependency is not found.
* @returns The resolved dependency or result of initialization, or `undefined` if not necessary and resolution fails. * @returns The resolved dependency or undefined if the dependency is not necessary
* @throws A {@link DependencyResolutionError} if the dependency is not found (and necessary). * and not found, or throws an error if the dependency is necessary and not found.
* @throws A {@link NoInstantiationMethodError} if instantiation is requested but no constructor exists. * @throws **Only throws errors if the dependency is necessary.**
* @throws An {@link InitializationError} if the initializer throws an error. * @throws A {@link DependencyResolutionError} if the dependency is not found.
* @throws A {@link InjectorError} for unknown errors during resolution or transformation. * @throws A {@link InjectorError} if an error occurs during the injection process.
* @throws A {@link NoInstantiationMethodError} if the dependency does not have a constructor.
* @throws An {@link InitializationError} if an error occurs during the initialization process.
* @example * @example
* ```ts * ```ts
* const service = inject<Service>('Service'); * let myDependency = inject<MyDependency>('MyDependencyIdentifier');
* ``` * ```
* @example * @example
* ```ts * ```ts
* const instance = inject<Service>('Service', true); * let logger = inject<ILogger>('ILogger_', (x: ILogger_) => x.getLogger('Tags'), false);
* ```
* @example
* ```ts
* const logger = inject<ILogger>('ILogger_', (x) => x.getLogger('Module'), false);
* ``` * ```
*/ */
export function inject<T, U>( export function inject<T, U>(
identifier: Identifier, identifier: Identifier,
init?: InitDelegate<T, U> | true | false, init?: InitDelegate<T, U> | true,
isNecessary = true, isNecessary = true,
): T | U | undefined { ): T | U | undefined {
let instance: T | U | undefined; let instance: T | U | undefined;
@@ -163,7 +56,7 @@ export function inject<T, U>(
? (): U => new dependency() as U ? (): U => new dependency() as U
: undefined; : undefined;
if (init == null || init === false) instance = dependency; if (init == null) instance = dependency;
else if (initFunction != null) else if (initFunction != null)
instance = tryAndCatch( instance = tryAndCatch(
initFunction, initFunction,