# grumpydi

> A simple DI container

Latest version **1.0.0** (published 2019-12-07) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2019-12-07 |
| First published | 2019-12-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | raichlsimon |
| Keywords | DI |

## Links

- npm: https://www.npmjs.com/package/grumpydi
- npm.io page: https://npm.io/package/grumpydi

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2019-12-07

## README

# GrumpyDI

A simple DI container.

### Installation

```
npm i grumpydi --save
```

### Import

```js
import GrumpyDI from "grumpydi";
```

### How to us

First of all you need to create new container and define object with your objects in it.

```js
import GrumpyDI from "grumpydi";

GrumpyDI({ Example1, Example2 });
```

DI container pass itself in constructor of all objects, so you can easily destructure it.

Let's implement Example1 and Example2

```js
class Example1 {
    onInit ({ Example2 }) {
        console.log(Example2.number * 2);
    }
}

class Example2 {
    constructor (di, number, secondNumber) {
        this.number = number;
        this.secondNumber = secondNumber;
    }
}
```

As you can see we have in **Example1** method **onInit**.
This method will be called when all objects have been registered into DI.
Like in constructor, to **onInit** method is passed DI container.

On top of that we need to pass parameters to constructor in Example2, so let's change our object a little bit.

```js
import GrumpyDI from "grumpydi";

class Example1 {
    onInit ({ Example2 }) {
        console.log((Example2.number + Example2.secondNumber) * 2);
    }
}

class Example2 {
    constructor (di, number, secondNumber) {
        this.number = number;
        this.secondNumber = secondNumber;
    }
}

GrumpyDI({
    Example1,
    Example2: [Example2, 2, 1]
});
```

You can add additional object through registerObject function.

```js
const di = GrumpyDI({ 
    Example1,
    Example2: [Example2, 2, 1]
});

di.registerObject({
    test: function() {}
});
```

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