# @microgamma/digator

> Handcrafted DI container

Latest version **2.1.0** (published 2022-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install @microgamma/digator
pnpm add @microgamma/digator
yarn add @microgamma/digator
bun add @microgamma/digator
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2022-06-17 |
| First published | 2019-01-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 64.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Davide Cavaliere |
| Maintainers | davidecavaliere |
| Keywords | typescirpt, decorator, dependency injection, di container |

## Links

- npm: https://www.npmjs.com/package/@microgamma/digator
- Homepage: https://github.com/davidecavaliere/-microgamma
- npm.io page: https://npm.io/package/@microgamma/digator

## Dependencies (2)

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

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2022-06-17
- 2.0.0 — 2022-05-30
- 1.16.0 — 2020-04-18
- 1.15.3 — 2020-01-23
- 1.15.2 — 2020-01-23
- 1.15.0 — 2020-01-22
- 1.14.6-beta.2 — 2020-01-20
- 1.14.6-beta.0 — 2020-01-20
- 1.14.2 — 2020-01-15
- 1.12.4 — 2020-01-10
- 1.12.3 — 2020-01-08
- 1.11.12 — 2020-01-07
- 1.11.11 — 2019-11-22
- 1.10.23 — 2019-04-05
- 1.10.19 — 2019-03-01
- … 6 more at https://npm.io/package/@microgamma/digator/versions

## README

# @microgamma/digator [![npm version](https://badge.fury.io/js/%40microgamma%2Fdigator.svg)](https://badge.fury.io/js/%40microgamma%2Fdigator)

## install

`npm i -S @microgamma/digator` or `yarn add @microgamma/digator`

## How to use

Decorate a class with `@Injectable()`.

```typescript
// my-class.ts


import { Injectable } from '@microgamma/digator';

@Injectable()
class MyClass {

  public sayHello(name: string) {
    return `Hello ${name}`;
  }
}
```

Inject it using `@Inject(<Class>)`

```typescript
// consumer.ts

import { Inject } from '@microgamma/digator';

class Consumer {

  constructor(
    @Inject(MyClass) public myClassSingleton: MyClass
  ) {
    this.myClassSingleton.sayHello('consumer');
  }

}
```

Getting a singleton programmatically

```typescript
// my-script.ts

const singleton = injector(MyClass);

```

Create the app

```typescript

@DI({
  providers: [
    MyClass,
  ]
})
class App {
}

const app = bootstrap(App);
```

## Providing custom implementation
`providers` array also accept an object such as:

```typescript
@DI({
  providers: [
    {
      provide: MyClass,
      useClass: MyCustomImplementation,
    },
  ]
})
class App {
}

const app = bootstrap(App);
```

## Testing
In order to test a singleton and mock dependencies use `TestBed`

```typescript
import { Inject } from '@microgamma/digator';


describe('MyClass', () => {

  @Injectable()
  class HttpService {
  }

  @Injectable()
  class TestClass {
    constructor(
      @Inject(HttpService) http: HttpService,
    ) {
    }
  }

  let instance;

  beforeEach(() => {
    TestBed.configure({
      providers: [
        TestClass,
        {
          provide: HttpService,
          useClass: class {
            // test implementation
          }
        }
      ]
    });

    instance = TestBed.inject(TestClass);
  });

  it('should exist', () => {
    expect(instance).toBeTruthy();
  });
});

```

## Mocked
Only if jest is used the `Mocked` utility can be used to automatically mock any given class.

```typescript
import { Inject } from '@microgamma/digator';


describe('MyClass', () => {

  @Injectable()
  class HttpService {
  }

  @Injectable()
  class TestClass {
    constructor(
      @Inject(HttpService) http: HttpService,
    ) {
    }
  }

  let instance;

  beforeEach(() => {
    TestBed.configure({
      providers: [
        TestClass,
        {
          provide: HttpService,
          useClass: Mocked(HttpService)
        }
      ]
    });

    instance = TestBed.inject(TestClass);
  });

  it('should exist', () => {
    expect(instance).toBeTruthy();
  });
});
```
for more info look at its [tests](src/lib/testing/mocked.spec.ts)


## DEBUG
Prepend `DEBUG=microgamma:digator*` to your script to see debugging information.

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