Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tiny-nestjs/auto-injectable

@tiny-nestjs/auto-injectable enables seamless DI within the Nest Framework.
https://github.com/tiny-nestjs/auto-injectable

library nest nestjs nodejs npm tiny-nestjs typescript

Last synced: about 13 hours ago
JSON representation

@tiny-nestjs/auto-injectable enables seamless DI within the Nest Framework.

Awesome Lists containing this project

README

        


tiny-nestjs


npm version
License

## Description

[AutoInjectable](https://github.com/tiny-nestjs/auto-injectable) is a utility library designed to simplify the usage of
dependency injection
in [Nest](https://github.com/nestjs/nest). It enables seamless
handling of automatic injection of dependencies by the framework.
With this library, you can inject dependencies into classes without the need for module definitions.

## Features

- `@ComponentScan()` enables automatic scanning and injection of classes within a module.
- `@AutoInjectable()` allows classes to be automatically injectable for DI.
- `@AutoController()` automatically registers controllers.
- `@AutoAlias()` defines an alias for the @AutoInjectable() class.

## Installation

```bash
npm install @tiny-nestjs/auto-injectable
```

```bash
yarn add @tiny-nestjs/auto-injectable
```

## Usage

**1. `@ComponentScan()` basic usage**

```ts
import { Module } from '@nestjs/common';
import { ComponentScan } from '@tiny-nestjs/auto-injectable';

@ComponentScan()
@Module({
imports: [],
controllers: [],
providers: [],
})
export class AppModule {
}
```

By applying the `@ComponentScan()` decorator to the `AppModule` class, Nest will automatically scan for classes and
inject necessary dependencies.

**2. `@AutoInjectable()`**

```ts
import { AutoInjectable } from '@tiny-nestjs/auto-injectable';

@AutoInjectable()
export class CatService {
// ...
}
```

In this case, by applying the `@AutoInjectable()` decorator to the `CatService` class, the class has become injectable,
allowing it to be injected into other modules without the need for module definitions. (The parameter
of `@AutoInjectable()` is the same as `@Injectable()`)

**3. `@AutoController()` and dependency injection**

```ts
import { AutoController } from '@tiny-nestjs/auto-injectable';

@AutoController()
export class CatController {
constructor(private readonly catService: CatService) {
}

@Get('cats')
getCats() {
return this.catService.findAll();
}
}
```

The class with the `@AutoInjectable()` decorator has been successfully injected and `/cats` api can be accessed by
applying `@AutoController()` on `CatController` service. (The parameter of `@AutoController()` is the same
as `@Controller()`)

| You can see actual [project example](https://github.com/tiny-nestjs/auto-injectable-example) here. |
|----------------------------------------------------------------------------------------------------|


---


- _Below are advanced usage techniques of the library. In most cases, utilizing the methods above will suffice._


**4. `@AutoAlias()`**

```ts
import { AutoAlias } from '@tiny-nestjs/auto-injectable';
import { AutoInjectable } from '@tiny-nestjs/auto-injectable';

@AutoAlias('kitty')
@AutoInjectable()
export class CatService {
// ...
}
```

```ts
import { Inject } from '@nestjs/common';
import { AutoController } from '@tiny-nestjs/auto-injectable';

@AutoController()
export class CatController {
constructor(@Inject('kitty') private readonly catService: CatService) {
}
}
```

`@AutoAlias()` is a decorator used to specify an alias. In the constructor of the `CatService` class, `@Inject('kitty')`
is used to configure the injection of a `CatService` instance with the alias 'kitty'.
As the library is fully compatible with the `Nest` core, you can use `Nest`'s built-in `@Inject()` decorator.

**5. Define DI scope with the `@ComponentScan()`**

```ts
import { Module } from '@nestjs/common';
import { ComponentScan } from '@tiny-nestjs/auto-injectable';

@ComponentScan()
@Module({})
export class AnimalModule {
}
```

The library recommends using `@ComponentScan()` in the AppModule. However, to enable seamless DI within the desired
scope, you can also specify `@ComponentScan()` in other modules.

**6. `@ComponentScan()` parameters**

```bash
# normal case
- /cat
- cat.module.ts
- ...
```

```bash
# special case
- /animal
- /cat
- /module
- cat.module.ts
- /service
- cat.service.ts
```

In most cases, the module is positioned at the top-level directory of its domain. However, in some cases, you can
specify the exact directory path as an array parameter, such as `@ComponentScan(['animal/cat'])`.


## Contribution

To contribute to this library, fork the [GitHub repository](https://github.com/tiny-nestjs/auto-injectable), make your
changes, and create a pull request. Your
contributions are highly appreciated. If you find any improvements or bugs, please open an issue.

## License

`@tiny-nestjs/auto-injectable` is distributed under
the [MIT license](https://github.com/tiny-nestjs/auto-injectable/blob/main/LICENSE).