0.0.3 • Published 12 months ago

@tauro/tr-injection v0.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

@tauro/tr-injection

A simple dependency injection container for TypeScript using decorators.

Installation

To install the package, use npm:

npm install @tauro/tr-injection

Usage

This dependency injection container allows you to register classes, methods, and properties using decorators and inject them wherever needed in your application.

Registering Classes To register a class, use the @AddClassContainer decorator:

import { container } from '@tauro/tr-injection';

@container.AddClassContainer('myService')
class MyService {
    greet() {
        console.log('Hello from MyService!');
    }
}

or

import { AddClassContainer } from '@tauro/tr-injection';

@AddClassContainer('myService')
class MyService {
    greet() {
        console.log('Hello from MyService!');
    }
}

Registering Methods

To register a method, use the @AddMethodContainer decorator:

import { container } from '@tauro/tr-injection';

class Utils {
    @container.AddMethodContainer('sumNumbers')
    sumNumbers = (a: number, b: number): number => {
        return a + b;
    };
}

// Instantiate the class to register the methods
new Utils();

or

import { AddMethodContainer } from '@tauro/tr-injection';

class Utils {
    @AddMethodContainer('sumNumbers')
    sumNumbers = (a: number, b: number): number => {
        return a + b;
    };
}

// Instantiate the class to register the methods
new Utils();

Registering Properties

To register a property, use the @AddPropertyContainer decorator:

import { container } from '@tauro/tr-injection';

class Config {
    @container.AddPropertyContainer('apiUrl')
    apiUrl = 'https://api.example.com';
}

// Instantiate the class to register the properties
new Config();

or

import { AddPropertyContainer } from '@tauro/tr-injection';

class Config {
    @AddPropertyContainer('apiUrl')
    apiUrl = 'https://api.example.com';
}

// Instantiate the class to register the properties
new Config();

Injecting Dependencies

To inject dependencies, use the @ContainerGet decorator:

import { container } from '@tauro/tr-injection';

class Application {
    @container.ContainerGet('myService')
    myService!: MyService;

    @container.ContainerGet('sumNumbers')
    sumNumbers!: (a: number, b: number) => number;

    @container.ContainerGet('apiUrl')
    apiUrl!: string;

    start() {
        this.myService.greet();  // Should print: "Hello from MyService!"
        console.log(this.sumNumbers(2, 3));  // Should print: 5
        console.log(this.apiUrl);  // Should print: "https://api.example.com"
    }
}

const app = new Application();
app.start();

or

import { ContainerGet } from '@tauro/tr-injection';

class Application {
    @ContainerGet('myService')
    myService!: MyService;

    @ContainerGet('sumNumbers')
    sumNumbers!: (a: number, b: number) => number;

    @ContainerGet('apiUrl')
    apiUrl!: string;

    start() {
        this.myService.greet();  // Should print: "Hello from MyService!"
        console.log(this.sumNumbers(2, 3));  // Should print: 5
        console.log(this.apiUrl);  // Should print: "https://api.example.com"
    }
}

const app = new Application();
app.start();

Automatic Module Loading

To avoid manually importing all your modules, you can use a dynamic module loader in your entry file:

import 'reflect-metadata';
import { container } from '@tauro/tr-injection';
import * as fs from 'fs';
import * as path from 'path';

// Function to dynamically import all files in a directory
const importAll = (dir: string) => {
    fs.readdirSync(dir).forEach(file => {
        const fullPath = path.join(dir, file);
        if (fs.statSync(fullPath).isDirectory()) {
            importAll(fullPath);
        } else if (file.endsWith('.ts')) {
            require(fullPath);
        }
    });
};

// Import all controllers, functions, and constants
importAll(path.join(__dirname, './controllers'));
importAll(path.join(__dirname, './funciones'));
importAll(path.join(__dirname, './constantes'));

License

This project is licensed under the MIT License - see the LICENSE file for details.

Here is the README.md file that provides a basic guide on how to install and use your dependency container, including examples of how to register classes, methods, and properties, as well as how to inject these dependencies into other parts of the application. Additionally, it shows how to dynamically load all modules to avoid manual imports.

@tauro/tr-injection

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago