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 🙏

© 2025 – Pkg Stats / Ryan Hefner

di-container-js

v1.0.0

Published

Dependency injection library for js

Readme

Lint Tests

DI container

Dependency injection library for js with zero dependencies

Basic usage

import DiContainer from "di-container-js";
// OR
const DiContainer = require("di-container-js").DiContainer;

let diContainer = new DiContainer();

const componentRef1 = Symbol(),
    componentRef2 = "component2";

class Component2 {
    // define `static dependencies()` method in your class to specify what dependencies are needed
    // otherwise required dependecies will be resolved from constructor arguments
    static dependencies() {
        return [componentRef1];
    }

    constructor(component1) {
        this.component1 = component1;
    }
}

class Component1 {
    constructor() {
    }
}


diContainer.registerClass(componentRef1, Component1);
diContainer.registerClass(componentRef2, Component2);

(async () => {
    // .get method returns promise resolved to dependency instance
    let component2Instance = await diContainer.get(componentRef2);
    // now component2Instance has component1 assigned to it
})();

You can also inject diContainer itself to any component:

constructor(diContainer, ...) {
    this.diContainer = diContainer;
    ...
}

Providing configuration

...

class TestClass {

    constructor() {
    }

    postConstruct(config) {
        // gets called by DI container after instance created
        // can return promise, in this case DI container will
        // wait until promise resolved
        return Promise.resolved();
    }

}

let diContainer = new DiContainer();
// pass configuration as third parameter to .registerClass
diContainer.registerClass("testDependency", TestClass, {configKey: "configValue"});
// OR
// provide config using .configure method
diContainer.configure("testDependency", {configKey: "configValue"});

(async () => {
    let testDependency = await diContainer.get("testDependency");
    // do something
})();

Usage with React

To use dependency injection with React class components you should have a custom ComponentProvider. Unlike default one, it should construct a react component instead of an instance of a class. There is a useful example of inject() decorator function which uses the custom component provider ReactProvider and InjectionHoc high-order component to add dependency injection functionality to React class components. Usage:

import {inject} from '../../di/inject';

class MyComponent extends React.Component {
    
    static dependencies() {
        return [/*...*/];
    }


}

export default inject(MyComponent);

API Reference

DiContainer

register(componentRef, componentProvider)

Register a dependency using the ComponentProvider

| Param | Type | Description | | --- | --- | --- | | componentRef | symbol | string | dependency identifier | | componentProvider | ComponentProvider | provider which is used to construct a dependency instance |

registerClass(componentRef, classRef, [config])

Register a dependency using a class reference

| Param | Type | Description | | --- | --- | --- | | componentRef | symbol | string | dependency identifier | | classRef | function | reference to a class | | [config] | object | configuration object which is passed to the postConstruct method of a class after it's creation |

provide(componentRef, instance)

Provide a component instance

| Param | Type | | --- | --- | | componentRef | symbol | string | | instance | any |

configure(componentRef, config, mergeConfig)

Sets a configuration object which will be passed to 'postConstruct' method of a component

| Param | Type | Default | Description | | --- | --- | --- | --- | | componentRef | symbol | string | | | | config | object | | | | mergeConfig | boolean | true | whenever merge new config to old one |

get(componentRef) ⇒ Promise.<any>

Retrieve a component instance

| Param | Type | | --- | --- | | componentRef | symbol | string |

constructExternal(classRef, config) ⇒ Promise.<(*|undefined)>

Construct an instance of a class without registration in the DI container

| Param | Type | | --- | --- | | classRef | function | | config | object |

constructExternalUsingProvider(provider) ⇒ Promise.<(*|undefined)>

Construct a component without registration in the DI container

| Param | Type | | --- | --- | | provider | ComponentProvider |

createFactory(classRef) ⇒ Promise.<ComponentFactory>

A factory itself is not registered in DI container class and inject dependencies to them.

| Param | Type | | --- | --- | | classRef | function |

isInitialized(componentRef) ⇒ boolean

| Param | Type | | --- | --- | | componentRef | symbol | string |

isProvided(componentRef) ⇒ boolean

| Param | Type | | --- | --- | | componentRef | symbol | string |