API Reference
Reference for the container and decorators provided by @di-framework/core. For package-specific APIs, see Repositories, HTTP Router, GraphQL, Events, Sockets, RPC, Configuration, Authentication, Resource Authorization, and AI.
Decorators
@Container(options?)
Marks a class as injectable and automatically registers it with the DI container.
Options:
singleton?: boolean(default:true) - Create a new instance each time or reuse the same instancecontainer?: DIContainer- Specify a custom container (defaults to global container)
Example:
@Component(target)
Marks a constructor parameter or property for dependency injection.
Parameters:
target- The class to inject or a string identifier for factory-registered services
Example - Constructor Parameter:
Example - Property Injection:
Example - Factory Service:
@Telemetry(options?)
Marks a method for telemetry tracking. When called, it emits a telemetry event on the container. Works with both synchronous and asynchronous methods.
Options:
logging?: boolean(default:false) - If true, logs the method execution details (status and duration) to the console.
Example:
@TelemetryListener()
Marks a method as a listener for telemetry events. The method will be automatically registered to the container's telemetry event when the service is instantiated.
Example:
@Publisher(optionsOrEvent)
Marks a method to publish a custom event on the container upon invocation. This is useful for cross-platform event-driven architectures.
Parameters:
optionsOrEvent- A string representing the event name, or an options object.
Options:
event: string- The custom event name.phase?: "before" | "after" | "both"(default:"after") - When to emit the event relative to method invocation.logging?: boolean(default:false) - Optional console logging for debug purposes.
Example:
@Subscriber(event)
Marks a method to subscribe to a custom event emitted on the container. The decorated method will automatically receive the published payload when the event occurs.
Parameters:
event: string- The custom event name to listen for.
Example:
Container API
Getting a container instance
To interact with the container, you can either obtain the shared global instance via a function, or import the shared instance directly.
Option A — using a function:
Option B — using a named import:
Note: Prefer Option A when you want to make the acquisition explicit in your code examples; both options reference the same singleton instance by default.
useContainer()
Returns the global DI container instance.
Returns: Container
Example:
container.register(serviceClass, options?)
Manually register a service class.
Parameters:
serviceClass- The class to registeroptions?- Registration optionssingleton?: boolean(default:true)
Example:
container.registerFactory(name, factory, options?)
Register a service using a factory function.
Parameters:
name: string- Identifier for the servicefactory: () => T- Factory function that creates the service instanceoptions?- Registration optionssingleton?: boolean(default:true)
Example:
container.resolve(serviceClass)
Resolve and get an instance of a service.
Parameters:
serviceClass- The class or string identifier to resolve
Returns: Instance of the service with all dependencies injected
Example:
container.has(serviceClass)
Check if a service is registered in the container.
Parameters:
serviceClass- The class or string identifier to check
Returns: boolean
Example:
container.getServiceNames()
Get all registered service names.
Returns: string[] - Array of all registered service identifiers
Example:
container.on(event, listener) / container.off(event, listener)
Subscribe to container lifecycle events (observer pattern). Returns an unsubscribe function from on.
Events:
registered-{ key, singleton, kind }resolved-{ key, instance, singleton, fromCache }constructed-{ key, instance, overrides }cleared-{ count }telemetry-{ className, methodName, args, startTime, endTime, result, error }
Example:
container.construct(serviceClass, overrides?)
Create a fresh instance without registering it, while still resolving dependencies. Use overrides to supply specific constructor arguments (by index) such as primitives or config values.
Example:
container.fork(options?)
Clone all registrations into a new container (prototype pattern). Pass { carrySingletons: true } to reuse existing singleton instances; default is to start with fresh instances.
Example:
Lifecycle Methods
Services can optionally implement lifecycle methods that are called by the framework or your application code.
setEnv(env: Record<string, any>)
Called to initialize environment-specific configuration.
Example:
setCtx(context: any)
Called to set execution context (e.g., request context).
Example:
Types
Container
The DI container class.
Next Steps
Advanced Usage - Learn advanced patterns and techniques
Error Handling - Understand error scenarios and how to handle them
Testing - Learn how to test services with di-framework