DI container that supports extension bundles. See addExtension and getExtension.

For more information, refer to BundleInterface.

Hierarchy (view full)

Constructors

Properties

parameters: Record<string, any> = {}
dependencies: Map<ClassType<any>, ClassType<any>[]> = ...
services: Map<ClassType<any>, any> = ...
overides: Map<ClassType<any>, ClassType<any>> = ...
#serviceLifetimes: Map<ClassType<any>, Lifetime> = ...
#compiled: boolean = false
extensions: Map<ClassType<any>, BundleInterface<any>> = ...
extensionConfig: Map<ClassType<any>, any> = ...
#extTypeMap: Map<string, ClassType<BundleInterface<any>>> = ...

Methods

  • Registers a class as a service. You can also use singleton and transient.

    Throws ArgumentCountError if the given dependencies do not match the required constructor arguments.

    Throws CyclicalDependencyError if there is a self-referencing constructor argument.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    • Optional lifetime: Lifetime = Lifetime.SHARED

    Returns this

    Example

    // Logger depends on some log level constant
    container.register(LoggerService, [LogLevel.DEBUG]);

    // Database depends on Logger
    container.register(Database, [LoggerService]);
  • Registers a class as a singleton service.

    Shortcut for register with Lifetime.SHARED.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    Returns this

  • Registers a class as a transient service.

    Shortcut for register with Lifetime.TRANSIENT.

    Type Parameters

    Parameters

    • ctor: T

      The service to register - can be any class.

    • dependencies: MapDependencies<ConstructorParameters<T>>

      Dependencies (constructor parameters) of the service

    Returns this

  • Overrides a service with another concrete implementation.

    Note: Javascript does not support interfaces (i.e. you cannot pass a TS interface by value). Therefore, you should first register() a 'base class' as a default implementation, after which you can override it using override().

    You may omit the 'dependencies' argument to match the constructor signature of the overridden class.

    Type Parameters

    Parameters

    • ctor: T
    • overrideCtor: T
    • Optional dependencies: MapDependencies<ConstructorParameters<T>>

    Returns this

    Example

    class IFoo {
    someMethod(): void {}
    }

    // First register the default implementation / 'base class' for IFoo.
    container.transient(IFoo, []);

    container.singleton(MyService, [IFoo]); // MyService depends on IFoo

    container.transient(ConcreteFoo, []); // Register an override service
    container.override(IFoo, ConcreteFoo); // ConcreteFoo will be passed to MyService
  • Retrieves an extension bundle from the container.

    Type Parameters

    Parameters

    Returns undefined | T

    Example

    // Basic usecase 
    import { MyBundle } from '.';
    const ext = container.getExtension(MyBundle);

    // Or use `import type` to aid tree-shaking
    import type { MyBundle } from '.';
    const ext = container.getExtension<MyBundle>('MyBundle');
  • Set a superglobal.

    Can be useful for general application config. Only use this if the parameter is 'global', shared across multiple services or bundles.

    For bundles specifically, if your parameter only applies to a single module (in a bundle), you should use the bundle's config, see BundleInterface.

    Parameters

    • key: string
    • value: any

    Returns this

  • Get a superglobal.

    Can be useful for general application config. Only use this if the parameter is 'global', shared across multiple services or bundles.

    For bundles specifically, if your parameter only applies to a single module (in a bundle), you should use the bundle's config, see BundleInterface.

    Type Parameters

    • T = any

    Parameters

    Returns T

Generated using TypeDoc