Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

justinject

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

justinject

Light DI for typescript

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
5
increased by150%
Maintainers
1
Weekly downloads
 
Created
Source

Dependency injection for Typescript

Node module for DI in Typescript

It is lightweight and very easy to use.

To install it type:

npm i justinject -S

Example

@Service()
export class FirstService {
    private time: any;

    constructor() {
        this.time = new Date();
    }

    public method() {
        return this.time;
    }
}

@Service()
export class SecondService {
    constructor(public first: FirstService) { }
    
    public method() {
        return this.first.method();
    }
}

import { Container } from 'justinject';
import { SecondService } from './SecondService';

const second = Container.resolve<SecondService>(SecondService);

console.log(second.method());
// retruns date

To specify Service as singleton add singelton key word in decorator @Service('singleton')

Testing

To mock Service is pretty easy, example:

@Service()
export class FirstServiceMock extends FirstService{
    private time: any;

    constructor() {
        this.time = new Date();
    }

    public method() {
        return `${this.time} mocked`;
    }
}


Container.mock([
            {
                service: FirstService,
                mockWith: FirstServiceMock,
                override: false,
                type: 'default'
            }
        ])

const second = Container.resolve<SecondService>(SecondService);

console.log(second.method());
// retruns date mocked

FAQs

Package last updated on 23 Apr 2018

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc