@loopback/context
Advanced tools
Comparing version 4.0.0-alpha.10 to 4.0.0-alpha.11
@@ -163,3 +163,16 @@ import { Context } from './context'; | ||
/** | ||
* Bind the key to a BindingProvider | ||
* Bind the key to a value computed by a Provider. | ||
* | ||
* * @example | ||
* | ||
* ```ts | ||
* export class DateProvider implements Provider<Date> { | ||
* constructor(@inject('stringDate') private param: String){} | ||
* value(): Date { | ||
* return new Date(param); | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param provider The value provider to use. | ||
*/ | ||
@@ -166,0 +179,0 @@ toProvider<T>(providerClass: Constructor<Provider<T>>): this; |
@@ -266,3 +266,16 @@ "use strict"; | ||
/** | ||
* Bind the key to a BindingProvider | ||
* Bind the key to a value computed by a Provider. | ||
* | ||
* * @example | ||
* | ||
* ```ts | ||
* export class DateProvider implements Provider<Date> { | ||
* constructor(@inject('stringDate') private param: String){} | ||
* value(): Date { | ||
* return new Date(param); | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param provider The value provider to use. | ||
*/ | ||
@@ -269,0 +282,0 @@ toProvider(providerClass) { |
export { Binding, BindingScope, BoundValue, ValueOrPromise } from './binding'; | ||
export { Context } from './context'; | ||
export { Constructor } from './resolver'; | ||
export { inject } from './inject'; | ||
export { inject, Setter, Getter } from './inject'; | ||
export { NamespacedReflect } from './reflect'; | ||
@@ -6,0 +6,0 @@ export { Provider } from './provider'; |
@@ -47,2 +47,40 @@ import { BoundValue, ValueOrPromise } from './binding'; | ||
/** | ||
* The function injected by `@inject.getter(key)`. | ||
*/ | ||
export declare type Getter<T> = () => Promise<T>; | ||
/** | ||
* The function injected by `@inject.setter(key)`. | ||
*/ | ||
export declare type Setter<T> = (value: T) => void; | ||
export declare namespace inject { | ||
/** | ||
* Inject a function for getting the actual bound value. | ||
* | ||
* This is useful when implementing Actions, where | ||
* the action is instantiated for Sequence constructor, but some | ||
* of action's dependencies become bound only after other actions | ||
* have been executed by the sequence. | ||
* | ||
* See also `Getter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to eventually get. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
const getter: (bindingKey: string, metadata?: Object | undefined) => (target: any, propertyKey?: string | symbol | undefined, propertyDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
/** | ||
* Inject a function for setting (binding) the given key to a given | ||
* value. (Only static/constant values are supported, it's not possible | ||
* to bind a key to a class or a provider.) | ||
* | ||
* This is useful e.g. when implementing Actions that are contributing | ||
* new Elements. | ||
* | ||
* See also `Setter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to set. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
const setter: (bindingKey: string, metadata?: Object | undefined) => (target: any, propertyKey?: string | symbol | undefined, propertyDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
} | ||
/** | ||
* Return an array of injection objects for parameters | ||
@@ -49,0 +87,0 @@ * @param target The target class for constructor or static methods, |
@@ -67,2 +67,46 @@ "use strict"; | ||
exports.inject = inject; | ||
(function (inject) { | ||
/** | ||
* Inject a function for getting the actual bound value. | ||
* | ||
* This is useful when implementing Actions, where | ||
* the action is instantiated for Sequence constructor, but some | ||
* of action's dependencies become bound only after other actions | ||
* have been executed by the sequence. | ||
* | ||
* See also `Getter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to eventually get. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
inject.getter = function injectGetter(bindingKey, metadata) { | ||
return inject(bindingKey, metadata, resolveAsGetter); | ||
}; | ||
/** | ||
* Inject a function for setting (binding) the given key to a given | ||
* value. (Only static/constant values are supported, it's not possible | ||
* to bind a key to a class or a provider.) | ||
* | ||
* This is useful e.g. when implementing Actions that are contributing | ||
* new Elements. | ||
* | ||
* See also `Setter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to set. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
inject.setter = function injectSetter(bindingKey, metadata) { | ||
return inject(bindingKey, metadata, resolveAsSetter); | ||
}; | ||
})(inject = exports.inject || (exports.inject = {})); | ||
function resolveAsGetter(ctx, injection) { | ||
return function getter() { | ||
return ctx.get(injection.bindingKey); | ||
}; | ||
} | ||
function resolveAsSetter(ctx, injection) { | ||
return function setter(value) { | ||
ctx.bind(injection.bindingKey).to(value); | ||
}; | ||
} | ||
/** | ||
@@ -69,0 +113,0 @@ * Return an array of injection objects for parameters |
import { ValueOrPromise } from './binding'; | ||
/** | ||
* @exports Provider<T> : interface definition for a provider of a value | ||
* of type T | ||
* @summary Providers allow binding of a value provider class instead of the | ||
* value itself | ||
* @example: | ||
* Providers allow developers to compute injected values dynamically, | ||
* with any dependencies required by the value getter injected automatically | ||
* from the Context. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
@@ -15,18 +16,17 @@ * export class DateProvider implements Provider<Date> { | ||
* } | ||
* ``` | ||
* @example: Binding a context | ||
* ```ts | ||
* | ||
* ctx.bind('stringDate').to('2017-01-01') | ||
* ctx.bind('provider_key').toProvider(MyProvider); | ||
* | ||
* const value = ctx.getAsync('provider_key'); | ||
* // value is a Date instance | ||
* ``` | ||
* @example: getting a value dynamically | ||
* ```ts | ||
* ctx.get('provider_key'); | ||
* ctx.getBinding('provider_key').getValue(); | ||
* ``` | ||
*/ | ||
export interface Provider<T> { | ||
/** | ||
* @returns a value or a promise | ||
* @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. | ||
*/ | ||
value(): ValueOrPromise<T>; | ||
} |
@@ -163,3 +163,16 @@ import { Context } from './context'; | ||
/** | ||
* Bind the key to a BindingProvider | ||
* Bind the key to a value computed by a Provider. | ||
* | ||
* * @example | ||
* | ||
* ```ts | ||
* export class DateProvider implements Provider<Date> { | ||
* constructor(@inject('stringDate') private param: String){} | ||
* value(): Date { | ||
* return new Date(param); | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param provider The value provider to use. | ||
*/ | ||
@@ -166,0 +179,0 @@ toProvider<T>(providerClass: Constructor<Provider<T>>): this; |
@@ -266,3 +266,16 @@ "use strict"; | ||
/** | ||
* Bind the key to a BindingProvider | ||
* Bind the key to a value computed by a Provider. | ||
* | ||
* * @example | ||
* | ||
* ```ts | ||
* export class DateProvider implements Provider<Date> { | ||
* constructor(@inject('stringDate') private param: String){} | ||
* value(): Date { | ||
* return new Date(param); | ||
* } | ||
* } | ||
* ``` | ||
* | ||
* @param provider The value provider to use. | ||
*/ | ||
@@ -269,0 +282,0 @@ toProvider(providerClass) { |
export { Binding, BindingScope, BoundValue, ValueOrPromise } from './binding'; | ||
export { Context } from './context'; | ||
export { Constructor } from './resolver'; | ||
export { inject } from './inject'; | ||
export { inject, Setter, Getter } from './inject'; | ||
export { NamespacedReflect } from './reflect'; | ||
@@ -6,0 +6,0 @@ export { Provider } from './provider'; |
@@ -47,2 +47,40 @@ import { BoundValue, ValueOrPromise } from './binding'; | ||
/** | ||
* The function injected by `@inject.getter(key)`. | ||
*/ | ||
export declare type Getter<T> = () => Promise<T>; | ||
/** | ||
* The function injected by `@inject.setter(key)`. | ||
*/ | ||
export declare type Setter<T> = (value: T) => void; | ||
export declare namespace inject { | ||
/** | ||
* Inject a function for getting the actual bound value. | ||
* | ||
* This is useful when implementing Actions, where | ||
* the action is instantiated for Sequence constructor, but some | ||
* of action's dependencies become bound only after other actions | ||
* have been executed by the sequence. | ||
* | ||
* See also `Getter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to eventually get. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
const getter: (bindingKey: string, metadata?: Object | undefined) => (target: any, propertyKey?: string | symbol | undefined, propertyDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
/** | ||
* Inject a function for setting (binding) the given key to a given | ||
* value. (Only static/constant values are supported, it's not possible | ||
* to bind a key to a class or a provider.) | ||
* | ||
* This is useful e.g. when implementing Actions that are contributing | ||
* new Elements. | ||
* | ||
* See also `Setter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to set. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
const setter: (bindingKey: string, metadata?: Object | undefined) => (target: any, propertyKey?: string | symbol | undefined, propertyDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
} | ||
/** | ||
* Return an array of injection objects for parameters | ||
@@ -49,0 +87,0 @@ * @param target The target class for constructor or static methods, |
@@ -67,2 +67,46 @@ "use strict"; | ||
exports.inject = inject; | ||
(function (inject) { | ||
/** | ||
* Inject a function for getting the actual bound value. | ||
* | ||
* This is useful when implementing Actions, where | ||
* the action is instantiated for Sequence constructor, but some | ||
* of action's dependencies become bound only after other actions | ||
* have been executed by the sequence. | ||
* | ||
* See also `Getter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to eventually get. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
inject.getter = function injectGetter(bindingKey, metadata) { | ||
return inject(bindingKey, metadata, resolveAsGetter); | ||
}; | ||
/** | ||
* Inject a function for setting (binding) the given key to a given | ||
* value. (Only static/constant values are supported, it's not possible | ||
* to bind a key to a class or a provider.) | ||
* | ||
* This is useful e.g. when implementing Actions that are contributing | ||
* new Elements. | ||
* | ||
* See also `Setter<T>`. | ||
* | ||
* @param bindingKey The key of the value we want to set. | ||
* @param metadata Optional metadata to help the injection | ||
*/ | ||
inject.setter = function injectSetter(bindingKey, metadata) { | ||
return inject(bindingKey, metadata, resolveAsSetter); | ||
}; | ||
})(inject = exports.inject || (exports.inject = {})); | ||
function resolveAsGetter(ctx, injection) { | ||
return function getter() { | ||
return ctx.get(injection.bindingKey); | ||
}; | ||
} | ||
function resolveAsSetter(ctx, injection) { | ||
return function setter(value) { | ||
ctx.bind(injection.bindingKey).to(value); | ||
}; | ||
} | ||
/** | ||
@@ -69,0 +113,0 @@ * Return an array of injection objects for parameters |
import { ValueOrPromise } from './binding'; | ||
/** | ||
* @exports Provider<T> : interface definition for a provider of a value | ||
* of type T | ||
* @summary Providers allow binding of a value provider class instead of the | ||
* value itself | ||
* @example: | ||
* Providers allow developers to compute injected values dynamically, | ||
* with any dependencies required by the value getter injected automatically | ||
* from the Context. | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
@@ -15,18 +16,17 @@ * export class DateProvider implements Provider<Date> { | ||
* } | ||
* ``` | ||
* @example: Binding a context | ||
* ```ts | ||
* | ||
* ctx.bind('stringDate').to('2017-01-01') | ||
* ctx.bind('provider_key').toProvider(MyProvider); | ||
* | ||
* const value = ctx.getAsync('provider_key'); | ||
* // value is a Date instance | ||
* ``` | ||
* @example: getting a value dynamically | ||
* ```ts | ||
* ctx.get('provider_key'); | ||
* ctx.getBinding('provider_key').getValue(); | ||
* ``` | ||
*/ | ||
export interface Provider<T> { | ||
/** | ||
* @returns a value or a promise | ||
* @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. | ||
*/ | ||
value(): ValueOrPromise<T>; | ||
} |
{ | ||
"name": "@loopback/context", | ||
"version": "4.0.0-alpha.10", | ||
"version": "4.0.0-alpha.11", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -25,3 +25,3 @@ "scripts": { | ||
"devDependencies": { | ||
"@loopback/testlab": "^4.0.0-alpha.5", | ||
"@loopback/testlab": "^4.0.0-alpha.6", | ||
"@types/bluebird": "^3.5.8", | ||
@@ -28,0 +28,0 @@ "bluebird": "^3.5.0" |
# @loopback/context | ||
LoopBack uses Context to manage state and dependencies in your application. | ||
## Overview | ||
LoopBack implements the concept of a Context to represent a global registry in your application to manage config, state, dependencies, classes, etc. Context also serves as an IoC container used to inject dependencies into your code. | ||
## Installation | ||
``` | ||
$ npm install --save @loopback/context | ||
``` | ||
## Basic use | ||
``` | ||
// app level | ||
const app = new Application(); // `app` is a "Context" | ||
app.bind('hello').to('world'); // ContextKey='hello', ContextValue='world' | ||
console.log(app.getSync('hello')); // => 'world' | ||
``` | ||
Dependency injection using context | ||
``` | ||
const Application = require('@loopback/core'); | ||
const app = new Application(); | ||
const app.bind('defaultName').to('John'); // setting context | ||
class HelloController { | ||
// consider this.ctx here | ||
constructor(@inject('defaultName') name) { // injecting dependency using context | ||
this.name = name; | ||
} | ||
greet(name) { | ||
return `Hello ${name || this.name}`; | ||
} | ||
} | ||
``` | ||
For additional information, please refer to the [wiki](https://github.com/strongloop/loopback-next/wiki/Context). | ||
## Contributions | ||
IBM/StrongLoop is an active supporter of open source and welcomes contributions to our projects as well as those of the Node.js community in general. For more information on how to contribute please refer to the [Contribution Guide](https://loopback.io/doc/en/contrib/index.html). | ||
## Tests | ||
Run `npm test` from the root folder. | ||
## Contributors | ||
See [all contributors](https://github.com/strongloop/loopback-next/graphs/contributors). | ||
## License | ||
MIT |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
648107
6111
57