# kontik

> Simple node service container for managing of singletons

Latest version **3.2.1** (published 2019-11-27) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.1 |
| Published | 2019-11-27 |
| First published | 2018-05-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 18 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Antonín Bouchal |
| Maintainers | scorpio1337 |

## Links

- npm: https://www.npmjs.com/package/kontik
- Repository: https://github.com/bouchal/node-kontik
- Homepage: https://github.com/bouchal/node-kontik#readme
- Issues: https://github.com/bouchal/node-kontik/issues
- npm.io page: https://npm.io/package/kontik

## Recent versions

- 3.2.1 (latest) — 2019-11-27
- 3.1.2 — 2019-08-31
- 3.1.1 — 2019-08-31
- 3.0.0 — 2019-08-30
- 2.3.0 — 2019-04-01
- 2.2.0 — 2019-03-31
- 2.1.0 — 2019-01-20
- 2.0.0 — 2019-01-20
- 1.2.3 — 2018-09-05
- 1.2.2 — 2018-08-11
- 1.2.1 — 2018-08-05
- 1.2.0 — 2018-08-05
- 1.1.0 — 2018-06-15
- 1.0.2 — 2018-06-15
- 1.0.1 — 2018-05-12

## README

![kontik logo](https://raw.githubusercontent.com/bouchal/node-kontik/master/doc/images/logo.png)

Easy to understand and simple container for providing singletons.

[![Coverage Status](https://coveralls.io/repos/github/bouchal/node-kontik/badge.svg?branch=master)](https://coveralls.io/github/bouchal/node-kontik?branch=master)

Define providers of services or other instances separated files and load them as singletons.

## Philosophy

For clean architecture development you need container which will provide access to all defined dependencies that you need.

When you define this dependencies, you should have access to two things. ... Container itself (for loading
dependent components) and application config. 
 

## Usage

__Create instance of kontik providers container__

```typescript
import kontik, {Options} from 'kontik'

// Application custom config. Will be provide to all provider definition
const config = {...}

const options: Options = {
    dir: 'customProviderDir' // Default `$CWD/providers`
}

const providers = kontik(config, options)
```

__Create provider definition__

For every provider you need to create file in providers dir. For example `customServiceProvider.ts`

```typescript
import {Provider, Providers} from 'kontik'
import CustomService from './services/CustomService'
import FakeDependService from './services/FakeDependService'

const provider: Provider<CustomService> = async (providers: Providers, config: any) => {
    const fakeDependService = await providers.get<FakeDependService>('fakeDependService')
    const valueFromConfig = config.some.defined.value
     
    return new CustomService(valueFromConfig, fakeDependService)
}

export default provider
```

__Load and use provided instance__

```typescript
import CustomService from './services/CustomService'

const loadService = async () => {
    return await providers.get<CustomService>('customServiceProvider')
}

loadService()
```

### Predefined services

Sometimes you need create providers through different way or you need to initialize them on different place, but work
with them inside services initialized in Kontik.

That's why here is solution how you can pass already initialized object to Kontik constructor.

```typescript
import kontik, {Options} from 'kontik';
import SomeService from './services/SomeService'

const options: Options = {
    providers: {
        predefinedServiceProvider: new SomeService()
    }
}

const providers = kontik({...config}, options);

const loadService = async () => {
    return await providers.get<CustomService>('predefinedServiceProvider')
}

loadService()
```

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