Gathering detailed insights and metrics for @daisugi/kado
Gathering detailed insights and metrics for @daisugi/kado
Gathering detailed insights and metrics for @daisugi/kado
Gathering detailed insights and metrics for @daisugi/kado
🌿 Daisugi monorepo of TypeScript/ESM projects for building composable applications.
npm install @daisugi/kado
Typescript
Module System
Node Version
NPM Version
73.6
Supply Chain
95.5
Quality
78.3
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
30,701
Last Day
8
Last Week
38
Last Month
161
Last Year
1,874
18 Stars
405 Commits
5 Forks
4 Watching
5 Branches
3 Contributors
Latest Version
0.5.2
Package Id
@daisugi/kado@0.5.2
Unpacked Size
51.23 kB
Size
10.09 kB
File Count
11
NPM Version
lerna/3.0.0/node@v20.10.0+x64 (linux)
Node Version
20.10.0
Publised On
04 Jan 2024
Cumulative downloads
Total Downloads
Last day
-50%
8
Compared to previous day
Last week
-19.1%
38
Compared to previous week
Last month
14.2%
161
Compared to previous month
Last year
-81.7%
1,874
Compared to previous year
2
4
This project is part of the @daisugi monorepo.
Kado is a minimal and unobtrusive inversion of control container.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(bar) { 7 this.bar = bar; 8 } 9} 10 11class Bar {} 12 13container.register([ 14 { 15 token: 'Foo', 16 useClass: Foo, 17 params: ['Bar'], 18 }, 19 { 20 token: 'Bar', 21 useClass: Bar, 22 }, 23]); 24 25const foo = await container.resolve('Foo');
Using npm:
1npm install @daisugi/kado
Using yarn:
1yarn 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.
Used for registration of manifest items
in the container
.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5container.register([{ token: 'Foo' }]);
Use this method when you need to resolve the registered dependency.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5container.register([{ token: 'Foo' }]); 6 7const foo = await container.resolve('Foo');
Returns registered manifest item
by token
.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7container.register([ 8 { 9 token: 'Foo', 10 useClass: Foo, 11 scope: 'Transient', 12 }, 13]); 14 15const 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.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(bar) { 7 this.bar = bar; 8 } 9} 10 11class Bar {} 12 13container.register([ 14 { 15 token: 'Foo', 16 useClass: Foo, 17 params: ['Bar'], 18 }, 19 { 20 token: 'Bar', 21 useClass: Bar, 22 }, 23]); 24 25const foo = await container.resolve('Foo');
Useful for storing constants.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5container.register([ 6 { 7 token: 'foo', 8 useValue: 'text', 9 }, 10]); 11 12const foo = await container.resolve('foo');
Provides container
as argument to the factory method.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7function bar(c) { 8 return c.resolve('Foo'); 9} 10 11container.register([ 12 { 13 token: 'Foo', 14 useClass: Foo, 15 }, 16 { 17 token: 'bar', 18 useFnByContainer: bar, 19 }, 20]); 21 22const foo = await container.resolve('bar');
Same as useFnByContainer
, except provides params
to it, instead of the container
.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7function bar(foo) { 8 return foo; 9} 10 11container.register([ 12 { 13 token: 'Foo', 14 useClass: Foo, 15 }, 16 { 17 token: 'bar', 18 useFn: bar, 19 params: ['Foo'], 20 }, 21]); 22 23const 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.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7container.register([ 8 { 9 token: 'Foo', 10 useClass: Foo, 11 scope: 'Transient', 12 }, 13]); 14 15const foo = await container.resolve('Foo');
Can be used to store arbitrary values.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7container.register([ 8 { 9 token: 'Foo', 10 meta: { 11 isFoo: true, 12 } 13 }, 14]); 15 16const foo = container.get('Foo'); 17foo.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
.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(bar) { 7 this.bar = bar; 8 } 9} 10 11container.register([ 12 { 13 token: 'Foo', 14 useClass: Foo, 15 param: [{ 16 useValue: 'text', 17 }], 18 }, 19]); 20 21const foo = await container.resolve('Foo'); 22 23foo.bar; // 'text'
Get the list of the registered dependencies.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo {} 6 7container.register([ 8 { 9 token: 'Foo', 10 useClass: Foo, 11 scope: 'Transient', 12 }, 13]); 14 15const manifestItems = container.list(); 16 17// Now you can iterate over the manifest items and decorate them.
Useful when you want to inject a value.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(bar) { 7 this.bar = bar; 8 } 9} 10 11container.register([ 12 { 13 token: 'Foo', 14 useClass: Foo, 15 param: [Kado.value('text')], 16 }, 17]); 18 19const foo = await container.resolve('Foo'); 20 21foo.bar; // 'text'
Useful when you want to resolve an array of items.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(args) { 7 this.bar = args[0]; 8 } 9} 10 11container.register([ 12 { 13 token: 'bar', 14 useValue: 'text', 15 }, 16 { 17 token: 'Foo', 18 useClass: Foo, 19 param: [Kado.map(['bar'])], 20 }, 21]); 22 23const foo = await container.resolve('Foo'); 24 25foo.bar; // 'text'
The same as Kado.map
but also it flats the array result.
1import { Kado } from '@daisugi/kado'; 2 3const { container } = new Kado(); 4 5class Foo { 6 constructor(args) { 7 this.bar = args[0]; 8 } 9} 10 11container.register([ 12 { 13 token: 'bar', 14 useValue: ['text'], 15 }, 16 { 17 token: 'Foo', 18 useClass: Foo, 19 param: [Kado.flatMap(['bar'])], 20 }, 21]); 22 23const foo = await container.resolve('Foo'); 24 25foo.bar; // 'text'
The Kado is fully written in TypeScript, therefore you have available some types.
1import { 2 Kado, 3 type KadoManifestItem, 4 type KadoContainer, 5 type KadoToken, 6 type KadoScope, 7} from '@daisugi/kado'; 8 9const { container } = new Kado(); 10 11class Foo {} 12 13const myContainer: KadoContainer = container; 14const token: KadoToken = 'Foo'; 15const scope: KadoScope = Kado.scope.Transient; 16const manifestItems: KadoManifestItem[] = [ 17 { 18 token, 19 useClass: Foo, 20 scope, 21 } 22]; 23 24myContainer.register(manifestItems); 25 26const 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.
No vulnerabilities found.
No security vulnerabilities found.