AdonisJS API
    Preparing search index...

    Class ContainerResolver<KnownBindings>

    Container resolver exposes the APIs to resolve bindings. You can think of resolver as an isolated container instance, with only the APIs to resolve bindings.

    const container = new Container()
    const resolver = container.createResolver()

    await resolver.make(BINDING_NAME)
    await resolver.make(CLASS_CONSTRUCTOR)

    Type Parameters

    • KnownBindings extends Record<any, any>
    Index
    • Find if the resolver has a binding registered using the "bind", the "singleton", the "bindValue", or the "if" methods.

      A registered binding is not always resolvable. When a binding key only has conditional bindings registered for it, this method returns true, but resolving it still fails if none of the conditions match.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      • binding: Binding

        The binding key to check for

      Returns boolean

      resolver.hasBinding('route')
      resolver.hasBinding(Route)
    • Find if the resolver has a binding registered using the "bind", the "singleton", the "bindValue", or the "if" methods.

      A registered binding is not always resolvable. When a binding key only has conditional bindings registered for it, this method returns true, but resolving it still fails if none of the conditions match.

      Parameters

      • binding: BindingKey

        The binding key to check for

      Returns boolean

      resolver.hasBinding('route')
      resolver.hasBinding(Route)
    • Find if the resolver has all the bindings registered using the "bind", the "singleton", the "bindValue", or the "if" methods.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      • bindings: Binding[]

        Array of binding keys to check for

      Returns boolean

      resolver.hasAllBindings(['route', 'encryption'])
      resolver.hasAllBindings([Route, Encryption])
    • Find if the resolver has all the bindings registered using the "bind", the "singleton", the "bindValue", or the "if" methods.

      Parameters

      • bindings: BindingKey[]

        Array of binding keys to check for

      Returns boolean

      resolver.hasAllBindings(['route', 'encryption'])
      resolver.hasAllBindings([Route, Encryption])
    • Resolves binding in context of a parent. The method is same as the "make" method, but instead takes a parent class constructor. This is used internally for dependency resolution.

      Type Parameters

      • Binding

      Parameters

      • parent: unknown

        The parent class requesting the binding

      • binding: Binding

        The binding to resolve

      • OptionalruntimeValues: any[]

        Optional runtime values for dependencies

      • createError: ErrorCreator = ...

        Error creator function

      Returns Promise<Make<Binding>>

      const db = await resolver.resolveFor(UsersController, Database)
      
    • 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.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      • binding: Binding

        The binding key or class constructor to resolve

      • OptionalruntimeValues: any[]

        Optional runtime values for dependencies

      • OptionalcreateError: ErrorCreator

        Error creator function

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

      await resolver.make('route')
      await resolver.make(Database)
      await resolver.make(UsersController, [request, response])
    • 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.

      Type Parameters

      • Binding

      Parameters

      • binding: Binding

        The binding key or class constructor to resolve

      • OptionalruntimeValues: any[]

        Optional runtime values for dependencies

      • OptionalcreateError: ErrorCreator

        Error creator function

      Returns Promise<Make<Binding>>

      await resolver.make('route')
      await resolver.make(Database)
      await resolver.make(UsersController, [request, response])
    • Call a method on an object by injecting its dependencies. The method dependencies are resolved in the same manner as a class constructor dependencies.

      Type Parameters

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

      Parameters

      • value: Value

        The object instance containing the method

      • method: Method

        The method name to call

      • OptionalruntimeValues: any[]

        Optional runtime values for method dependencies

      • createError: ErrorCreator = ...

        Error creator function

      Returns Promise<ReturnType<Value[Method]>>

      const controller = await resolver.make(UsersController)
      await resolver.call(controller, 'index')
    • Register a binding as a value. Values bound to the resolver are isolated from the container and only available within this resolver instance.

      Type Parameters

      • Binding extends string | number | symbol

      Parameters

      • binding: Binding extends string | symbol ? Binding : never

        The binding key (string, symbol, or class constructor)

      • value: KnownBindings[Binding]

        The pre-resolved value to bind

      Returns void

      const resolver = container.createResolver()
      resolver.bindValue(HttpContext, ctx)
      await resolver.make(UsersController) // Will receive the ctx
    • Register a binding as a value. Values bound to the resolver are isolated from the container and only available within this resolver instance.

      Type Parameters

      • Binding extends AbstractConstructor<any>

      Parameters

      • binding: Binding

        The binding key (string, symbol, or class constructor)

      • value: InstanceType<Binding>

        The pre-resolved value to bind

      Returns void

      const resolver = container.createResolver()
      resolver.bindValue(HttpContext, ctx)
      await resolver.make(UsersController) // Will receive the ctx