Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luncher/ttdi
a tiny dependency injection tool for TypeScript and JavaScript
https://github.com/luncher/ttdi
decorators dependency-injection ioc javascript metadata typescript
Last synced: about 1 month ago
JSON representation
a tiny dependency injection tool for TypeScript and JavaScript
- Host: GitHub
- URL: https://github.com/luncher/ttdi
- Owner: Luncher
- License: mit
- Created: 2021-08-14T07:00:27.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-22T16:58:02.000Z (over 3 years ago)
- Last Synced: 2024-04-24T09:22:37.864Z (8 months ago)
- Topics: decorators, dependency-injection, ioc, javascript, metadata, typescript
- Language: TypeScript
- Homepage:
- Size: 88.9 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ttdi
![GitHub](https://img.shields.io/github/license/Luncher/ttdi?style=for-the-badge)
![npm](https://img.shields.io/npm/v/ttdi?style=for-the-badge)
![Travis (.com)](https://img.shields.io/travis/com/Luncher/ttdi?style=for-the-badge)`ttdi` is a [dependency injection](https://en.wikipedia.org/wiki/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](https://github.com/tc39/proposal-decorators) and [metadata](https://github.com/rbuckton/reflect-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:
```javascript
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
```For the decorator of TypeScript, please refer to [decorators](https://www.typescriptlang.org/docs/handbook/decorators.html)
### Install
```shell
yarn add ttdi -S
```or
```shell
npm i ttdi -S
```---
### Usage
#### declare.ts
```typescript
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
```typescript
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.