Socket
Book a DemoInstallSign in
Socket

fl-node-di

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fl-node-di

NodeJS dependency injection inspired by Angular

0.0.6
latest
Source
npmnpm
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Hierarchical NodeJS dependency injection inspired by Angular

See this library in action with this stackblitz demo.

Build Status Coverage Status npm version Gitter chat

Installation

Add the package to your project

npm i --save fl-node-di
# or
yarn add fl-node-di

Import the decorators with

import { FlModule, Component, Injectable, Inject } from 'fl-node-di'

Usage

NodeJS dependency injection module on top of inversify for using backend DI in a way similar to Angulars DI. E.g. you can use the following snippet throughout your complete application

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

The other two decorators are

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

which treats the decorator as an inversify @injectable and directly creates an instance when the parent module gets instanciated. The other one is the @Injectable() decorator

@Injectable()
export class AuthService {}

which returns an inversify @injectable decorator, but does not directly creates an instance but instanciates itself when an @Inject(serviceIdentifier) is used as a constructor parameter in the same or a childs containers class.

The DI system is hierarchical.

Cheatsheet

The decorators:

DecoratorDescriptionParametersReturn value
@FlModule()Creates a container where the classes are stored, imports child-FlModulesconfig: { 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 FlModules container-Inversify @injectable()
@Component()Make class bindable to an FlModules container-Inversify @injectable()
@Inject(serviceIdentifier)Let the DI know that a class instance is needed, if not exist, create classserviceIdentifier-

The @FlModule() parameters:

Input parameterDescription
importsCreates an instance of the imported FlModule(), 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.
declarationsBinds @Component() decorated classes to the container and after creating all imports it directly creates an instance.
providersBinds @Injectable() decorated classes to the container
exportsBinds @Injectable() or @Component() decorated classes to the parents container

Dependencies

Keywords

nodejs

FAQs

Package last updated on 23 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.