# dicoi

> DiCoI - a Dependency Injection / Inversion of Control Container example in typescript

Latest version **1.0.3** (published 2020-11-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install dicoi
pnpm add dicoi
yarn add dicoi
bun add dicoi
```

## 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 | 1.0.3 |
| Published | 2020-11-12 |
| First published | 2020-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 19.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Issa Shabo |
| Maintainers | ishabo |

## Links

- npm: https://www.npmjs.com/package/dicoi
- Repository: https://github.com/ishabo/dicoi
- Homepage: https://github.com/ishabo/dicoi#readme
- Issues: https://github.com/ishabo/dicoi/issues
- npm.io page: https://npm.io/package/dicoi

## Dependencies (1)

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

## Recent versions

- 1.0.3 (latest) — 2020-11-12
- 1.0.1 — 2020-04-13
- 1.0.0 — 2020-04-13

## README

# DiCoI - DI/IoC Container

DiCoI is a simple Dependency injection / Inversion of Control Container example in Typescript that enables developers handle automatic dependency injection.

## Installation

Using npm:

```bash
$ npm install dicoi
```

Using yarn:

```bash
$ yarn add dicoi
```

## Example

Lets suppose a `RatesService` that connects to an API to get currency rates and logs results. It depends on ApiService and Logger. `ApiService` also depends on `Logger` and also requires a param. To automatically initialize RatesService with those dependencies, we'd like to be able to do:

```typescript
import { Container } from 'dicoi';

const container = new Container();
const rates = container
  .inject({ ref: Logger, source: Logger, type: 'class' })
  .and({
    ref: BASE_URL,
    source: 'https://api.exchangeratesapi.io',
    type: 'param'
  })
  .and({ ref: ApiService, source: ApiService, type: 'class' })
  .into(RatesService);

rates.getRate('USD', ['GBP', 'EUR']).then(console.log);
```

DiCoI gives us a `Container` that will handle those dependenies for us. It has declarative methods that enable us to `inject(DEPENDENCY_PARAM_OBJET).and(DEPENDENCY_CLASS_OBJET).into(MAIN_CLASS)`

For this to work, you'll need to decorate `RatesService` and `ApiService` with `@Injectable` decorator, and any dependency (non-class) param must be decorated with `@Inject` decorator.

```typescript
import { Container, Injectable, Inject, InjectableRef } from 'dicoi';

const BASE_URL = new InjectableRef('BASE_URL');

export class Logger {
  public log(...args: any[]) {
    console.log('[log]', ...args);
  }
}

@Injectable()
class ApiService {
  constructor(
    @Inject(BASE_URL) private baseUrl: string,
    private logger: Logger
  ) {
    this.logger.log('ApiService initialized');
  }

  async getData(endpoint: string) {
    const response = await fetch(`${this.baseUrl}/${endpoint}`);
    return await response.json();
  }
}

@Injectable()
class RatesService {
  constructor(
    private readonly api: ApiService,
    private readonly logger: Logger
  ) {
    this.logger.log('Launching rates service');
  }
  async getRate(base: string, symbols: string[]) {
    return await this.api.getData(
      `latest?base=${base}&symbols=${symbols.join(',')}`
    );
  }
}
```

> **NOTE:** that `Logger` class does not need any decorator, because we do not need to inject anything into it.

You can view this working example in [this demo](https://codesandbox.io/s/nifty-gauss-l4lor)

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