@newdash/inject
TypeScript icon, indicating that this package has built-in type declarations

0.2.31 • Public • Published

Dependency Inject Container

npm (scoped) Codecov Netlify Quality Gate Status Security Rating

Yet another dependency injection container for typescript

Quick Start

# install libs
npm i -S reflect-metadata @newdash/inject
// tsconfig.json
// remember enable decorator related flags
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}
// import reflect lib firstly
import "reflect-metadata"; 
import { inject } from "@newdash/inject"

// a really simple example
it('should support deep constructor injection', async () => {

  class A {
    v: number
    constructor(@inject("v") v) {
      this.v = v;
    }
  }

  class B {
    a: InjectWrappedInstance<A>
    constructor(@inject(A) a) {
      this.a = a;
    }
  }

  const ic = InjectContainer.New();
  ic.registerInstance("v", 999); // define an instance provider in simple way
  const b = await ic.getInstance(B);
  expect(b.a.v).toBe(999);

});

CHANGELOG

LICENSE

Dependents (1)

Package Sidebar

Install

npm i @newdash/inject

Weekly Downloads

72

Version

0.2.31

License

MIT

Unpacked Size

205 kB

Total Files

46

Last publish

Collaborators

  • suntao