Class: binding = new Binding()

Properties
Name Type Description
isLocked boolean
key string
scope BindingScope
tags Set
type BindingType
valueConstructor Constructor
PROPERTY_SEPARATOR string

constructor(key: string, isLocked: boolean) : Binding

Arguments
Name Type Description
key string
isLocked boolean

getValue(ctx: Context) : BoundValue | Promise <BoundValue>

This is an internal function optimized for performance. Users should use @inject(key) or ctx.get(key) instead.

Get the value bound to this key. Depending on isSync, this function returns either:

  • the bound value
  • a promise of the bound value

Consumers wishing to consume sync values directly should use isPromise to check the type of the returned value to decide how to handle it.

const result = binding.getValue(ctx);
if (isPromise(result)) {
  result.then(doSomething)
} else {
  doSomething(result);
}

Arguments
Name Type Description
ctx Context

inScope(scope: BindingScope) : this

Arguments
Name Type Description
scope BindingScope

lock() : this

tag(tagName: undefined) : this

Arguments
Name Type Description
tagName

to(value: BoundValue) : this

Bind the key to a constant value.

Arguments
Name Type Description
value BoundValue

The bound value.

toClass(ctor: Constructor) : this

Bind the key to an instance of the given class.

Arguments
Name Type Description
ctor Constructor

The class constructor to call. Any constructor arguments must be annotated with @inject so that we can resolve them from the context.

toDynamicValue(factoryFn: () => BoundValue | Promise <BoundValue> ) : this

Bind the key to a computed (dynamic) value.

Arguments
Name Type Description
factoryFn Function

The factory function creating the value. Both sync and async functions are supported.

toJSON() : Object

toProvider(providerClass: Constructor) : this

Bind the key to a value computed by a Provider.

  • @example

export class DateProvider implements Provider<Date> {
  constructor(@inject('stringDate') private param: String){}
  value(): Date {
    return new Date(param);
  }
}

Arguments
Name Type Description
providerClass Constructor

unlock() : this

parseKeyWithPath(keyWithPath: string) :

Parse a string containing both the binding key and the path to the deeply nested property to retrieve.

Arguments
Name Type Description
keyWithPath string

The key with an optional path, e.g. "application.instance" or "config#rest.port".

validateKey(key: string) : string

Validate the binding key format. Please note that # is reserved.

Arguments
Name Type Description
key string

Binding key, such as `a, a.b, a:b, a/b

BoundValue = any

Class: context = new Context()

Context provides an implementation of Inversion of Control (IoC) container

constructor(_parent: Context) : Context

Create a new context

Arguments
Name Type Description
_parent Context

The optional parent context

bind(key: string) : Binding

Create a binding with the given key in the context. If a locked binding already exists with the same key, an error will be thrown.

Arguments
Name Type Description
key string

Binding key

contains(key: string) : boolean

Check if a binding exists with the given key in the local context without delegating to the parent context

Arguments
Name Type Description
key string

Binding key

find(pattern: undefined) :

Find bindings using the key pattern

Arguments
Name Type Description
pattern

Key regexp or pattern with optional * wildcards

findByTag(pattern: undefined) :

Find bindings using the tag pattern

Arguments
Name Type Description
pattern

Tag name regexp or pattern with optional * wildcards

get(key: string) : Promise

Get the value bound to the given key, optionally return a (deep) property of the bound value.

Returns:

A promise of the bound value.

Arguments
Name Type Description
key string

Context provides an implementation of Inversion of Control (IoC) container

getBinding(key: string) : Binding

Look up a binding by key in the context and its ancestors. If no matching binding is found, an error will be thrown.

Arguments
Name Type Description
key string

Binding key

getSync(key: string) : BoundValue

Get the synchronous value bound to the given key, optionally return a (deep) property of the bound value.

This method throws an error if the bound value requires async computation (returns a promise). You should never rely on sync bindings in production code.

Returns:

A promise of the bound value.

Arguments
Name Type Description
key string

Context provides an implementation of Inversion of Control (IoC) container

getValueOrPromise(keyWithPath: string) : ValueOrPromise

Get the value bound to the given key.

This is an internal version that preserves the dual sync/async result of Binding#getValue(). Users should use get() or getSync() instead.

Returns:

The bound value or a promise of the bound value, depending on how the binding was configured.

Arguments
Name Type Description
keyWithPath string

The binding key, optionally suffixed with a path to the (deeply) nested property to retrieve.

isBound(key: string) : boolean

Check if a key is bound in the context or its ancestors

Arguments
Name Type Description
key string

Binding key

toJSON() : Object

Create a plain JSON object for the context

Interface: Injection

Descriptor for an injection point

Properties
Name Type Description
bindingKey string

Descriptor for an injection point

metadata Function

Descriptor for an injection point

resolve ResolverFunction

Descriptor for an injection point

Interface: ResolverFunction

A function to provide resolution of injected values

Getter(): Promise<T>

The function injected by @inject.getter(key).

Setter(value: T): void

The function injected by @inject.setter(key).

Arguments
Name Type Description
value T

describeInjectedArguments(target: any, method: undefined) :

Return an array of injection objects for parameters

Arguments
Name Type Description
target any

The target class for constructor or static methods, or the prototype for instance methods

method

Method name, undefined for constructor

describeInjectedProperties(target: any) : MetadataMap

Return a map of injection objects for properties

Arguments
Name Type Description
target any

The target class for static properties or prototype for instance properties.

isPromise(value: undefined) : boolean

Check whether a value is a Promise-like instance. Recognizes both native promises and third-party promise libraries.

Arguments
Name Type Description
value

The value to check.

Interface: Provider

Providers allow developers to compute injected values dynamically, with any dependencies required by the value getter injected automatically from the Context.

value() : ValueOrPromise

Returns:

The value to inject to dependents. This method can return a promise too, in which case the IoC framework will resolve this promise to obtain the value to inject.

KV(p: string): BoundValue

Arguments
Name Type Description
p string

instantiateClass(ctor: Constructor, ctx: Context, nonInjectedArgs: undefined) : T | Promise <T>

Create an instance of a class which constructor has arguments decorated with @inject.

The function returns a class when all dependencies were resolved synchronously, or a Promise otherwise.

Arguments
Name Type Description
ctor Constructor

The class constructor to call.

ctx Context

The context containing values for @inject resolution

nonInjectedArgs

Optional array of args for non-injected parameters

invokeMethod(target: any, method: string, ctx: Context, nonInjectedArgs: undefined) : ValueOrPromise

Invoke an instance method with dependency injection

Arguments
Name Type Description
target any

Target of the method, it will be the class for a static method, and instance or class prototype for a prototype method

method string

Name of the method

ctx Context

Context

nonInjectedArgs

Optional array of args for non-injected parameters

resolveInjectedArguments(target: any, ctx: Context, method: string, nonInjectedArgs: undefined) : undefined | Promise <undefined>

Given a function with arguments decorated with @inject, return the list of arguments resolved using the values bound in ctx.

The function returns an argument array when all dependencies were resolved synchronously, or a Promise otherwise.

Arguments
Name Type Description
target any

The class for constructor injection or prototype for method injection

ctx Context

The context containing values for @inject resolution

method string

The method name. If set to '', the constructor will be used.

nonInjectedArgs

Optional array of args for non-injected parameters

resolveInjectedProperties(fn: Function, ctx: Context) : KV | Promise <KV>

Arguments
Name Type Description
fn Function
ctx Context