Documentation
    Preparing search index...

    Class Container<KnownBindings>

    The container class exposes the API to register bindings, values and resolve them.

    Known bindings types can be defined at the time of the constructing the container.

    new Container<{ 'route': Route, encryption: Encryption }>()
    

    You can resolve bindings and construct classes as follows

    await container.make(BINDING_NAME)
    await container.make(CLASS_CONSTRUCTOR)

    Type Parameters

    • KnownBindings extends Record<any, any>
    Index

    Constructors

    Methods

    • Create a container resolver to resolve bindings, or make classes.

      const resolver = container.createResolver()
      await resolver.make(CLASS_CONSTRUCTOR)

      Bind values with the resolver. Resolver values are isolated from the container.

      resolver.bindValue(HttpContext, new HttpContext())
      await resolver.make(UsersController)

      Returns ContainerResolver<KnownBindings>

      A new container resolver instance

    • Find if the container has a binding registered using the "bind", the "singleton", or the "bindValue" methods.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns boolean

      True if binding exists, false otherwise

    • Find if the container has a binding registered using the "bind", the "singleton", or the "bindValue" methods.

      Parameters

      Returns boolean

      True if binding exists, false otherwise

    • Find if the container has all the bindings registered using the "bind", the "singleton", or the "bindValue" methods.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns boolean

      True if all bindings exist, false otherwise

    • Find if the container has all the bindings registered using the "bind", the "singleton", or the "bindValue" methods.

      Parameters

      Returns boolean

      True if all bindings exist, false otherwise

    • Resolves the binding or constructor a class instance as follows.

      • Resolve the binding from the values (if registered)
      • Resolve the binding from the bindings (if registered)
      • If binding is a class, then create a instance of it. The constructor dependencies are further resolved as well.
      • All other values are returned as it is.
      await container.make('route')
      await container.make(Database)

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns Promise<
          Binding extends string
          | symbol
              ? KnownBindings[Binding<Binding>]
              : Make<Binding>,
      >

    • Resolves the binding or constructor a class instance as follows.

      • Resolve the binding from the values (if registered)
      • Resolve the binding from the bindings (if registered)
      • If binding is a class, then create a instance of it. The constructor dependencies are further resolved as well.
      • All other values are returned as it is.
      await container.make('route')
      await container.make(Database)

      Type Parameters

      • Binding

      Parameters

      Returns Promise<Make<Binding>>

    • Call a method on an object by injecting its dependencies. The method dependencies are resolved in the same manner as a class constructor dependencies.

      await container.call(await container.make(UsersController), 'index')
      

      Type Parameters

      • Value extends Record<any, any>
      • Method extends string | number | symbol

      Parameters

      Returns Promise<ReturnType<Value[Method]>>

    • Register an alias for a binding. The value can be a reference to an existing binding or to a class constructor that will instantiate to the same value as the alias.

      Type Parameters

      • Alias extends string | number | symbol

      Parameters

      • alias: Alias extends string | symbol ? Alias<Alias> : never

        An alias must always be defined as a string or a symbol. Classes cannot be aliases

      • value:
            | AbstractConstructor<KnownBindings[Alias]>
            | Exclude<
                {
                    [K in string
                    | number
                    | symbol]: KnownBindings[K] extends KnownBindings[Alias] ? K : never
                }[keyof KnownBindings],
                Alias,
            >

        The value should either be the constructor point to the alias value or reference to binding that has the same value as the alias

      Returns void

      Sets the alias for the binding

    • Register a binding inside the container. The method receives a key-value pair.

      • Key can be a string, symbol or a constructor.
      • The value is always a factory function to construct the dependency.
      container.bind('route', () => new Route())
      await container.make('route')

      container.bind(Route, () => new Route())
      await container.make(Route)

      const routeSymbol = Symbol('route')
      container.bind(routeSymbol, () => new Route())
      await container.make(routeSymbol)

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns void

    • Register a binding inside the container. The method receives a key-value pair.

      • Key can be a string, symbol or a constructor.
      • The value is always a factory function to construct the dependency.
      container.bind('route', () => new Route())
      await container.make('route')

      container.bind(Route, () => new Route())
      await container.make(Route)

      const routeSymbol = Symbol('route')
      container.bind(routeSymbol, () => new Route())
      await container.make(routeSymbol)

      Type Parameters

      • Binding extends AbstractConstructor<any>

      Parameters

      Returns void

    • Register a binding as a value

      container.bindValue(Route, new Route())
      

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns void

    • Register a binding as a value

      container.bindValue(Route, new Route())
      

      Type Parameters

      • Binding extends AbstractConstructor<any>

      Parameters

      Returns void

    • Register a binding as a single. The singleton method is same as the bind method, but the factory function is invoked only once.

      container.singleton('route', () => new Route())
      await container.make('route')

      container.singleton(Route, () => new Route())
      await container.make(Route)

      const routeSymbol = Symbol('route')
      container.singleton(routeSymbol, () => new Route())
      await container.make(routeSymbol)

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      Returns void

    • Register a binding as a single. The singleton method is same as the bind method, but the factory function is invoked only once.

      container.singleton('route', () => new Route())
      await container.make('route')

      container.singleton(Route, () => new Route())
      await container.make(Route)

      const routeSymbol = Symbol('route')
      container.singleton(routeSymbol, () => new Route())
      await container.make(routeSymbol)

      Type Parameters

      • Binding extends AbstractConstructor<any>

      Parameters

      Returns void

    • Restore binding by removing its swap

      Parameters

      • binding: AbstractConstructor<any>

      Returns void

    • Restore mentioned or all bindings by removing their swaps

      Parameters

      • Optionalbindings: AbstractConstructor<any>[]

      Returns void

    • Add a contextual binding for a given class constructor. A contextual takes a parent, parent's dependency and a callback to self resolve the dependency.

      For example:

      • When "UsersController"
      • Asks for "Hash class"
      • Provide "Argon2" implementation

      Type Parameters

      • Binding extends AbstractConstructor<any>

      Parameters

      Returns void