1.1.3 • Published 6 years ago

justinject v1.1.3

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

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.

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.23

6 years ago

1.0.22

6 years ago

1.0.21

6 years ago

1.0.20

6 years ago

1.0.19

6 years ago

1.0.18

6 years ago

1.0.17

6 years ago

1.0.16

6 years ago

1.0.15

6 years ago

1.0.14

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago