@all-in-js/injector
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

npm Build Status Coverage Status

a Javascript & Nodejs dependencies injector

Usage

  • Container class
import { ContainerClass } from '@all-in-js/injector';

const initDeps = {
  key: function key() {}
};
const container = new ContainerClass(initDeps);

container.add(class TestService{});

container.resolve(['TestService', 'key'], function(testService, key) {});
// or
const [testService] = container.resolve('TestService');

// this `container.resolve` support various types, eg:
// `container.resolve(string[array<string>[function]])` 
// if argument just is a function, it's arguments would be parsed to be an array to be resolved.
  • Decorator
import { Injectable } from '@all-in-js/injector';

@Injectable
class TestService {}
  • inject to constructor
import { Inject } from '@all-in-js/injector';

@Inject('TestService')
class TestControler {
  constructor(testService) {
    this.testService = testService;
  }
}
  • inject to class's function prop
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService')
  test(testService) {
    // ...
  }
}
  • inject to class's value prop
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService') testService;
  toString() {
    // this.testService
  }
}
  • when inject to class's function prop, merge injected value and your arguments.
import { Inject } from '@all-in-js/injector';

class TestControler {
  @Inject('TestService')
  test(testService, yourArguments) {
    // ...
  }
}

new TestControler().test('yourArguments');

Package Sidebar

Install

npm i @all-in-js/injector

Weekly Downloads

2

Version

1.0.3

License

MIT

Unpacked Size

30.6 kB

Total Files

16

Last publish

Collaborators

  • eryue