tiny-nestjs / auto-injectable

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

Home Page:https://www.npmjs.com/package/@tiny-nestjs/auto-injectable

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tiny-nestjs

npm version License

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() 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

npm install @tiny-nestjs/auto-injectable
yarn add @tiny-nestjs/auto-injectable

Usage

1. @ComponentScan() basic usage

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. (The parameter of @AutoInjectable() is the same as @Injectable())

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. (The parameter of @AutoController() is the same as @Controller())

You can see actual project example here.



  • Below are advanced usage techniques of the library. In most cases, utilizing the methods above will suffice.

4. @AutoAlias()

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

@AutoAlias('kitty')
@AutoInjectable()
export class CatService {
  // ...
}
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()

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

# normal case
- /cat
  - cat.module.ts
  - ...
# 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, 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.

About

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

https://www.npmjs.com/package/@tiny-nestjs/auto-injectable

License:MIT License


Languages

Language:TypeScript 88.8%Language:JavaScript 10.0%Language:Shell 1.1%