1.0.6 • Published 3 years ago

ttdi v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

ttdi

GitHub npm Travis (.com)

ttdi is a dependency injection tool for TypeScript and JavaScript.

features:

  • property based injection
  • constructor based injection
  • support for multiple DI containers
  • auto collection providers(injectable)

Quick Start

Prerequisite

ttdi leverage decorator and metadata, you need to enable emitting decorator metadata in your Typescript config.Add these two lines to your tsconfig.json file under the compilerOptions key:

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

For the decorator of TypeScript, please refer to decorators

Install

yarn add ttdi -S

or

npm i ttdi -S

Usage

declare.ts

import { Inject, Injectable } from 'ttdi'

@Injectable()
export class Bar {
  constructor() {

  }
}

@Injectable()
export class Baz {

}

@Injectable()
export class Foo {
  @Inject
  baz!: Baz
  constructor(public bar: Bar) {}
}

main.ts

import { configure, Container } from 'ttdi'
import { Bar, Baz, Foo } from './declare'

const container = new Container()
await configure(container, __dirname + '/declare.ts')
const foo = container.get(Foo)

expect(foo).toBeInstanceOf(Foo)
expect(foo.bar).toBeInstanceOf(Bar)
expect(foo.baz).toBeInstanceOf(Baz)

API

@Injectable()

The tag class can be injected as a provider.

@Inject()

class property inject.

notice: constructor argument will auto injected.

configure

a async providers(injectable) loader.

container

instance storage container, you can create multi DI containers.

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago