npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tiny-nestjs/auto-injectable

v0.3.1

Published

Enables seamless DI within the Nest Framework.

Downloads

14

Readme

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 usages 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

@ComponentScan() defaults to managing dependencies based on the directory path of the module class with the attached decorator, just like in the normal case.

However, you can use a string or string[] to specify which locations the component scan should explore and manage dependencies. Since this string path is a pattern, it will scan all matching paths, so you should provide a unique pattern. For example, in the above instance, instead of simply defining the directory path as 'cat', you should specify it as ' animal/cat' to ensure uniqueness. The directory 'cat' could be used somewhere else, after all, such as @ComponentScan('animal/cat').

By supporting such custom scope definitions, you can apply the library seamlessly to all projects, even handling exceptional cases without exceptions.

For more refined scope specification, you can use @ComponentScan(['animal/cat/module', 'animal/cat/service']).

FYI

  • The scanning scope of @ComponentScan() cannot overlap. If there is an overlap, Nest will throw an error. Please handle it by referring to the error log.

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.