@mildjs/di
v2.0.2
Published
A Lightweight Dependency Injection container for Javascript/Typescript.
Downloads
6
Readme
@MildJS/di
A Lightweight Dependency Injection container for Javascript/Typescript.
Features
- Support
value
,Class
,Factory
andAsyncFactory
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