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.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Build Status Coverage Status

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

And it is necessary to enable:

"experimentalDecorators": true "emitDecoratorMetadata": true

in tsconfig.json file

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

HemeraJs Support

Hemera is node microservices framework. You can find more details here: https://github.com/hemerajs/hemera

Justinject has support for hemera actions. You can declare new action like this:

@Service()
export class ActionService {
    constructor(public hemera: HemeraService, public validate: ValidateService) { }

    @Action({
        topic: 'new.topic',
        cmd: 'gogo'
    }, { additionalPattern: 'dothat', newAdditionalPattern: 'dothis' })
    public action(msg: any, done: any) {
        console.log('Action message', msg.data);
        done(null, 'Hemera action called!');
    }
}

ActionService must have 2 properties hemera and validator.

HemeraService must have instance getter which returns Hemera instance.

ValidateService must have schema getter which returns validation.

FAQs

Package last updated on 27 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