3.2.1 • Published 4 years ago

kontik v3.2.1

Weekly downloads
21
License
MIT
Repository
github
Last release
4 years ago

kontik logo

Easy to understand and simple container for providing singletons.

Coverage Status

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

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

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

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.

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()
3.2.1

4 years ago

3.2.0

4 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.0

5 years ago

2.3.0

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago