This project is part of the @daisugi monorepo.
Kado is a minimal and unobtrusive inversion of control container.
- 💡 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.
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');
- @daisugi/kado
Using npm:
npm install @daisugi/kado
Using yarn:
yarn add @daisugi/kado
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.
Used for registration of manifest items
in the container
.
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([{ token: 'Foo' }]);
Use this method when you need to resolve the registered dependency.
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([{ token: 'Foo' }]);
const foo = await container.resolve('Foo');
Returns registered manifest item
by token
.
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
class Foo {}
container.register([
{
token: 'Foo',
useClass: Foo,
scope: 'Transient',
},
]);
const manifestItem = container.get('Foo');
Is the name used to register the dependency, to later be resolved.
Can go along with params
property, which contains tokens
with which the class should be resolved.
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');
Useful for storing constants.
import { Kado } from '@daisugi/kado';
const { container } = new Kado();
container.register([
{
token: 'foo',
useValue: 'text',
},
]);
const foo = await container.resolve('foo');
Provides container
as argument to the factory method.
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');
Same as useFnByContainer
, except provides params
to it, instead of the container
.
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 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.
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');
Can be used to store arbitrary values.
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
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
.
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'
Get the list of the registered dependencies.
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.
Useful when you want to inject a value.
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'
Useful when you want to resolve an array of items.
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'
The same as Kado.map
but also it flats the array result.
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'
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');
The project aims to provide the basic functionality for IoC. The functionality will be kept simple and will not be overextended.
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.