Skip to main content

Utility Functions

@brushy/di provides several utility functions to facilitate the use of the dependency injection system in different contexts.

Typed tokens

Use createToken<T>() to create tokens with automatic type inference in register, resolve, inject, and useInject.

Import

Basic usage

Class as token

defineModule

Legacy tokens (Symbol / string) still work but require a manual generic: resolve<MyType>(TOKEN).

Request Scope (AsyncLocalStorage)

On Node.js, @brushy/di binds the active HTTP request scope with AsyncLocalStorage. Scoped dependencies resolve automatically inside that scope.

Import

Express middleware

Scripts and tests

Use isRequestScopeSupported() to detect Node.js ALS. On React Native, pass an explicit scope in InjectOptions or use BrushyDIProvider context.

resolve

The resolve function allows resolving dependencies globally, without needing to directly access the container.

Import

Basic Usage

With Scope

inject

The inject object provides utilities for dependency injection in any context.

Import

Global Container Configuration

Dependency Resolution

cache

The cache object provides utilities for managing promise caching.

Import

Clear Cache

monitor

The monitor object provides utilities for monitoring and observing container behavior.

Import

Create Monitor

BrushyDIProvider

The BrushyDIProvider component is used to provide a DI container to React components.

Import

Basic Usage

With Scope

Complete Example