@mildjs/di
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

@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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
2.0.21latest

Version History

VersionDownloads (Last 7 Days)Published
2.0.21
2.0.11
2.0.01
1.4.11
1.3.01
1.2.11
1.2.01
1.1.01
1.0.01

Package Sidebar

Install

npm i @mildjs/di

Weekly Downloads

9

Version

2.0.2

License

MIT

Unpacked Size

46.1 kB

Total Files

45

Last publish

Collaborators

  • mildronize