# midge

> Mini Injector

Latest version **0.2.0** (published 2016-11-25) · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2016-11-25 |
| First published | 2016-07-19 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Christian Blaschke |
| Maintainers | platdesign |
| Keywords | di, dependency injection, injector, inject, injection, nodejs, javascript, ilovejs |

## Links

- npm: https://www.npmjs.com/package/midge
- Repository: https://github.com/platdesign/midge
- Issues: https://github.com/platdesign/midge/issues
- npm.io page: https://npm.io/package/midge

## 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

- 0.2.0 (latest) — 2016-11-25
- 0.1.1 — 2016-07-24
- 0.1.0 — 2016-07-19

## README

# midge

Mini Injector

[![Build Status](https://travis-ci.org/platdesign/midge.svg?branch=master)](https://travis-ci.org/platdesign/midge)

# Install

`npm install --save midge`

# Usage

```javascript
const Midge = require('midge');

const cache = {
	a: 123,
	b: 321
};

const factories = {
	c: function(a, b) {
		return a + b;
	},
	d: function(b, c) {
		return b + c;
	}
};

// Create injector instance
const injector = Midge(cache, function factory(name) {
	return injector.invoke(factories[name]);
});


// Get results
let a = injector.get('a');
let b = injector.get('b');
let c = injector.get('c');
let d = injector.get('d');

expect(a).to.equal(123);
expect(b).to.equal(321);
expect(c).to.equal(a + b);
expect(d).to.equal(b + c);


// Injecting into functions
injector.invoke(function(a,b,c,d) {
	expect(a).to.equal(123);
	expect(b).to.equal(321);
	expect(c).to.equal(a + b);
	expect(d).to.equal(b + c);
});


// Injecting into constructors
injector.instantiate(function(a,b,c,d) {
	expect(this).to.be.an.object();
	expect(a).to.equal(123);
	expect(b).to.equal(321);
	expect(c).to.equal(a + b);
	expect(d).to.equal(b + c);
});

```



# API

## Injector methods

### `injector.get(name)`

Returns instance(s) from cache or creates it/them by executing the injector `factory`.

- `name` - A string or an array of strings with service-names.


### `injector.invoke(fn, [self], [locals])`

Injects the given function while executing it with `apply`.

- `fn` - Function to inject.
- `self` - Object which will be bound to `fn` as `this`.
- `locals` - Optional object containing values, which will be used before before cache and factory.


### `injector.instantiate(ctor, [locals])`

Injects the given constructor while instantiating it with `new`

- `ctor` - Constructor to inject.
- `locals` - Optional object containing values, which will be used before before cache and factory.


#Author

Twitter: [@platdesign](https://twitter.com/platdesign)

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