Skip to content

Commit

Permalink
docs: add tsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelJCamara committed Jan 14, 2025
1 parent a01a83b commit 7dc8eae
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,6 @@
],
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts"
"types": "dist/index.d.ts",
"packageManager": "yarn@4.5.3+sha512.3003a14012e2987072d244c720506549c1aab73ee728208f1b2580a9fd67b92d61ba6b08fe93f6dce68fd771e3af1e59a0afa28dd242dd0940d73b95fedd4e90"
}
6 changes: 6 additions & 0 deletions src/_shared/injectable.configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,11 @@ export interface IInjectableConfiguration {
*/
token?: InjectionKey;

/**
* Optional factory function to create the injectable.
* Use this factory whenever:
* - you want to have a custom way os creating objects that must be injected
* - the object has in its constructor a parameter that is not registered in the container (ex. integer)
*/
useFactory?: () => any;
}
13 changes: 13 additions & 0 deletions src/_shared/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
/* eslint-disable @typescript-eslint/no-empty-object-type */
/* eslint-disable @typescript-eslint/no-explicit-any */


/**
* A type representing a generic constructor function.
*
* This type can be used to define a class constructor that can accept any number of arguments
* and returns an instance of an object.
*
* @typeParam T - The type of the instance that the constructor creates.
*/
export type GenericConstructor = { new (...args: any[]): {} };

/**
* Represents a key used for dependency injection.
*/
export type InjectionKey = string;
7 changes: 7 additions & 0 deletions src/container/dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import { Lifetime } from "./lifetime";

/**
* Represents a dependency with a value and a lifetime.
*
* @interface IDependency
* @property {any} value - The value of the dependency.
* @property {Lifetime} lifetime - The lifetime of the dependency.
*/
export interface IDependency {
value: any;
lifetime: Lifetime;
Expand Down
21 changes: 21 additions & 0 deletions src/container/di-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import { Lifetime } from "./lifetime";
class DependencyInjectionContainer {
private readonly dependencies = new Map<InjectionKey, IDependency>();

/**
* Registers a dependency in the DI container.
*
* @template T - The type of the instance to register, which extends `GenericConstructor`.
* @param {T} instance - The instance of the dependency to register.
* @param {IInjectableConfiguration} dependencyConfiguration - The configuration for the dependency.
* @throws {Error} If a dependency with the same token is already registered.
* @throws {Error} If a factory is used with a non-singleton lifetime.
*/
register<T extends GenericConstructor>(instance: T, dependencyConfiguration: IInjectableConfiguration) {
const token = dependencyConfiguration.token ?? (new instance() as any).constructor.name;
if (this.dependencies.has(token)) {
Expand Down Expand Up @@ -36,6 +45,14 @@ class DependencyInjectionContainer {
}
}

/**
* Resolves a dependency based on the provided identifier.
*
* @template T - The type of the dependency to resolve.
* @param {InjectionKey | Constructor<T>} dependencyIdentifier - The identifier of the dependency to resolve.
* This can either be a string key or a constructor function.
* @returns {T} - The resolved dependency instance.
*/
resolve<T>(dependencyIdentifier: InjectionKey | Constructor<T>) : T{
return typeof dependencyIdentifier === 'string' ?
this.resolveProperty<T>(dependencyIdentifier) :
Expand Down Expand Up @@ -78,4 +95,8 @@ class DependencyInjectionContainer {

type Constructor<T = any> = new (...args: any[]) => T;

/**
* The `xContainer` is an instance of the `DependencyInjectionContainer` class.
* It serves as the main container for managing dependency injection within the application.
*/
export const xContainer = new DependencyInjectionContainer();
10 changes: 10 additions & 0 deletions src/container/lifetime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@

/**
* Enum representing the lifetime of a service in the dependency injection container.
*
* @enum {number}
* @property {number} Singleton - A single instance of the service is created and shared.
* @property {number} Transient - A new instance of the service is created each time it is requested.
*
* @readonly
*/
export enum Lifetime{
Singleton,
Transient
Expand Down
16 changes: 16 additions & 0 deletions src/decorator/inject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@ import { InjectionKey } from "../_shared/types";
import { xContainer } from "../container/di-container";
import "reflect-metadata";

/**
* Decorator that injects a dependency into a class property or constructor parameter.
*
* @param token - The token to identify the dependency to be injected. If not provided, the type of the property or parameter will be used.
* @returns A function that performs the injection.
*
* @example
* ```typescript
* class MyClass {
* @Inject(MyService)
* private myService: MyService;
*
* constructor(@Inject(OtherService) private otherService: OtherService) {}
* }
* ```
*/
export function Inject(token?: any) {
return function (target: any, propertyKey?: InjectionKey, parameterIndex?: number) {
if (isInjectionInConstructor(parameterIndex))
Expand Down
8 changes: 8 additions & 0 deletions src/decorator/injectable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { GenericConstructor } from "../_shared/types";
import { xContainer } from "../container/di-container";
import { Lifetime } from "../container/lifetime";


/**
* Decorator that marks a class as injectable and registers it with the dependency injection container.
*
* @template T - The type of the class constructor.
* @param {Partial<IInjectableConfiguration>} [injectableConfiguration={}] - Optional configuration for the injectable.
* @returns {Function} - A decorator function that registers the class with the dependency injection container.
*/
export function Injectable<T extends GenericConstructor>(injectableConfiguration: Partial<IInjectableConfiguration> = {}) {
const finalInjectableConfiguration = { ...defaultInjectableConfiguration, ...injectableConfiguration };
return function (constructor: T) {
Expand Down

0 comments on commit 7dc8eae

Please sign in to comment.