Page Contents

Home > @loopback/context

context package

@loopback/context

Classes

Class Description
Binding Binding represents an entry in the Context. Each binding has a key and a corresponding value getter.
BindingKey  
Context Context provides an implementation of Inversion of Control (IoC) container
ContextView ContextView provides a view for a given context chain to maintain a live list of matching bindings and their resolved values within the context hierarchy.This class is the key utility to implement dynamic extensions for extension points. For example, the RestServer can react to controller bindings even they are added/removed/updated after the application starts.ContextView is an event emitter that emits the following events: - ‘close’: when the view is closed (stopped observing context events) - ‘refresh’: when the view is refreshed as bindings are added/removed - ‘resolve’: when the cached values are resolved and updated
DefaultConfigurationResolver Resolver for configurations of bindings
InterceptionHandler A proxy handler that applies interceptorsSee https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy
InvocationContext InvocationContext represents the context to invoke interceptors for a method. The context can be used to access metadata about the invocation as well as other dependencies.
ResolutionSession Object to keep states for a session to resolve bindings and their dependencies within a context

Enumerations

Enumeration Description
BindingCreationPolicy Policy to control if a binding should be created for the context
BindingScope Scope for binding values
BindingType Type of the binding source

Functions

Function Description
asBindingTemplate(scopeAndTags) Convert binding scope and tags as a template function
asClassOrProvider(target) A factory function to create a template function to bind the target class as a class or Provider.
asGlobalInterceptor(group) The BindingTemplate function to configure a binding as a global interceptor by tagging it with ContextTags.INTERCEPTOR
asProvider(target) A factory function to create a template function to bind the target class as a Provider.
asResolutionOptions(optionsOrSession) Normalize ResolutionOptionsOrSession to ResolutionOptions
assertTargetType(injection, expectedType, expectedTypeName) Assert the target type inspected from TypeScript for injection to be the expected type. If the types don’t match, an error is thrown.
bind(specs) Decorate a class with binding configuration
bindingTemplateFor(cls) Get the binding template for a class with binding metadata
compareBindingsByTag(phaseTagName, orderOfPhases) Creates a binding compare function to sort bindings by tagged phase name.
compareByOrder(a, b, order) Compare two values by the predefined order
config(configPath, metadata) Inject a property from config of the current binding. If no corresponding config value is present, undefined will be injected as the configuration binding is resolved with optional: true by default.
configBindingKeyFor(key, configPath) Create binding key for configuration of the binding
createBindingFromClass(cls, options) Create a binding from a class with decorated metadata. The class is attached to the binding as follows: - binding.toClass(cls): if cls is a plain class such as MyController - binding.toProvider(cls): it cls is a value provider class with a prototype method value()
createProxyWithInterceptors(target, context) Create a proxy that applies interceptors for method invocations
createViewGetter(ctx, bindingFilter, session) Create a context view as a getter with the given filter
createViewGetter(ctx, bindingFilter, bindingComparator, session) Create a context view as a getter with the given filter and sort matched bindings by the comparator.
describeInjectedArguments(target, method) Return an array of injection objects for parameters
describeInjectedProperties(target) Return a map of injection objects for properties
filterByKey(keyPattern) Create a binding filter from key pattern
filterByTag(tagPattern) Create a binding filter for the tag pattern
getBindingMetadata(target) Get binding metadata for a class
getDeepProperty(value, path) Get nested properties of an object by path
globalInterceptor(group, specs) @globalInterceptor decorator to mark the class as a global interceptor
inject(bindingSelector, metadata, resolve) A decorator to annotate method arguments for automatic injection by LoopBack IoC container.
inspectTargetType(injection) Inspect the target type for the injection to find out the corresponding JavaScript type
instantiateClass(ctor, ctx, session, nonInjectedArgs) 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.
intercept(interceptorOrKeys) Decorator function @intercept for classes/methods to apply interceptors. It can be applied on a class and its public methods. Multiple occurrences of @intercept are allowed on the same target class or method. The decorator takes a list of interceptor functions or binding keys.
invokeMethod(target, method, ctx, nonInjectedArgs) Invoke an instance method with dependency injection
invokeMethodWithInterceptors(context, target, methodName, args) Invoke a method with the given context
isBindingAddress(bindingSelector) Type guard for binding address
isPromiseLike(value) Check whether a value is a Promise-like instance. Recognizes both native promises and third-party promise libraries.
isProviderClass(cls) Check if a class implements Provider interface
mergeInterceptors(interceptorsFromSpec, existingInterceptors) Adding interceptors from the spec to the front of existing ones. Duplicate entries are eliminated from the spec side.For example:- [log] + [cache, log] => [cache, log] - [log] + [log, cache] => [log, cache] - [] + [cache, log] => [cache, log] - [cache, log] + [] => [cache, log] - [log] + [cache] => [log, cache]
removeNameAndKeyTags(binding) A binding template function to delete name and key tags
resolveInjectedArguments(target, method, ctx, session, nonInjectedArgs) 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.
resolveInjectedProperties(constructor, ctx, session) Given a class with properties decorated with @inject, return the map of properties resolved using the values bound in ctx.The function returns an argument array when all dependencies were resolved synchronously, or a Promise otherwise.
resolveList(list, resolver) Resolve entries of an array into a new array with the same indexes. If one or more entries of the source array are resolved to a promise by the resolver function, this method returns a promise which will be resolved to the new array with fully resolved entries.
resolveMap(map, resolver) Resolve entries of an object into a new object with the same keys. If one or more entries of the source object are resolved to a promise by the resolver function, this method returns a promise which will be resolved to the new object with fully resolved entries.
resolveUntil(source, resolver, evaluator) Resolve an iterator of source values into a result until the evaluator returns true
sortBindingsByPhase(bindings, phaseTagName, orderOfPhases) Sort bindings by phase names denoted by a tag and the predefined order
transformValueOrPromise(valueOrPromise, transformer) Transform a value or promise with a function that produces a new value or promise
tryWithFinally(action, finalAction) Try to run an action that returns a promise or a value

Interfaces

Interface Description
BindingComparator Compare function to sort an array of bindings. It is used by Array.prototype.sort().
BindingElement Wrapper for bindings tracked by resolution sessions
ConfigurationResolver Resolver for configuration of bindings. It’s responsible for finding corresponding configuration for a given binding key.By default, undefined is expected if no configuration is provided. The behavior can be overridden by setting optional to false in resolution options.
ContextObserver Observers of context bind/unbind events
InjectBindingMetadata Metadata for @inject.binding
Injection Descriptor for an injection point
InjectionElement Wrapper for injections tracked by resolution sessions
InjectionMetadata An object to provide metadata for @inject
Interceptor Interceptor function to intercept method invocations
Provider Providers allow developers to compute injected values dynamically, with any dependencies required by the value getter injected automatically from the Context.
ResolutionOptions Options for binding/dependency resolution
ResolverFunction A function to provide resolution of injected values
Subscription Subscription of context events. It’s modeled after https://github.com/tc39/proposal-observable.

Namespaces

Namespace Description
bind  
config  
ContextBindings Namespace for context bindings
ContextTags Namespace for context tags
Getter  
inject  

Variables

Variable Description
BINDING_METADATA_KEY Metadata key for binding metadata
DEFAULT_TYPE_NAMESPACES  
GLOBAL_INTERCEPTOR_NAMESPACE Default namespace for global interceptors
INTERCEPT_CLASS_KEY Metadata key for method-level interceptors
INTERCEPT_METHOD_KEY Metadata key for method-level interceptors

Type Aliases

Type Alias Description
AsInterceptedFunction The intercepted variant of a function to return ValueOrPromise<T>. If T is not a function, the type is T.
AsValueOrPromise Create the Promise type for T. If T extends Promise, the type is T, otherwise the type is ValueOrPromise<T>.
AsyncProxy The proxy type for T. The return type for any method of T with original return type R becomes ValueOrPromise<R> if R does not extend Promise. Property types stay untouched.
BindingAddress  
BindingFilter A function that filters bindings. It returns true to select a given binding.
BindingFromClassOptions Options to customize the binding created from a class
BindingMetadata Binding metadata from @bind
BindingScopeAndTags An object to configure binding scope and tags
BindingSelector Select binding(s) by key or a filter function
BindingSpec Specification of parameters for @bind()
BindingTag Binding tag can be a simple name or name/value pairs
BindingTemplate A function as the template to configure bindings
BoundValue  
Constructor A class constructor accepting arbitrary arguments.
ContextEventObserver Context event observer type - An instance of ContextObserver or a function
ContextEventType Context event types. We support bind and unbind for now but keep it open for new types
ContextObserverFn Listen on bind, unbind, or other events
Getter The function injected by @inject.getter(bindingSelector). It can be used to fetch bound value(s) from the underlying binding(s). The return value will be an array if the bindingSelector is a BindingFilter function.
InterceptorOrKey Interceptor function or binding key that can be used as parameters for @intercept()
InvocationArgs Array of arguments for a method invocation
InvocationResult Return value for a method invocation
MapObject  
Notification Event data for observer notifications
ResolutionAction A function to be executed with the resolution session
ResolutionElement Binding or injection elements tracked by resolution sessions
ResolutionOptionsOrSession Resolution options or session
Setter The function injected by @inject.setter(bindingKey). It sets the underlying binding to a constant value using binding.to(value).
TagMap  
TypeNamespaceMapping Mapping artifact types to binding key namespaces (prefixes).
ValueOrPromise Representing a value or promise. This type is used to represent results of synchronous/asynchronous resolution of values.Note that we are using PromiseLike instead of native Promise to describe the asynchronous variant. This allows producers of async values to use any Promise implementation (e.g. Bluebird) instead of native Promises provided by JavaScript runtime.