katagami 2.3.0 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -592
- package/dist/chunk-J2NYR3SH.js +6 -0
- package/dist/container/index.d.cts +108 -0
- package/dist/container/index.d.ts +5 -82
- package/dist/disposable/index.cjs +16 -25
- package/dist/disposable/index.d.cts +69 -0
- package/dist/disposable/index.d.ts +16 -8
- package/dist/disposable/index.js +1 -1
- package/dist/error/index.d.cts +11 -0
- package/dist/index.cjs +181 -86
- package/dist/index.d.cts +6 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +177 -45
- package/dist/internal.d.cts +29 -0
- package/dist/internal.d.ts +3 -1
- package/dist/lazy/index.cjs +16 -25
- package/dist/lazy/index.d.cts +33 -0
- package/dist/lazy/index.d.ts +7 -9
- package/dist/lazy/index.js +1 -1
- package/dist/resolver/index.d.cts +93 -0
- package/dist/scope/index.d.cts +120 -0
- package/dist/scope/index.d.ts +4 -4
- package/docs/README.de.md +65 -0
- package/docs/README.es.md +65 -0
- package/docs/README.fr.md +65 -0
- package/docs/README.ja.md +66 -0
- package/docs/README.ko.md +65 -0
- package/docs/README.zh-CN.md +65 -0
- package/docs/README.zh-TW.md +65 -0
- package/docs/ai-coding-agents.md +78 -0
- package/docs/articles/ai-coding-agents.ja.md +83 -0
- package/docs/articles/ai-coding-agents.md +70 -0
- package/docs/articles/request-scope.md +48 -0
- package/docs/articles/without-decorators.md +54 -0
- package/docs/choosing-di.md +30 -0
- package/docs/growth/baseline-2026-09-11.json +68 -0
- package/docs/growth/github-metadata.json +13 -0
- package/docs/growth/rollout.md +77 -0
- package/docs/guide.md +186 -0
- package/docs/type-safety.md +126 -0
- package/examples/request-scope/README.md +37 -0
- package/examples/request-scope/app.ts +31 -0
- package/examples/request-scope/demo.ts +10 -0
- package/examples/request-scope/tsconfig.json +11 -0
- package/llms.txt +16 -0
- package/package.json +56 -28
- package/dist/index-g50fxds1.js +0 -34
- package/dist/index-jx8b52m0.js +0 -4
- package/dist/scope/index.cjs +0 -212
- package/dist/scope/index.js +0 -153
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type ContainerInternals, INTERNALS } from '../internal.cjs';
|
|
2
|
+
import type { AbstractConstructor, Resolver } from '../resolver/index.cjs';
|
|
3
|
+
/**
|
|
4
|
+
* Create a new DI container.
|
|
5
|
+
*
|
|
6
|
+
* Pass an interface as generic T to fix the PropertyKey token type map upfront (order-independent).
|
|
7
|
+
* Class tokens are accumulated via registerSingleton/registerTransient method chaining (order-dependent).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* interface Services { SampleController: string }
|
|
12
|
+
* const c = createContainer<Services>()
|
|
13
|
+
* .registerSingleton(TextGenerationService, () => new MastraTextGenerationService())
|
|
14
|
+
* .registerTransient(GenerateTextUseCase, r => new GenerateTextUseCase(r.resolve(TextGenerationService)));
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function createContainer<T = Record<never, never>, ScopedT = Record<never, never>>(): Container<T, never, never, ScopedT>;
|
|
18
|
+
/**
|
|
19
|
+
* Lightweight DI container — registration only.
|
|
20
|
+
*
|
|
21
|
+
* Provides type inference through method chaining with registerSingleton/registerTransient/registerScoped.
|
|
22
|
+
* Resolution is performed through a Scope created via `createScope(container)`.
|
|
23
|
+
*
|
|
24
|
+
* Registering the same token multiple times accumulates all factories.
|
|
25
|
+
*
|
|
26
|
+
* @template T PropertyKey-based token type map (defined via interface, order-independent)
|
|
27
|
+
* @template Sync Union of registered sync class constructors (accumulated via chaining, order-dependent)
|
|
28
|
+
* @template Async Union of registered async class constructors (accumulated via chaining, order-dependent)
|
|
29
|
+
* @template ScopedT PropertyKey-based token type map for scoped registrations
|
|
30
|
+
* @template ScopedSync Union of scoped sync class constructors (accumulated via chaining, order-dependent)
|
|
31
|
+
* @template ScopedAsync Union of scoped async class constructors (accumulated via chaining, order-dependent)
|
|
32
|
+
*/
|
|
33
|
+
export declare class Container<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> {
|
|
34
|
+
private readonly registrations;
|
|
35
|
+
private readonly singletonCache;
|
|
36
|
+
private disposed;
|
|
37
|
+
/**
|
|
38
|
+
* Internal state accessor for extension modules (scope, disposable).
|
|
39
|
+
*
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
readonly [INTERNALS]: ContainerInternals;
|
|
43
|
+
constructor();
|
|
44
|
+
/**
|
|
45
|
+
* Register a factory function as a singleton for the given token.
|
|
46
|
+
*
|
|
47
|
+
* Creates the instance on the first resolve and returns the cached value thereafter.
|
|
48
|
+
* If the same token is registered multiple times, all factories are accumulated.
|
|
49
|
+
* `resolve()` returns the last registered instance; `resolveAll()` returns all.
|
|
50
|
+
*
|
|
51
|
+
* @param token Any value to use as a token
|
|
52
|
+
* @param factory Factory function that receives a resolver and returns an instance
|
|
53
|
+
* @returns The container for method chaining
|
|
54
|
+
*/
|
|
55
|
+
registerSingleton<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync>;
|
|
56
|
+
registerSingleton<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
57
|
+
registerSingleton<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
58
|
+
registerSingleton<V>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
59
|
+
/**
|
|
60
|
+
* Register a factory function as transient for the given token.
|
|
61
|
+
*
|
|
62
|
+
* Creates a new instance via the factory function on every resolve.
|
|
63
|
+
* If the same token is registered multiple times, all factories are accumulated.
|
|
64
|
+
* `resolve()` returns the last registered instance; `resolveAll()` returns all.
|
|
65
|
+
*
|
|
66
|
+
* @param token Any value to use as a token
|
|
67
|
+
* @param factory Factory function that receives a resolver and returns an instance
|
|
68
|
+
* @returns The container for method chaining
|
|
69
|
+
*/
|
|
70
|
+
registerTransient<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => Promise<V>): Container<T, Sync, Async | AbstractConstructor<V>, ScopedT, ScopedSync, ScopedAsync>;
|
|
71
|
+
registerTransient<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync | AbstractConstructor<V>, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
72
|
+
registerTransient<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<Record<K, V> & T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
73
|
+
registerTransient<V>(token: unknown, factory: (resolver: Resolver<T, Sync, Async>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
74
|
+
/**
|
|
75
|
+
* Register a factory function as scoped for the given token.
|
|
76
|
+
*
|
|
77
|
+
* Within a scope, creates the instance on the first resolve and returns the cached value thereafter.
|
|
78
|
+
* Each scope maintains its own cache, so different scopes produce different instances.
|
|
79
|
+
* Scoped tokens cannot be resolved from the root container — use createScope() first.
|
|
80
|
+
*
|
|
81
|
+
* @param token Any value to use as a token
|
|
82
|
+
* @param factory Factory function that receives a resolver and returns an instance
|
|
83
|
+
* @returns The container for method chaining
|
|
84
|
+
*/
|
|
85
|
+
registerScoped<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => Promise<V>): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync | AbstractConstructor<V>>;
|
|
86
|
+
registerScoped<V>(token: AbstractConstructor<V>, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, ScopedT, ScopedSync | AbstractConstructor<V>, ScopedAsync>;
|
|
87
|
+
registerScoped<K extends PropertyKey, V>(token: K, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, Record<K, V> & ScopedT, ScopedSync, ScopedAsync>;
|
|
88
|
+
registerScoped<V>(token: unknown, factory: (resolver: Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>) => V): Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
89
|
+
/**
|
|
90
|
+
* Apply all registrations from another container (module) to this container.
|
|
91
|
+
*
|
|
92
|
+
* Copies registration entries (factory + lifetime) by replacing existing entries for each token.
|
|
93
|
+
* Singleton instance caches are not shared — each container manages its own.
|
|
94
|
+
*
|
|
95
|
+
* @param source A container whose registrations will be copied into this container
|
|
96
|
+
* @returns The container for method chaining
|
|
97
|
+
*/
|
|
98
|
+
use<MT, MSync extends AbstractConstructor, MAsync extends AbstractConstructor, MScopedT, MScopedSync extends AbstractConstructor, MScopedAsync extends AbstractConstructor>(source: Container<MT, MSync, MAsync, MScopedT, MScopedSync, MScopedAsync>): Container<T & MT, Sync | MSync, Async | MAsync, ScopedT & MScopedT, ScopedSync | MScopedSync, ScopedAsync | MScopedAsync>;
|
|
99
|
+
/**
|
|
100
|
+
* Add a registration entry. Accumulates registrations for the same token.
|
|
101
|
+
*
|
|
102
|
+
* @param token Token
|
|
103
|
+
* @param factory Factory function
|
|
104
|
+
* @param lifetime Lifetime of the registration
|
|
105
|
+
* @returns The container for method chaining
|
|
106
|
+
*/
|
|
107
|
+
private addRegistration;
|
|
108
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type ContainerInternals, INTERNALS } from '../internal';
|
|
2
|
-
import type { AbstractConstructor, Resolver } from '../resolver';
|
|
1
|
+
import { type ContainerInternals, INTERNALS } from '../internal.js';
|
|
2
|
+
import type { AbstractConstructor, Resolver } from '../resolver/index.js';
|
|
3
3
|
/**
|
|
4
4
|
* Create a new DI container.
|
|
5
5
|
*
|
|
@@ -16,15 +16,12 @@ import type { AbstractConstructor, Resolver } from '../resolver';
|
|
|
16
16
|
*/
|
|
17
17
|
export declare function createContainer<T = Record<never, never>, ScopedT = Record<never, never>>(): Container<T, never, never, ScopedT>;
|
|
18
18
|
/**
|
|
19
|
-
* Lightweight DI container.
|
|
19
|
+
* Lightweight DI container — registration only.
|
|
20
20
|
*
|
|
21
|
-
* Provides type inference through method chaining with registerSingleton/registerTransient/
|
|
22
|
-
*
|
|
23
|
-
* - Singleton: Creates the instance on the first resolve and returns the cached value thereafter.
|
|
24
|
-
* - Transient: Creates a new instance via the factory function on every resolve.
|
|
21
|
+
* Provides type inference through method chaining with registerSingleton/registerTransient/registerScoped.
|
|
22
|
+
* Resolution is performed through a Scope created via `createScope(container)`.
|
|
25
23
|
*
|
|
26
24
|
* Registering the same token multiple times accumulates all factories.
|
|
27
|
-
* `resolve()` returns the last registered instance, while `resolveAll()` returns all.
|
|
28
25
|
*
|
|
29
26
|
* @template T PropertyKey-based token type map (defined via interface, order-independent)
|
|
30
27
|
* @template Sync Union of registered sync class constructors (accumulated via chaining, order-dependent)
|
|
@@ -36,7 +33,6 @@ export declare function createContainer<T = Record<never, never>, ScopedT = Reco
|
|
|
36
33
|
export declare class Container<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> {
|
|
37
34
|
private readonly registrations;
|
|
38
35
|
private readonly singletonCache;
|
|
39
|
-
private readonly resolvingTokens;
|
|
40
36
|
private disposed;
|
|
41
37
|
/**
|
|
42
38
|
* Internal state accessor for extension modules (scope, disposable).
|
|
@@ -100,79 +96,6 @@ export declare class Container<T = Record<never, never>, Sync extends AbstractCo
|
|
|
100
96
|
* @returns The container for method chaining
|
|
101
97
|
*/
|
|
102
98
|
use<MT, MSync extends AbstractConstructor, MAsync extends AbstractConstructor, MScopedT, MScopedSync extends AbstractConstructor, MScopedAsync extends AbstractConstructor>(source: Container<MT, MSync, MAsync, MScopedT, MScopedSync, MScopedAsync>): Container<T & MT, Sync | MSync, Async | MAsync, ScopedT & MScopedT, ScopedSync | MScopedSync, ScopedAsync | MScopedAsync>;
|
|
103
|
-
/**
|
|
104
|
-
* Resolve an instance for the given token.
|
|
105
|
-
*
|
|
106
|
-
* Returns the instance from the last registered factory for the token.
|
|
107
|
-
* For singleton registrations, creates the instance on the first call and caches it.
|
|
108
|
-
* For transient registrations, creates a new instance on every call.
|
|
109
|
-
*
|
|
110
|
-
* @param token A registered token
|
|
111
|
-
* @returns The instance associated with the token
|
|
112
|
-
* @throws ContainerError if the token is not registered
|
|
113
|
-
*/
|
|
114
|
-
resolve<V>(token: AbstractConstructor<V> & Async): Promise<V>;
|
|
115
|
-
resolve<V>(token: AbstractConstructor<V> & Sync): V;
|
|
116
|
-
resolve<K extends keyof T>(token: K): T[K];
|
|
117
|
-
/**
|
|
118
|
-
* Try to resolve an instance for the given token.
|
|
119
|
-
*
|
|
120
|
-
* Returns `undefined` instead of throwing when the token is not registered.
|
|
121
|
-
* Other errors (circular dependency, disposed container) are still thrown.
|
|
122
|
-
*
|
|
123
|
-
* @param token A token to resolve
|
|
124
|
-
* @returns The instance associated with the token, or `undefined` if not registered
|
|
125
|
-
*/
|
|
126
|
-
tryResolve<V>(token: AbstractConstructor<V> & Async): Promise<V> | undefined;
|
|
127
|
-
tryResolve<V>(token: AbstractConstructor<V> & Sync): V | undefined;
|
|
128
|
-
tryResolve<K extends keyof T>(token: K): T[K] | undefined;
|
|
129
|
-
tryResolve<V>(token: AbstractConstructor<V>): V | Promise<V> | undefined;
|
|
130
|
-
tryResolve(token: PropertyKey): unknown;
|
|
131
|
-
/**
|
|
132
|
-
* Resolve all instances for the given token.
|
|
133
|
-
*
|
|
134
|
-
* Returns an array of instances from all registered factories for the token,
|
|
135
|
-
* in registration order.
|
|
136
|
-
*
|
|
137
|
-
* @param token A registered token
|
|
138
|
-
* @returns An array of instances associated with the token
|
|
139
|
-
* @throws ContainerError if the token is not registered
|
|
140
|
-
*/
|
|
141
|
-
resolveAll<V>(token: AbstractConstructor<V> & Async): Promise<V>[];
|
|
142
|
-
resolveAll<V>(token: AbstractConstructor<V> & Sync): V[];
|
|
143
|
-
resolveAll<K extends keyof T>(token: K): T[K][];
|
|
144
|
-
/**
|
|
145
|
-
* Try to resolve all instances for the given token.
|
|
146
|
-
*
|
|
147
|
-
* Returns `undefined` instead of throwing when the token is not registered.
|
|
148
|
-
* Other errors (circular dependency, disposed container) are still thrown.
|
|
149
|
-
*
|
|
150
|
-
* @param token A token to resolve
|
|
151
|
-
* @returns An array of instances associated with the token, or `undefined` if not registered
|
|
152
|
-
*/
|
|
153
|
-
tryResolveAll<V>(token: AbstractConstructor<V> & Async): Promise<V>[] | undefined;
|
|
154
|
-
tryResolveAll<V>(token: AbstractConstructor<V> & Sync): V[] | undefined;
|
|
155
|
-
tryResolveAll<K extends keyof T>(token: K): T[K][] | undefined;
|
|
156
|
-
tryResolveAll<V>(token: AbstractConstructor<V>): (V | Promise<V>)[] | undefined;
|
|
157
|
-
tryResolveAll(token: PropertyKey): unknown;
|
|
158
|
-
/**
|
|
159
|
-
* Internal resolution logic shared by resolve and tryResolve.
|
|
160
|
-
* Resolves the last registered factory for the token.
|
|
161
|
-
*
|
|
162
|
-
* @param token Token to resolve
|
|
163
|
-
* @param required If true, throws when the token is not registered. If false, returns undefined.
|
|
164
|
-
* @returns The resolved instance, or undefined if not registered and required is false
|
|
165
|
-
*/
|
|
166
|
-
private resolveToken;
|
|
167
|
-
/**
|
|
168
|
-
* Internal resolution logic shared by resolveAll and tryResolveAll.
|
|
169
|
-
* Resolves all registered factories for the token.
|
|
170
|
-
*
|
|
171
|
-
* @param token Token to resolve
|
|
172
|
-
* @param required If true, throws when the token is not registered. If false, returns undefined.
|
|
173
|
-
* @returns An array of resolved instances, or undefined if not registered and required is false
|
|
174
|
-
*/
|
|
175
|
-
private resolveAllTokens;
|
|
176
99
|
/**
|
|
177
100
|
* Add a registration entry. Accumulates registrations for the same token.
|
|
178
101
|
*
|
|
@@ -1,40 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
-
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
8
|
-
if (entry)
|
|
9
|
-
return entry;
|
|
10
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
}));
|
|
16
|
-
__moduleCache.set(from, entry);
|
|
17
|
-
return entry;
|
|
18
|
-
};
|
|
19
6
|
var __export = (target, all) => {
|
|
20
7
|
for (var name in all)
|
|
21
|
-
__defProp(target, name, {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
27
17
|
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
19
|
|
|
29
20
|
// src/disposable/index.ts
|
|
30
|
-
var
|
|
31
|
-
__export(
|
|
21
|
+
var disposable_exports = {};
|
|
22
|
+
__export(disposable_exports, {
|
|
32
23
|
disposable: () => disposable
|
|
33
24
|
});
|
|
34
|
-
module.exports = __toCommonJS(
|
|
25
|
+
module.exports = __toCommonJS(disposable_exports);
|
|
35
26
|
|
|
36
27
|
// src/internal.ts
|
|
37
|
-
var INTERNALS = Symbol("katagami.internals");
|
|
28
|
+
var INTERNALS = /* @__PURE__ */ Symbol.for("katagami.internals.v3");
|
|
38
29
|
|
|
39
30
|
// src/disposable/index.ts
|
|
40
31
|
function disposable(container) {
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Container } from '../container/index.cjs';
|
|
2
|
+
import { type ContainerInternals, INTERNALS, type TYPE_STATE } from '../internal.cjs';
|
|
3
|
+
import type { AbstractConstructor, Resolver } from '../resolver/index.cjs';
|
|
4
|
+
import type { Scope } from '../scope/index.cjs';
|
|
5
|
+
/**
|
|
6
|
+
* A container wrapped with `disposable()`.
|
|
7
|
+
*
|
|
8
|
+
* Registration methods (`registerSingleton`, `registerTransient`, `registerScoped`, `use`)
|
|
9
|
+
* are excluded, preventing accidental registration on a potentially-disposed container.
|
|
10
|
+
* Use `createScope()` to create a scope for resolution.
|
|
11
|
+
*
|
|
12
|
+
* @template T PropertyKey-based token type map
|
|
13
|
+
* @template Sync Union of registered sync class constructors
|
|
14
|
+
* @template Async Union of registered async class constructors
|
|
15
|
+
* @template ScopedT PropertyKey-based token type map for scoped registrations
|
|
16
|
+
* @template ScopedSync Union of scoped sync class constructors
|
|
17
|
+
* @template ScopedAsync Union of scoped async class constructors
|
|
18
|
+
*/
|
|
19
|
+
export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends AsyncDisposable {
|
|
20
|
+
readonly [INTERNALS]: ContainerInternals;
|
|
21
|
+
readonly [TYPE_STATE]?: {
|
|
22
|
+
readonly kind: 'container';
|
|
23
|
+
readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A scope wrapped with `disposable()`.
|
|
28
|
+
*
|
|
29
|
+
* Only `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` are available at the type level.
|
|
30
|
+
*
|
|
31
|
+
* @template T PropertyKey-based token type map
|
|
32
|
+
* @template Sync Union of registered sync class constructors
|
|
33
|
+
* @template Async Union of registered async class constructors
|
|
34
|
+
* @template ScopedT PropertyKey-based token type map for scoped registrations
|
|
35
|
+
* @template ScopedSync Union of scoped sync class constructors
|
|
36
|
+
* @template ScopedAsync Union of scoped async class constructors
|
|
37
|
+
*/
|
|
38
|
+
export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
|
|
39
|
+
readonly [INTERNALS]: ContainerInternals;
|
|
40
|
+
readonly [TYPE_STATE]?: {
|
|
41
|
+
readonly kind: 'scope';
|
|
42
|
+
readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Add async disposal capability to a container or scope.
|
|
47
|
+
*
|
|
48
|
+
* Enables `await using` syntax by attaching `[Symbol.asyncDispose]` to the target.
|
|
49
|
+
* Disposes owned instances in reverse creation order (LIFO), calling
|
|
50
|
+
* `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` on each instance that implements them.
|
|
51
|
+
*
|
|
52
|
+
* The returned type prevents registration methods from being called on a potentially-disposed container.
|
|
53
|
+
* For scopes, `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` remain available.
|
|
54
|
+
*
|
|
55
|
+
* @param container A Container or Scope to make disposable
|
|
56
|
+
* @returns The same object with `AsyncDisposable` capability added and registration methods removed from the type
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* import { createContainer } from 'katagami';
|
|
61
|
+
* import { disposable } from 'katagami/disposable';
|
|
62
|
+
*
|
|
63
|
+
* await using container = disposable(
|
|
64
|
+
* createContainer().registerSingleton(DB, () => new Database())
|
|
65
|
+
* );
|
|
66
|
+
* ```
|
|
67
|
+
*/
|
|
68
|
+
export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(container: Container<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableContainer<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
69
|
+
export declare function disposable<T, Sync extends AbstractConstructor, Async extends AbstractConstructor, ScopedT, ScopedSync extends AbstractConstructor, ScopedAsync extends AbstractConstructor>(scope: Scope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>): DisposableScope<T, Sync, Async, ScopedT, ScopedSync, ScopedAsync>;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import type { Container } from '../container';
|
|
2
|
-
import { type ContainerInternals, INTERNALS } from '../internal';
|
|
3
|
-
import type { AbstractConstructor, Resolver } from '../resolver';
|
|
4
|
-
import type { Scope } from '../scope';
|
|
1
|
+
import type { Container } from '../container/index.js';
|
|
2
|
+
import { type ContainerInternals, INTERNALS, type TYPE_STATE } from '../internal.js';
|
|
3
|
+
import type { AbstractConstructor, Resolver } from '../resolver/index.js';
|
|
4
|
+
import type { Scope } from '../scope/index.js';
|
|
5
5
|
/**
|
|
6
6
|
* A container wrapped with `disposable()`.
|
|
7
7
|
*
|
|
8
|
-
* Only `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` are available at the type level.
|
|
9
8
|
* Registration methods (`registerSingleton`, `registerTransient`, `registerScoped`, `use`)
|
|
10
9
|
* are excluded, preventing accidental registration on a potentially-disposed container.
|
|
10
|
+
* Use `createScope()` to create a scope for resolution.
|
|
11
11
|
*
|
|
12
12
|
* @template T PropertyKey-based token type map
|
|
13
13
|
* @template Sync Union of registered sync class constructors
|
|
@@ -16,8 +16,12 @@ import type { Scope } from '../scope';
|
|
|
16
16
|
* @template ScopedSync Union of scoped sync class constructors
|
|
17
17
|
* @template ScopedAsync Union of scoped async class constructors
|
|
18
18
|
*/
|
|
19
|
-
export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never,
|
|
19
|
+
export interface DisposableContainer<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends AsyncDisposable {
|
|
20
20
|
readonly [INTERNALS]: ContainerInternals;
|
|
21
|
+
readonly [TYPE_STATE]?: {
|
|
22
|
+
readonly kind: 'container';
|
|
23
|
+
readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
|
|
24
|
+
};
|
|
21
25
|
}
|
|
22
26
|
/**
|
|
23
27
|
* A scope wrapped with `disposable()`.
|
|
@@ -33,6 +37,10 @@ export interface DisposableContainer<T = Record<never, never>, Sync extends Abst
|
|
|
33
37
|
*/
|
|
34
38
|
export interface DisposableScope<T = Record<never, never>, Sync extends AbstractConstructor = never, Async extends AbstractConstructor = never, ScopedT = Record<never, never>, ScopedSync extends AbstractConstructor = never, ScopedAsync extends AbstractConstructor = never> extends Resolver<T & ScopedT, Sync | ScopedSync, Async | ScopedAsync>, AsyncDisposable {
|
|
35
39
|
readonly [INTERNALS]: ContainerInternals;
|
|
40
|
+
readonly [TYPE_STATE]?: {
|
|
41
|
+
readonly kind: 'scope';
|
|
42
|
+
readonly registrations: readonly [T, Sync, Async, ScopedT, ScopedSync, ScopedAsync];
|
|
43
|
+
};
|
|
36
44
|
}
|
|
37
45
|
/**
|
|
38
46
|
* Add async disposal capability to a container or scope.
|
|
@@ -41,8 +49,8 @@ export interface DisposableScope<T = Record<never, never>, Sync extends Abstract
|
|
|
41
49
|
* Disposes owned instances in reverse creation order (LIFO), calling
|
|
42
50
|
* `[Symbol.asyncDispose]()` or `[Symbol.dispose]()` on each instance that implements them.
|
|
43
51
|
*
|
|
44
|
-
* The returned type
|
|
45
|
-
*
|
|
52
|
+
* The returned type prevents registration methods from being called on a potentially-disposed container.
|
|
53
|
+
* For scopes, `resolve`, `tryResolve`, `resolveAll`, and `tryResolveAll` remain available.
|
|
46
54
|
*
|
|
47
55
|
* @param container A Container or Scope to make disposable
|
|
48
56
|
* @returns The same object with `AsyncDisposable` capability added and registration methods removed from the type
|
package/dist/disposable/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown by the DI container.
|
|
3
|
+
*
|
|
4
|
+
* Represents failures in container operations such as resolving an unregistered token.
|
|
5
|
+
*/
|
|
6
|
+
export declare class ContainerError extends Error {
|
|
7
|
+
/**
|
|
8
|
+
* @param message Error message
|
|
9
|
+
*/
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|