@axonlabs/core - v0.13.0
    Preparing search index...

    Class NeuronContainerExperimental

    NeuronContainer is a flexible and performant dependency injection system. Supports aliasing, hybrid string/class tokens, async registration, and lifecycles.

    • Singleton: Shared instance for entire app lifetime.
    • Transient: New instance on every request.
    • Scoped: New instance per logical scope/request.

    This is an experimental feature.

    0.5.1

    0.13.0

    Index

    Constructors

    Methods

    • Experimental

      Check status of a dependency in scope or outside of a scope.

      Parameters

      • key: string | Function

        Key or alias of dependency

      • OptionalscopeId: string

        The ID of scope that you want to resolve

      Returns boolean

      Throw error if something wrong and return true if everything was ok

    • Experimental

      Clear all scoped instances for a specific scope ID.

      Parameters

      • scopeId: string

        The ID of scope that you want to clear

      Returns void

      container.clearScope(req.id);
      
    • Experimental

      Inspect metadata of a registered dependency.

      Parameters

      • key: string | Function

        Key or alias of dependency

      Returns
          | null
          | {
              isFactory: boolean;
              key: string
              | Function;
              lifecycle: Lifecycle;
              resolved: boolean;
          }

      metadata of dependency

      container.inspect('db');
      
    • Experimental

      List all registered dependency keys.

      Returns (string | Function)[]

      Array of dependencies

      console.log(container.listDependencies());
      
    • Experimental

      Override a registered dependency with a new singleton and non-factory value.

      Type Parameters

      • T

      Parameters

      • key: string | Function

        Key or alias of an exist dependency

      • value: T

        value of dependency

      Returns void

      container.override('logger', customLogger);
      
    • Experimental

      Main registration method used internally by value/factory variants.

      Type Parameters

      • T

      Parameters

      • keys: string | Function | string[]

        String, array of string or Function key for the dependency

      • value: T | (() => T | Promise<T>)

        Value to inject as dependency value

      • options: { isFactory: boolean; lifecycle?: Lifecycle }

        Some options for configuring the dependency

      Returns void

    • Experimental

      Register a factory function that creates the dependency (sync or async).

      Type Parameters

      • T

      Parameters

      • keys: string | Function | string[]

        String, array of string or Function key for the dependency

      • factory: () => T | Promise<T>

        Factory function or something that must run to return the instance of dependency

      • options: { lifecycle?: Lifecycle } = {}

        Some options for configuring the dependency

      Returns void

      container.registerFactory('db', () => new DB())
      container.registerFactory('auth', async () => await AuthService.build())
    • Experimental

      Register a static value or instance (object, function, class instance).

      Type Parameters

      • T

      Parameters

      • keys: string | Function | string[]

        String, array of string or Function key for the dependency

      • value: T

        Function, object or instance to register as dependency value

      • options: { lifecycle?: Lifecycle } = {}

        Some options for configuring the dependency

      Returns void

      container.registerValue('logger', new Logger())
      container.registerValue('config', { port: 3000 })
    • Experimental

      Resolve a dependency, respecting its lifecycle (singleton, scoped, transient).

      Type Parameters

      • T

      Parameters

      • key: string | Function

        Key or alias of dependency

      • OptionalscopeId: string

        The ID of scope that you want to resolve

      Returns Promise<T>

      value or instance of the dependency

      const logger = await container.resolve('logger')
      const auth = await container.resolve('auth', req.id)
    • Experimental

      Shortcut for resolve(), mostly for user-facing code.

      Type Parameters

      • T

      Parameters

      • key: string | Function

        Key or alias of dependency

      • OptionalscopeId: string

        The ID of scope that you want to resolve

      Returns Promise<T>

      value or instance of the dependency

      const db = await container.use(DBService);