Interface: SealedDIContainer<S>
Defined in: src/types.ts:894
A DIContainer after seal(): it resolves services, runs middlewares and events, resets and disposes, but registers nothing. Its forks are regular, open containers.
Example
const app: SealedDIContainer<{ db: Database }> = new DIContainer().addSingleton('db', createDb).seal();
app.get('db');
app.fork().addInstance('requestId', id);Extends
Omit<DIContainer<S>,RegistrationMethod|ChainedMethod>
Type Parameters
S
S extends object = { }
Properties
keys
keys: readonly
ServiceKey[]
Defined in: src/container.ts:269
Inherited from
ownKeys
ownKeys: readonly
ServiceKey[]
Defined in: src/container.ts:276
Inherited from
Methods
[asyncDispose]()
[asyncDispose]():
Promise<void>
Defined in: src/container.ts:731
Same as dispose; enables await using container = ….
Returns
Promise<void>
Inherited from
Omit.[asyncDispose]
addEventListener()
addEventListener<
E>(event,handler):this
Defined in: src/types.ts:903
Subscribes to container events.
Type Parameters
E
E extends keyof ContainerEvents
Parameters
event
E
handler
(event) => void
Returns
this
bind()
bind<
D,R>(dependencies,factory): () =>R
Defined in: src/container.ts:551
Returns a function that runs factory with freshly resolved dependencies on every call.
Type Parameters
D
D extends Dependency<{ }, S>[]
R
R
Parameters
dependencies
[...D[]]
factory
((...args) => R) | (new (...args) => R)
Returns
() => R
Example
const sendReport = app.bind(['mailer', 'reports'], (mailer, reports) => mailer.send(reports.weekly()));
sendReport();Inherited from
Omit.bind
call()
call<
K>(key,args,thisArg?):ReturnType<Extract<ServiceType<S,K>, (...args) =>any>>
Defined in: src/container.ts:253
Resolves a function service and calls it.
Type Parameters
K
K extends string | number | symbol
Parameters
key
K | TaggedKey<any, K>
args
Parameters<Extract<ServiceType<S, K>, (...args) => any>>
thisArg?
unknown = undefined
Returns
ReturnType<Extract<ServiceType<S, K>, (...args) => any>>
Example
app.addInstance('greet', (name: string) => `Hello ${name}`);
app.call('greet', ['Ada']); // "Hello Ada"Inherited from
Omit.call
createResolver()
createResolver<
K>(key): () =>ServiceType<S,K>
Defined in: src/container.ts:238
Returns a function that resolves key when called.
Type Parameters
K
K extends string | number | symbol
Parameters
key
K | TaggedKey<any, K>
Returns
() => ServiceType<S, K>
Inherited from
Omit.createResolver
dispose()
dispose():
Promise<void>
Defined in: src/container.ts:725
Disposes every instance this container owns, dependents first (reverse creation order), then marks the container as disposed: later get() and registrations throw.
Owned instances are the singletons this container created, and addInstance values registered with dispose. Each is disposed with its registration's dispose function, or with [Symbol.asyncDispose] / [Symbol.dispose] when it has one. Namespace containers are disposed too; forks are not (dispose each fork you create, e.g. with await using).
Calling it again returns the same promise. If some disposers fail, the others still run and the promise rejects with an AggregateError.
Returns
Promise<void>
Example
await using scope = app.fork().addSingleton('tx', (db) => db.begin(), ['db']);
// ... scope and its singletons (the transaction) are disposed at the end of the blockInherited from
Omit.dispose
fork()
fork(
options?):DIContainer<S>
Defined in: src/container.ts:674
Creates a child container. The child sees every service of this container (and later additions to it); services added to the child stay in the child.
- By default a service registered in this container runs here and is shared by all forks, so overriding one of its dependencies in a fork does not affect it.
- With
isolated: truethe fork runs and caches every service it resolves, including ones registered here. Overrides in the fork then reach the whole graph, and nothing leaks back. Use it for tests and per-tenant variants.
Services registered here can't depend on what a fork adds. A fork for one unit of work (a session, a request, a transaction, a job) is a scope: register its services in it with a scope module, and dispose it with await using. The "Scoped state" guide compares scopes with patterns that keep every service here (an argument, a factory with using, a context accessor).
Parameters
options?
Returns
DIContainer<S>
Example
await using requestScope = app.fork().addSingleton('tx', (db) => db.begin(), ['db']);
const testContainer = app.fork({ isolated: true }).addInstance('db', fakeDb, { replace: true });
testContainer.get('users'); // built with fakeDb; app is untouchedInherited from
Omit.fork
get()
get<
K,O>(key,options?):Oextendsobject?ServiceType<S,K> |undefined:ServiceType<S,K>
Defined in: src/container.ts:220
Resolves a service: returns the cached singleton, creates it, or creates a new transient. Looks in parent containers when the key is not registered here. Throws when nothing is registered under key, unless { optional: true } is passed.
Type Parameters
K
K extends string | number | symbol
O
O extends GetOptions = { }
Parameters
key
K | TaggedKey<any, K>
options?
O
Returns
O extends object ? ServiceType<S, K> | undefined : ServiceType<S, K>
Throws
INJECUTE_NOT_REGISTERED (with "did you mean" suggestions), INJECUTE_RESOLUTION_FAILED when a factory throws (the original error is cause), INJECUTE_CLASS_NOT_CONSTRUCTED, INJECUTE_DISPOSED.
Example
const users = app.get('users');
const cache = app.get('cache', { optional: true }); // Cache | undefinedInherited from
Omit.get
getParent()
getParent():
ServiceProvider<{ }> |undefined
Defined in: src/container.ts:281
The parent container as a read-only view, if any.
Returns
ServiceProvider<{ }> | undefined
Inherited from
Omit.getParent
getRegistration()
getRegistration(
key):RegistrationInfo|undefined
Defined in: src/container.ts:286
Read-only metadata of the registration visible under key, or undefined.
Parameters
key
Returns
RegistrationInfo | undefined
Inherited from
Omit.getRegistration
has()
has(
key,options?):boolean
Defined in: src/container.ts:232
true when a service is registered under key, here or (unless local: true) in a parent.
Parameters
key
options?
Returns
boolean
Inherited from
Omit.has
injecute()
injecute<
D,R>(factory,dependencies):R
Defined in: src/container.ts:531
Runs a function (or constructs a class) with resolved dependencies, without registering it.
Type Parameters
D
D extends Dependency<{ }, S>[]
R
R
Parameters
factory
((...args) => R) | (new (...args) => R)
dependencies
[...D[]]
Returns
R
Example
const handler = app.injecute((users, logger) => createHandler(users, logger), ['users', 'logger']);Inherited from
Omit.injecute
removeEventListener()
removeEventListener<
E>(event,handler):this
Defined in: src/types.ts:908
Removes a listener added with addEventListener().
Type Parameters
E
E extends keyof ContainerEvents
Parameters
event
E
handler
(event) => void
Returns
this
reset()
reset(
options?):this
Defined in: src/types.ts:913
Clears cached instances (all, or keys), so the next resolution creates them again.
Parameters
options?
ResetOptions<S>
Returns
this
unuse()
unuse(
middleware):this
Defined in: src/types.ts:901
Removes a middleware added with use().
Parameters
middleware
Returns
this
use()
use(
middleware):this
Defined in: src/types.ts:899
Adds a middleware around every resolution started from this container and its forks.
Parameters
middleware
Returns
this