# krate

> Flexible async inverson of control dependency injection container for javascript.

Latest version **0.2.2** (published 2016-12-29) · MIT license · 0 weekly downloads

## Install

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

## 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.2 |
| Published | 2016-12-29 |
| First published | 2016-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Christian Bradley |
| Maintainers | christianbradley |
| Keywords | inversion, control, dependency, injection, container, async, promise |

## Links

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

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 0.2.2 (latest) — 2016-12-29
- 0.2.1 — 2016-12-29
- 0.2.0 — 2016-12-29
- 0.1.0 — 2016-12-29

## README

# ![krate.js logo](http://i.imgur.com/krjQPnU.png)

![npm version](https://img.shields.io/npm/v/krate.svg)
![npm license](https://img.shields.io/npm/l/krate.svg)
![dependencies](https://david-dm.org/christianbradley/krate.png)

Flexible async IOC dependency injection container for javascript

* [installation](#installation)
* [quick start](#quick-start)

## installation

Install using [npm](http://npmjs.org)

```
npm install krate
```

## quick start

#### configure krate.js

All krate.js dependencies are injectable via the core `configure` function.
This allows you to use your own Promise library, for example.

```js
const Krate = require('krate').configure({
  DEFAULT_TIMEOUT: 10000,
  Promise: require('bluebird')
})
```


#### create a container

Your container will be used to define and resolve your app's components.

```js
const container = new Krate.Container()
```

#### define some components

Each component is defined using a `factory` that can be synchronous or asynchronous.
Synchronous factories can return a value or a promise, while async factories will be passed
a hybrid node.js-style callback/deferred resolver.

```js
container.define({
  foo: { factory: () => 'foo' },
  bar: { factory: () => Promise.resolve('bar') },
  baz: { factory: (done) => done(null, 'baz') },
  qux: { factory: (deferred) => deferred.resolve('qux') }
})
```

#### define components with dependencies

In addition to a factory, each component may define their `depends`. These will be resolved to an object and injected as the first parameter of the factory. Basic depends can be defined as strings (with "as" aliases), arrays of strings, or objects with keys representing aliases, and values representing dependency names.

```js
container.define('combos/foofoo', {
  depends: 'foo as val',
  factory: ({ val }) => val + val
})

container.define('combos/foobar', {
  depends: ['foo', 'bar as b'],
  factory: ({ foo, bar }, deferred) => deferred.resolve(foo + bar)
})

container.define('combos/barbaz', {
  depends: { b: 'bar', baz: true },
  factory: ({ b, baz }, done) => done(null, b + baz)
})
```

#### define complex dependencies with a reducer

For more complex scenarios, you can use a function to reduce the array of all defined component keys into an array of strings to be resolved as your component's `depends`.

For example, we can use a reducer to build an index of all components starting with "combos/".

```js
container.define('combos', {
  factory: (values) => values,
  depends: (depends, key, i) => {
    if(key.indexOf("combos/") !== 0) return depends
    return depends.concat(`${key} as ${key.slice(7)}`)
  }
})
```

#### resolve components

You can resolve your components using the same format you used for defining
your component's `depends`. The `resolve` method returns a promised object
with each of your resolved components, keyed by their name or their provided alias.

```js
container.resolve(['combos as c', 'foo']).then(({ c, foo }) => {
  console.log(c) // => { foobar: "foobar", barbaz: "barbaz" }
  console.log(f) // => "foo"
})
```

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