Skip to main content
This release is 2 versions behind 0.2.1 — the latest version of @findhow/container. Jump to latest

Built and signed on GitHub Actions

Wasn't a fan of using decorators to dependency inject things in the TypeScript world. Now you don't have to.

This package works with Node.js, DenoIt is unknown whether this package works with Cloudflare Workers, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score
100%
Published
a week ago (0.1.0)
class DIContainer
implements IDIContainer

Dependency Injection Container

The DIContainer class provides a way to manage dependencies and their lifecycles. It supports various binding types, contextual bindings, and middleware.

Properties

contextualBindings: Map<Bindable, ContextualBinding[]>
private
aliases: Map<string | symbol, Bindable>
private
bindings: Map<Bindable, Binding>
private
instances: Map<Bindable, any>
private
middlewares: Middleware[]
private
parent: DIContainer | null
private
resolvingStack: Bindable[]
private
tags: Map<string, Bindable[]>

Methods

alias(
alias: string | symbol,
abstract: Bindable,
): void

Alias an abstract type.

private
applyMiddleware(factory: () => any): any
bind(
abstract: Bindable,
factory: Function,
schema?: z.ZodType<any>,
): this

Bind a type to a factory function.

private
build(
binding: Binding,
context?: Bindable,
): any
createChild(): IDIContainer

Create a child container.

createInstance<T>(Target: Bindable & WithParamTypes): T

Create an instance of a class with dependencies.

createScope(): IDIContainer

Create a new scope.

dispose(): void

Dispose of the container and its bindings.

private
getAlias(abstract: Bindable): Bindable
instance(
abstract: Bindable,
instance: any,
): this

Bind an instance.

lazyBind<T>(
token: any,
factory: () => T,
): void

Lazy bind a type to a factory function.

resolve<T = any>(
abstract: Bindable,
context?: Bindable,
): T

Resolve a type.

resolveAsync<T = any>(
abstract: Bindable,
context?: Bindable,
): Promise<T>

Resolve a type asynchronously.

private
resolveWithContext(
abstract: Bindable,
context: Bindable,
): any
scoped(
abstract: Bindable,
factory: Function,
): this

Bind a scoped instance.

singleton(
abstract: Bindable,
factory: Function,
): this

Bind a singleton.

tag(
abstracts: Bindable[],
tag: string,
): this

Tag multiple abstracts with a tag.

tagged(tag: string): any[]
transient(
abstract: Bindable,
factory: Function,
): this

Bind a transient.

use(middleware: Middleware): void

Use a middleware function.

when(concrete: Bindable): IContextualBindingBuilder

Create a contextual binding.

Add Package

deno add jsr:@findhow/container

Import symbol

import { DIContainer } from "@findhow/container/dicontainer";

---- OR ----

Import directly with a jsr specifier

import { DIContainer } from "jsr:@findhow/container/dicontainer";

Add Package

npx jsr add @findhow/container

Import symbol

import { DIContainer } from "@findhow/container/dicontainer";

Add Package

yarn dlx jsr add @findhow/container

Import symbol

import { DIContainer } from "@findhow/container/dicontainer";

Add Package

pnpm dlx jsr add @findhow/container

Import symbol

import { DIContainer } from "@findhow/container/dicontainer";

Add Package

bunx jsr add @findhow/container

Import symbol

import { DIContainer } from "@findhow/container/dicontainer";