# @decorators/di

> node decorators - decorators for dependency injection

Latest version **3.1.0** (published 2023-08-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install @decorators/di
pnpm add @decorators/di
yarn add @decorators/di
bun add @decorators/di
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2023-08-09 |
| First published | 2017-11-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 57 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 379 |
| Author | Serhii Sol |
| Maintainers | decorators |
| Keywords | nodejs, dependency injection, node decorators, typescript |

## Links

- npm: https://www.npmjs.com/package/@decorators/di
- Repository: https://github.com/serhiisol/node-decorators
- Homepage: https://github.com/serhiisol/node-decorators#readme
- Issues: https://github.com/serhiisol/node-decorators/issues
- npm.io page: https://npm.io/package/@decorators/di

## Dependencies (1)

- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 3.1.0 (latest) — 2023-08-09
- 3.0.1 — 2023-08-03
- 3.0.0 — 2023-07-28
- 2.0.0 — 2023-01-30
- 1.0.3 — 2021-04-08
- 1.0.2 — 2018-03-01
- 1.0.1 — 2017-11-19
- 1.0.0 — 2017-11-01

## README

![Node Decorators](https://github.com/serhiisol/node-decorators/blob/master/decorators.png?raw=true)

## Installation
```
npm install @decorators/di --save
```

## Example
Fully working example can be found in [example](example) folder.

## API
* `@Injectable()` - Registers class as provider in the container
```typescript
@Injectable()
class HttpService {}
```

* `@Inject(injectable: Injectable)` - A parameter decorator that marks parameter as dependency.
```typescript
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
```

* `@Optional()` - A parameter decorator that marks parameter as optional dependency.
```typescript
@Injectable()
class HttpService {
  constructor(@Optional() @Inject(API_URL) apiUrl: string) {}
}
```

* `InjectionToken` - Creates a token that can be used in DI as Provider.
```typescript
const API_URL = new InjectionToken('API_URL');
...
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
```

* `Container` - Container interface
  * `.setParent(container: Container)` - set parent container
  * `.provide(providers: Providers[])` - Registers an array of providers.
  * `.get<T>(injectable: Injectable): Promise<T>` - Retrieves a Promise with an instance of the injectable, throws:
    * `MissingProviderError` if dependency provider wasn't found
    * `RecursiveProviderError` in case of recursive dependency injection
```typescript
const container = new Container();

container.provide([
  {
    provide: 'Message',
    async useFactory() {
      return delay('Async Provider');
    },
  },
  {
    provide: 'Hello World',
    useClass: Service,
  },
]);
```

## Multi-support
It's possible to provide multiple things using one injection token via `multi` flag:
```typescript
container.provide([
  {
    provide: GLOBAL_PIPE,
    useClass: ServerPipe,
    multi: true
  },
  {
    provide: GLOBAL_PIPE,
    useClass: ErrorPipe,
    multi: true
  },
]);
```

---
_Source: https://npm.io/package/@decorators/di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
