Description
AutoInjectable is a utility library designed to simplify the usage of dependency injection
in 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() decorator enables automatic scanning and injection of classes within a module.
@AutoInjectable() decorator allows classes to be automatically injectable for DI.
@AutoController() decorator automatically registers controllers.
Installation
npm install @tiny-nestjs/auto-injectable
Usage
1. @ComponentScan()
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()
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.
3. @AutoController() and dependency injection
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.
Contribution
To contribute to this library, fork the GitHub repository, 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.