@daisugi/kado
v0.5.2
Published
Kado is a minimal and unobtrusive inversion of control container.
Downloads
129
Readme
@daisugi/kado
This project is part of the @daisugi monorepo.
Kado is a minimal and unobtrusive inversion of control container.
🌟 Features
- 💡 Minimum size overhead.
- ⚡️ Written in TypeScript.
- 📦 Only uses trusted dependencies.
- 🔨 Powerful and agnostic to your code.
- 🧪 Well tested.
- 🤝 Is used in production.
- ⚡️ Exports ES Modules as well as CommonJS.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(bar) {
this.bar = bar;
}
}
class Bar {}
container.register([
{
token: 'Foo',
useClass: Foo,
params: ['Bar'],
},
{
token: 'Bar',
useClass: Bar,
},
]);
const foo = await container.resolve('Foo');
Table of contents
- @daisugi/kado
Install
Using npm:
npm install @daisugi/kado
Using yarn:
yarn add @daisugi/kado
Motivation
This library is a result of a series of the requirements that either were not met by other libraries of same type, or were partially met, or finally met everything but also brought an overhead not required by the project.
If you feel that any of the following requirements is close to your demand, feel free to use this library, otherwise there are many other good IoC libraries out there such as di-ninja or tsyringe, among many others that you can use.
- ✅ Should allow to create multiple instances of the container, and not share the state globally (useful when multiple packages are using it, or for monorepo).
- ✅ The DI configuration must be abstracted from the base code, and must be able to be easily ported (Composition Root).
- ✅ Dependencies must be able easily decorated (useful to add telemetry, debug ...).
- ✅ Avoid use of decorators by annotations (see style guide).
- ✅ Should work with pure JavaScript (don't depend of any superset like TypeScript).
- ✅ Keep the API simple (singleton, transient, classes, values, factories, and not much more), but with enough pieces to cover the most common use cases.
API
#register(manifestItems)
Used for registration of manifest items
in the container
.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([{ token: 'Foo' }]);
#resolve(token)
Use this method when you need to resolve the registered dependency.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([{ token: 'Foo' }]);
const foo = await container.resolve('Foo');
#get(token)
Returns registered manifest item
by token
.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
container.register([
{
token: 'Foo',
useClass: Foo,
scope: 'Transient',
},
]);
const manifestItem = container.get('Foo');
token
Is the name used to register the dependency, to later be resolved.
useClass
Can go along with params
property, which contains tokens
with which the class should be resolved.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(bar) {
this.bar = bar;
}
}
class Bar {}
container.register([
{
token: 'Foo',
useClass: Foo,
params: ['Bar'],
},
{
token: 'Bar',
useClass: Bar,
},
]);
const foo = await container.resolve('Foo');
useValue
Useful for storing constants.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([
{
token: 'foo',
useValue: 'text',
},
]);
const foo = await container.resolve('foo');
useFnByContainer
Provides container
as argument to the factory method.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
function bar(c) {
return c.resolve('Foo');
}
container.register([
{
token: 'Foo',
useClass: Foo,
},
{
token: 'bar',
useFnByContainer: bar,
},
]);
const foo = await container.resolve('bar');
useFn
Same as useFnByContainer
, except provides params
to it, instead of the container
.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
function bar(foo) {
return foo;
}
container.register([
{
token: 'Foo',
useClass: Foo,
},
{
token: 'bar',
useFn: bar,
params: ['Foo'],
},
]);
const foo = await container.resolve('bar');
scope
Scope can be Transient
or Singleton
, by default it's Singleton
. Can be used along with useClass
, useFnByContainer
and useFn
. Having scope as Transient
it will create a new instance every time the dependency is resolved, Singleton
will reuse the already created instance.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
container.register([
{
token: 'Foo',
useClass: Foo,
scope: 'Transient',
},
]);
const foo = await container.resolve('Foo');
meta
Can be used to store arbitrary values.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
container.register([
{
token: 'Foo',
meta: {
isFoo: true,
}
},
]);
const foo = container.get('Foo');
foo.meta.isFoo; // true
params
As can be observed in the previous examples the params
key can receive an array of tokens
, but also you can provide manifest items
, you have an example below where we are injecting a text to the Foo
class. Also for the convenience Kado
provides some helpers Kado.value, Kado.map and Kado.flatMap, behind the scene these helpers are returning a simple manifest items
.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(bar) {
this.bar = bar;
}
}
container.register([
{
token: 'Foo',
useClass: Foo,
param: [{
useValue: 'text',
}],
},
]);
const foo = await container.resolve('Foo');
foo.bar; // 'text'
#list()
Get the list of the registered dependencies.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
container.register([
{
token: 'Foo',
useClass: Foo,
scope: 'Transient',
},
]);
const manifestItems = container.list();
// Now you can iterate over the manifest items and decorate them.
Kado.value
Useful when you want to inject a value.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(bar) {
this.bar = bar;
}
}
container.register([
{
token: 'Foo',
useClass: Foo,
param: [Kado.value('text')],
},
]);
const foo = await container.resolve('Foo');
foo.bar; // 'text'
Kado.map
Useful when you want to resolve an array of items.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(args) {
this.bar = args[0];
}
}
container.register([
{
token: 'bar',
useValue: 'text',
},
{
token: 'Foo',
useClass: Foo,
param: [Kado.map(['bar'])],
},
]);
const foo = await container.resolve('Foo');
foo.bar; // 'text'
Kado.flatMap
The same as Kado.map
but also it flats the array result.
Usage
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {
constructor(args) {
this.bar = args[0];
}
}
container.register([
{
token: 'bar',
useValue: ['text'],
},
{
token: 'Foo',
useClass: Foo,
param: [Kado.flatMap(['bar'])],
},
]);
const foo = await container.resolve('Foo');
foo.bar; // 'text'
TypeScript
The Kado is fully written in TypeScript, therefore you have available some types.
import {
Kado,
type KadoManifestItem,
type KadoContainer,
type KadoToken,
type KadoScope,
} from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
const myContainer: KadoContainer = container;
const token: KadoToken = 'Foo';
const scope: KadoScope = Kado.scope.Transient;
const manifestItems: KadoManifestItem[] = [
{
token,
useClass: Foo,
scope,
}
];
myContainer.register(manifestItems);
const foo = await myContainer.resolve<Foo>('Foo');
Goal
The project aims to provide the basic functionality for IoC. The functionality will be kept simple and will not be overextended.
Etymology
Kado is a Japanese art that involves an arrangement of a variety of plants. A characteristic of Japanese Kado is an emphasis on shapes and lines, as well as the manner in which the flower is placed into the dish.