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

ts-nest

v0.0.9

Published

NodeJS dependency injection inspired by Angular

Downloads

22

Readme

Hierarchical NodeJS dependency injection inspired by Angular

Super lightweight (2kb) library for creating dependency trees in NodeJS applications inpired by Angular. It is build on top of inversify. See this library in action with this stackblitz demo.

Build Status Coverage Status npm version code style: prettier Gitter chat

Installation

Add the package to your project

npm i --save ts-nest
# or
yarn add ts-nest

Import the decorators with

import { NestModule, Component, Injectable, Inject } from 'ts-nest'

Usage

Create a NestModule as application entrypoint and import all the wanted child modules, which are NestModules themselves.

@NestModule({
  imports: [ AuthModule, HttpModule ],
  declarations: [ AppComponent ],
  providers: [ AuthService ],
  exports: [ ]
})
export class AppModule {}

Bootstrap your application by simply contructing it

const app = new AppModule();

There are two other types of objects in this dependency tree, Components and Injectables. Components can be created like follows.

@Component()
export class AppComponent {
  constructor (@Inject(AuthService) authService: AuthService) {}
}

As you may have recognized, the constructor receives an Injected parameter. This parameter can be provided in your NestModule, like it is done in the first listing. The AuthService is decorated by an @Injectable() decorator

@Injectable()
export class AuthService {}

When it is provided, the application knows about it, but does not directly create an instance. It gets instanciated when an @Inject(serviceIdentifier) is used as a constructor parameter in the same or a childs containers class.

The DI system is hierarchical. So the class provided in the parent module will also be available in all its children (with a single object instance).

Cheatsheet

Decorators

| Decorator | Description | Parameters | Return value | | ---------------------------- | ------------------- | ----------------- | -------------------- | | @NestModule() | Creates a container where the classes are stored, imports child-NestModules | config: { imports?: any[], declarations?: any[], providers?: any[], exports?: any[] } | Returns a custom decorator where a container object is created in the constructor | | @Injectable() | Make class bindable to an NestModules container | - | Inversify @injectable() | | @Component() | Make class bindable to an NestModules container | - | Inversify @injectable() | | @Inject(serviceIdentifier) | Let the DI know that a class instance is needed, if not exist, create class | serviceIdentifier | - |

NestModule parameters

| Input parameter | Description | | ---------------------------- | -------------------------------- | | imports | Creates an instance of the imported NestModule(), reads the exports parameter of the instantiated object and stores the exports in its own container. The instance is handled as a child of this module, so Inject()s will work in the child even if the child does not contain the instance itself, but its parent. | | declarations | Binds @Component() decorated classes to the container and after creating all imports it directly creates an instance. | | providers | Binds @Injectable() decorated classes to the container | | exports | Binds @Injectable() or @Component() decorated classes to the parents container |

Dependencies

Get in touch

twitter github stackoverflow

Hi, I am Felix, Angular developer and NgRX contributor

avatar

If you like this library, think about giving it a star or follow me on twitter or github.