# kinject

> Minimalistic dependency injector, the K stands for KISS

Latest version **0.0.4** (published 2018-04-16) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2018-04-16 |
| First published | 2018-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 17.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Isidro Torregrosa Torralba |
| Maintainers | isidrok |
| Keywords | di, ioc, dependency, injection, container |

## Links

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

## 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.0.4 (latest) — 2018-04-16
- 0.0.3 — 2018-04-16
- 0.0.2 — 2018-04-16
- 0.0.1 — 2018-04-16
- 0.0.0 — 2018-04-16

## README

# Kinject

Minimalistic dependency injector, the K stands for KISS.

[![Build Status](https://travis-ci.org/isidrok/kinject.svg?branch=master)](https://travis-ci.org/isidrok/kinject) [![Coverage Status](https://coveralls.io/repos/github/isidrok/kinject/badge.svg?branch=master)](https://coveralls.io/github/isidrok/kinject?branch=master)

## Description

A simple dependency injector inspired by Angular ones, lets you create `service locators` where providers are registered and from with classes are instantiated with its dependencies injected on the constructor.

## Setup

```bash
npm install --save kinject
```

If your target browsers doesn't support [`Proxy`](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Proxy) you can lazy load the [`polyfill`](https://github.com/GoogleChrome/proxy-polyfill) like this:

```js
if(!window.proxy){
    var script = document.createElement('script');
    script.src = 'proxy.min.js';
    document.body.appendChild(script);
}
```

## Example

```js
const container1 = new Kinject();
const container2 = new Kinject();
container2.linkTo(container1);

class Foo {}
container1.provide(Foo, 'Foo', /* transient */ true);

class Bar {
    constructor(foo) {
        this.foo = foo;
    }
}
Bar = container2.register(Bar, {
    inject: ['Foo'],
    provide: 'Bar',
});

class Baz {
    constructor(foo, bar) {
        this.foo = foo;
        this.bar = bar;
    }
}
Baz = container2.inject(Baz, ['Foo', 'Bar']);
baz1 = new Baz();
baz2 = new Baz();
console.log(baz1.bar === baz2.bar); // true
console.log(baz1.foo === baz2.foo); //false
```

## API

### Create a container

```js
const appContainer = new Kinject();
```

### Register a provider

For registering a provider use the method `container.provide` which recieves as arguments:

* `ctor`: constructor or instance to provide.
* `token`: injection token for that provider.
* `transient`: default `false`, if set to true every time a provider registered is injected a new instance will be created. In this case `ctor` must be instantiable. Default behaviour is to create a singleton the first time the dependency is injected.

```js
class Foo{ }
appContainer.provide(Foo, 'Foo');
appContainer.provide(Foo, 'TransientFoo' , true);
```

### Inject dependencies

When injecting dependencies to a class use the method `container.inject` passing:

* `ctor`: class to which dependencies will be injected in the constructor.
* `dependencies`: array of strings representing the providers.

The function will return a `proxy` that will inject all provided dependencies everytime the class
is instantiated. Dependencies will be recieved in the same order that were specified in the dependencies array and starting at the first position of the constructor.

```js
class Bar{
    constructor(foo, someParam){
        this._foo = foo;
    }
}
Bar = appContainer.inject(Bar, ['Foo']);
```

### All in one

If a class acts as a provider and also recieves injected dependencies, you can first use the `inject` method and then register it as an injectable with `provide` or use the utility method `container.register` which takes as arguments:

* `ctor`: class to which dependencies will be injected and that will act as a provider
* `ConfigObejct`.

  * `provide`: token for the provider.
  * `transient`: boolean, default false.
  * `inject`: array of dependencies tokens to get injected.

This method will return a proxy or the constructor given if no inject was supplied.

```js
 class Baz{
     constructor(foo){
         this._foo = foo;
     }
 }
 Baz = appContainer.register(Baz, {
     provide: 'Baz',
     inject: 'Foo'
 });
 ```

### Link to a parent container

You can specify a parent of a container, when looking for a provider in the child container, if it is not found, it will be provided from the parent if present.

```js
const appContainer = new Kinject();
appContainer.provide(UserService, 'UserService');
const submoduleContainer = new Kinject();
submoduleContainer.linkTo(appContainer);
UserProfile = submoduleContainer.inject(UserProfile, ['UserService']);
```

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