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

fast-inject

v1.1.2

Published

A lightweight dependency injection container for Node.js.

Downloads

6

Readme

fast-inject

Build Status Coverage Status

A lightweight dependency injection container for Node.js.

Node.js Support

This library currently supports Node.js >= 8.0.0.

Why dependency injection in Node.js?

To be honest dependency injection isn't really necessary in Node.js most of the time due to its module-based design but I created this library because I wanted the following.

  • A lightweight container that I can instantiate on every HTTP request
  • Lazy-loaded services that only get instantiated if they're used
  • The ability to decouple modules for testing without needing proxyquire
  • Eliminate relative paths to other modules

Installation

This library is intended for use in Node.js projects and can be installed with npm.

npm install fast-inject --save

Creating a basic container

Containers are merely just an object with properties for constants or services. These constants or services are provided as singletons. Services are also lazy-loaded and thus only get instantiated if they are accessed.

To setup a container you just create an Injector instance and register your constants and/or services.

const { Injector } = require('fast-inject');

// Service class
class B {
  // Expects an instance of `A` to be injected.
  constructor(a) {
    this.a = a;
  }
}

// Service class
class C {
  // Expects an instance of `B` to be injected.
  constructor(b) {
    this.b = b;
  }
}

const { container } = new Injector()
  // Constants can provide any static value (string, number, Object, Function). 
  .constant('A', 1234)
  // Services must be defined as ES6 classes and can have dependencies.
  // Any dependencies are injected into the constructor when the service is instantiated.
  .service(B, ['A'])
  // Use the `name` property of the class when one service is dependent on another.
  .service(C, [B.name]);
  
// Access a constant
console.log(container.A);
// 1234

// Access a service
console.log(container.B.a);
// 1234

// Access a nested service
console.log(container.C.b.a);
// 1234

Creating a container pipeline

In addition to using the approach above, fast-inject also allows you to build a container pipeline by means of functional composition. Ramda is used in the example below but you can use any library that supports function composition like Lodash or Underscore.js.

const R = require('ramda');
const { constant, service } = require('fast-inject');

// Service class
class B {
  // Expects an instance of `A` to be injected.
  constructor(a) {
    this.a = a;
  }
}

// Service class
class C {
  // Expects an instance of `B` to be injected.
  constructor(b) {
    this.b = b;
  }
}

// Compose the pipeline
const pipeline = R.pipe(
  constant('A', 1234),
  service(B, ['A']),
  service(C, [B.name])
);

// Create a container
const container = pipeline(Object.create(null));

The functional approach also gives you the advantage of being able to compose multiple pipelines together.

API

Classes

Functions

Injector

Kind: global class
Properties

| Name | Type | Description | | --- | --- | --- | | container | Object | The container instance. |

new Injector()

The class for constructing a dependency container.

injector.constant(name, value) ⇒ Injector

Define a constant on the injector's container.

Kind: instance method of Injector
Returns: Injector - The Injector instance.

| Param | Type | | --- | --- | | name | string | | value | * |

injector.service(Class, dependencies) ⇒ Injector

Define a service on the injector's container.

Kind: instance method of Injector
Returns: Injector - The Injector instance.

| Param | Type | | --- | --- | | Class | function | | dependencies | Array.<string> |

constant(name, value, container) ⇒ Object

Define a constant on a container.

Kind: global function
Returns: Object - The container instance.

| Param | Type | | --- | --- | | name | string | | value | * | | container | Object |

service(Class, dependencies, container) ⇒ Object

Define a service on a container.

Kind: global function
Returns: Object - The container instance.

| Param | Type | | --- | --- | | Class | function | | dependencies | Array.<string> | | container | Object |