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

di-node

v0.2.2

Published

Dependency injection for node js

Downloads

4

Readme

DI Node Build Status

Support

  • Dependency injection for nodejs >= v4.0.0
  • Path aliases
  • Dynamic module definitions
  • Module mocking for easier testing

Usage of older version presentation on MNUG bounded to MVCJS.

Example of CMS app on older version.

Usage

Dependency injection provides you ioc and great testing features. Module returns single instance of dependency injection.

While using di-node, do not load anything via require, except di-node otherwise you will not be able to mock that object or provide custom implementation of that object when it's needed.

CREATE FILE ./di.js

VERY IMPORTANT is to pass local require resolver to correctly resolve dependencies

let DI = require('di-node');
let di = new DI(require);
/// do something here
di.setAlias('mypath', __dirname + '/mypath');
module.exports = di;

Example module.js

let di = require('./di');
let Type = di.load('typed-js');
let fs = di.load('fs');

class D extends Type {
    constructor(name) {
        super(
            {
                name: Type.STRING
            }
        );
        this.name = name;
    }
}

module.exports = new D('Igor');

Test of class D

it('mock', () => {
    class A {
        constructor(config) {
            expect(config).toEqual({name: 'str'});
        }
    }
    A.STRING = 'str';
    di.setAlias('b', __dirname + '/b');
    let val = di.load('@{b}/di-test');
    expect(val instanceof Type).toBe(true);
    let valMock = di.mock('@{b}/di-test', {
        'typed-js': A
    });
    expect(valMock instanceof A).toBe(true);
});

DI Methods

di.load(key)

While using di-node do not load anything via require except di-node otherwise you will not be able to mock that object or provide custom implementation of that object when it's needed.

di.load Valid

'use strict';

let di = require('./di');
let Type = di.load('typed-js');
let fs = di.load('fs');

class D extends Type {
    constructor(name) {
        super(
            {
                name: Type.STRING
            }
        );
        this.name = name;
    }
}

di.load Invalid

Because you will not be able to mock fs when it's needed

'use strict';

let di = require('./di');
let Type = di.load('typed-js');
let fs = require('fs');

class D extends Type {
    constructor(name) {
        super(
            {
                name: Type.STRING
            }
        );
        this.name = name;
    }
}

di.mock(module, modules)

Mock is used for testing purposes {string} module {Object} definition of modules

it('mock', () => {
    class A {
        constructor(config) {
            expect(config).toEqual({name: 'str'});
        }
    }
    A.STRING = 'str';
    di.setAlias('b', __dirname + '/b');
    let val = di.load('@{b}/di-test');
    expect(val instanceof Type).toBe(true);
    let valMock = di.mock('@{b}/di-test', {
        'typed-js': A
    });
    expect(valMock instanceof A).toBe(true);
});

di.setAlias(key, value)

Sets specific path alias which can be used in load method {string} key {string} value

// set aliases
di.setAlias('app', __dirname + '/app');
di.setAlias('components', '@{app}/components');
// usage
di.load('@{app}/module');
di.load('@{components}/redis');

di.getAlias(key)

Get specific alias {string} key return {string}

di.setAlias('app', __dirname + '/app');
di.getAlias('app'); // returns __dirname/app

di.hasAlias(key)

Check if alias is setted return {boolean}

di.setAlias('app', __dirname + '/app');
di.hasAlias('app'); // true

di.normalize(key)

Normalize path return {string}

di.setAlias('app', '/app');
di.setAlias('components', '@{app}/components');
di.normalize('@{components}/path'); //  returns /app/components/path

di.setModule(key, value)

Set specific module {string} key {Object|Any} value

// My fs implementation
di.setModule('fs', {
    readFileSync: {
    
    }
});
di.setModule('app/info', '@{app}/core/info');
// usage
di.load('fs'); // will load custom implementation
di.load('app/info'); // will load appPath/core/info.js

di.getModule(key)

Get specific module {string} key return {Object|Any} value

di.setModule('fs', {
    readFileSync: {
    
    }
});
di.getModule('fs'); // returns custom fs module

di.hasModule(key)

Check if module is registered in di return {boolean}

di.setModule('custom', {
    readFileSync: {
    
    }
});
di.hasModule('custom'); // true