2.0.2 • Published 4 years ago

@mildjs/di v2.0.2

Weekly downloads
11
License
MIT
Repository
github
Last release
4 years ago

@MildJS/di

Build Status codecov npm version

A Lightweight Dependency Injection container for Javascript/Typescript.

Features

  • Support value, Class, Factory and AsyncFactory

Installation

$ npm install @mildjs/di reflect-metadata 

Usage

import "reflect-metadata";
import { Injectable, ReflectiveInjector, Inject } from '../../../src';

@Injectable()
export class InjectableClass {
    constructor(@Inject('mock_token') public api: string) { }

}

const asyncFunc = () => new Promise(resolve =>
    setTimeout(() => { resolve("Hello async") }, 100)
);

const injector = ReflectiveInjector.init([
    InjectableClass,
    {
        provide: "mock_token",
        useAsyncFactory: asyncFunc()
    },
]);


async function main() {
    const instance = await injector.get(InjectableClass)
    console.log(instance.api);
}

main();

output

Hello async

Original by https://dev.to/darcyrayner/typescript-dependency-injection-in-200-loc-12j7

2.0.2

4 years ago

2.0.1

4 years ago

1.4.1

4 years ago

2.0.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago