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

node-dependency-injection

v3.2.2

Published

The NodeDependencyInjection component allows you to standardize and centralize the way objects are constructed in your application.

Downloads

22,816

Readme

Node Dependency Injection

NDI Logo

A special thanks to Symfony which was a great inspiration and example for this project.

The Node Dependency Injection component allows you to standardize and centralize the way objects are constructed in your application.

Npm Version Build Status Publish Status Code Coverage Code Climate Coding Standard Known Vulnerabilities Npm Downloads License

Installation

npm install --save node-dependency-injection

Usage: register and get services

Imagine you have a Mailer class like this:

// services/Mailer.js

export default class Mailer {
  /**
   * @param {ExampleService} exampleService
   */
  constructor(exampleService) {
    this._exampleService = exampleService;
  }

  ...
}

You can register this in the container as a service:

import {ContainerBuilder} from 'node-dependency-injection'
import Mailer from './services/Mailer'
import ExampleService from './services/ExampleService'

let container = new ContainerBuilder()

container
  .register('service.example', ExampleService)

container
  .register('service.mailer', Mailer)
  .addArgument('service.example')

And get services from your container

const mailer = container.get('service.mailer')

Autowire for TypeScript

import {ContainerBuilder, Autowire} from 'node-dependency-injection'

const container = new ContainerBuilder(
  false, 
  '/path/to/src'
)
const autowire = new Autowire(container)
await autowire.process()

or from yaml-json-js configuration

# /path/to/services.yml
services:
  _defaults:
    autowire: true
    rootDir:  "/path/to/src"

You can also get a service from a class definition

import SomeService from '@src/service/SomeService'

container.get(SomeService)

If you are transpiling your Typescript may you need to dump the some kind of service configuration file.

import {ContainerBuilder, Autowire, ServiceFile} from 'node-dependency-injection'

const container = new ContainerBuilder(
  false, 
  '/path/to/src'
)
const autowire = new Autowire(container)
autowire.serviceFile = new ServiceFile('/some/path/to/dist/services.yaml')
await autowire.process()

My proposal for load configuration file in a production environment with transpiling/babel compilation:

if (process.env.NODE_ENV === 'dev') {
  this._container = new ContainerBuilder(false, '/src');
  this._autowire = new Autowire(this._container);
  this._autowire.serviceFile = new ServiceFile('/some/path/to/dist/services.yaml');
  await this._autowire.process();
} else {
  this._container = new ContainerBuilder(false, '/dist');
  this._loader = new YamlFileLoader(this._container);
  await this._loader.load('/some/path/to/dist/services.yaml');
}
await this._container.compile();

Configuration files: how to load and use configuration files

You can also use configuration files to improve your service configuration

# /path/to/file.yml
services:
  service.example:
    class: 'services/ExampleService'

  service.mailer:
    class: 'services/Mailer'
    arguments: ['@service.example']
import {ContainerBuilder, YamlFileLoader} from 'node-dependency-injection'

let container = new ContainerBuilder()
let loader = new YamlFileLoader(container)
await loader.load('/path/to/file.yml')

And get services from your container easily

...
const mailer = container.get('service.mailer')

List of features

  • Autowire for TypeScript
  • Configuration files with JS, YAML or JSON.
  • Multiple configuration files
  • Custom relative service directory
  • Compiling container
    • Custom compiler pass
    • Change definition behaviour
  • Using a factory to create services
  • Nullable Dependencies
  • Public or private services
  • Service Aliasing
  • Service Tagging
  • Parameters Injection
  • Lazy Services
  • Deprecate Services
  • Decorate Services
  • Synthetic Services
  • Non Shared Services
  • Parent and Abstract Services
  • Custom Logger
  • Container as Service

Please read full documentation

ExpressJS Usage

If you are using expressJS and you like Node Dependency Injection Framework then I strongly recommend you to use the node-dependency-injection-express-middleware package. That gives you the possibility to retrieve the container from the request.

npm install --save node-dependency-injection-express-middleware
import NDIMiddleware from 'node-dependency-injection-express-middleware'
import express from 'express'

const app = express()

const options = {serviceFilePath: 'some/path/to/config.yml'}
app.use(new NDIMiddleware(options).middleware())

Express Middleware Documentation

TypeScript Usage

If you are using typescript and you like Node Dependency Injection Framework then typing are now provided at node-dependency-injection so you do not have to create custom typing anymore.

npm install --save node-dependency-injection
import { ContainerBuilder } from 'node-dependency-injection'
import MongoClient from './services/MongoClient'
import { Env } from './EnvType'

export async function boot(container = new ContainerBuilder(), env: Env) {
    container.register('Service.MongoClient', MongoClient).addArgument({
        host: env.HOST,
        port: env.PORT,
    })
}

Resources