# bottler

> Very simple ioc container for Javascript(& Typescript).

Latest version **0.3.0** (published 2018-11-12) · MIT license · 0 weekly downloads

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

## Install

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

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.3.0 |
| Published | 2018-11-12 |
| First published | 2018-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Cris |
| Maintainers | corgidisco |
| Keywords | di, ioc, dependency injection, inversion of control, inversify, ninject, typescript |

## Links

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

## 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.3.0 (latest) — 2018-11-12
- 0.2.0 — 2018-06-01
- 0.1.0 — 2018-05-16
- 0.0.0 — 2018-05-16

## README

# Bottler

[![Build](https://travis-ci.org/corgidisco/bottler.svg?branch=master)](https://travis-ci.org/corgidisco/bottler)
[![Downloads](https://img.shields.io/npm/dt/bottler.svg)](https://npmcharts.com/compare/bottler?minimal=true)
[![Version](https://img.shields.io/npm/v/bottler.svg)](https://www.npmjs.com/package/bottler)
[![License](https://img.shields.io/npm/l/bottler.svg)](https://www.npmjs.com/package/bottler)

[![dependencies Status](https://david-dm.org/corgidisco/bottler/status.svg)](https://david-dm.org/corgidisco/bottler)
[![devDependencies Status](https://david-dm.org/corgidisco/bottler/dev-status.svg)](https://david-dm.org/corgidisco/bottler?type=dev)

[![NPM](https://nodei.co/npm/bottler.png)](https://www.npmjs.com/package/bottler)

Very simple ioc container for Javascript(& Typescript).

## Installation

```bash
npm install bottler --save
```

## Usage

```javascript
const bottler = require("bottler")
// or import * as bottler from "bottler"

const container = bottler.create()
// or const container = new bottler.Container()
```

### Bind simple value

```ts
container.instance("obj1", {message: "this is obj1"})
container.instance("obj2", {message: "this is obj2"})

console.log(await container.get("obj1")) // {message: "this is obj1"}
console.log(await container.get("obj2")) // {message: "this is obj2"}

console.log(await container.get("obj1") === await container.get("obj1")) // true
console.log(await container.get("obj2") === await container.get("obj2")) // true
```

### Bind promise value

```ts
function promise1() {
  return new Promise(resolve => resolve({message: "this is promise1"}))
}
async function promise2() {
  sleep(500)
  return {message: "this is promise2"}
}
container.instance("promise1", promise1())
container.instance("promise2", promise2())

console.log(await container.get("promise1")) // {message: "this is promise1"}
console.log(await container.get("promise2")) // {message: "this is promise2"}

console.log(await container.get("promise1") === await container.get("promise1")) // true
console.log(await container.get("promise2") === await container.get("promise2")) // true
```

### Bind factory

```ts
container.factory("factory1", () => ({message: "this is factory"}))
container.factory("factory2", () => {
  return new Promise(resolve => {
    resolve({message: "this is promise factory"})
  })
})
container.factory("factory3", async () => {
  sleep(500)
  return {message: "this is async factory"}
})

console.log(await container.get("factory1")) // {message: "this is factory"}
console.log(await container.get("factory2")) // {message: "this is promise factory"}
console.log(await container.get("factory3")) // {message: "this is async factory"}
```

### Bind class

```ts
class Driver {
}

class Connection {
  constructor(@bottler.inject("driver") public driver: Driver) {
  }
}
container.bind("driver", Driver)
container.bind("connection", Connection)

const connection = await container.get<Connection>("connection")
console.log(connection) // Connection { driver: Driver {} }
console.log(connection.driver) // Driver {}
```

### Singleton descriptor

Descriptor is very useful if you using factory or bind. this is example of singleton.

```ts
container.factory("factory.normal", () => ({message: "this is factory"}))
container.factory("factory.singleton", () => ({message: "this is factory with singleton"})).singleton()

container.bind("class.normal", Foo)
container.bind("class.singleton", Foo).singleton()

// not same
console.log(await container.get("factory.normal") === await container.get("factory.normal")) // false
console.log(await container.get("class.normal") === await container.get("class.normal")) // false

// always same
console.log(await container.get("factory.singleton") === await container.get("factory.singleton")) // true
console.log(await container.get("class.singleton") === await container.get("class.singleton")) // true
```

### After descriptor

```ts

container
  .factory("foo", () => ({message: "this is origin maessage."}))
  .after(async (context) => {
    await sleep(300)
    context.message = context.message + " and something appended."
    return context
  })

console.log(await container.get("foo")) // {message: "this is origin maessage. and something appended."}
```

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