@loopback/context
Advanced tools
Comparing version 1.23.5 to 1.24.0
@@ -6,2 +6,15 @@ # Change Log | ||
# [1.24.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@1.23.5...@loopback/context@1.24.0) (2019-11-25) | ||
### Features | ||
* **context:** allow current binding to be injected with `[@inject](https://github.com/inject).binding` ([c01b4c6](https://github.com/strongloop/loopback-next/commit/c01b4c6b4dcc5c7ac79832bda144b1ec0da191b1)) | ||
* **context:** improve context/binding with inspect/toJSON for metadata dumping ([ac399f7](https://github.com/strongloop/loopback-next/commit/ac399f7f105eea402ef1932bd96093baad0a009f)) | ||
* **core:** add [@service](https://github.com/service) decorator to inject a service by class/interface ([1d80904](https://github.com/strongloop/loopback-next/commit/1d80904b670724b00cb6a2965b8472f44d23eed0)) | ||
## [1.23.5](https://github.com/strongloop/loopback-next/compare/@loopback/context@1.23.4...@loopback/context@1.23.5) (2019-11-12) | ||
@@ -8,0 +21,0 @@ |
@@ -137,7 +137,13 @@ import { BindingAddress } from './binding-key'; | ||
private _valueConstructor?; | ||
private _providerConstructor?; | ||
/** | ||
* For bindings bound via toClass, this property contains the constructor | ||
* function | ||
* For bindings bound via `toClass()`, this property contains the constructor | ||
* function of the class | ||
*/ | ||
get valueConstructor(): Constructor<T> | undefined; | ||
/** | ||
* For bindings bound via `toProvider()`, this property contains the | ||
* constructor function of the provider class | ||
*/ | ||
get providerConstructor(): Constructor<Provider<T>> | undefined; | ||
constructor(key: BindingAddress<T>, isLocked?: boolean); | ||
@@ -323,3 +329,3 @@ /** | ||
*/ | ||
toJSON(): Object; | ||
toJSON(): object; | ||
/** | ||
@@ -326,0 +332,0 @@ * A static method to create a binding so that we can do |
@@ -140,4 +140,4 @@ "use strict"; | ||
/** | ||
* For bindings bound via toClass, this property contains the constructor | ||
* function | ||
* For bindings bound via `toClass()`, this property contains the constructor | ||
* function of the class | ||
*/ | ||
@@ -148,2 +148,9 @@ get valueConstructor() { | ||
/** | ||
* For bindings bound via `toProvider()`, this property contains the | ||
* constructor function of the provider class | ||
*/ | ||
get providerConstructor() { | ||
return this._providerConstructor; | ||
} | ||
/** | ||
* Cache the resolved value by the binding scope | ||
@@ -389,2 +396,3 @@ * @param ctx - The current context | ||
this._type = BindingType.PROVIDER; | ||
this._providerConstructor = providerClass; | ||
this._setValueGetter((ctx, options) => { | ||
@@ -465,3 +473,2 @@ const providerOrPromise = resolver_1.instantiateClass(providerClass, ctx, options.session); | ||
toJSON() { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const json = { | ||
@@ -476,2 +483,8 @@ key: this.key, | ||
} | ||
if (this._valueConstructor != null) { | ||
json.valueConstructor = this._valueConstructor.name; | ||
} | ||
if (this._providerConstructor != null) { | ||
json.providerConstructor = this._providerConstructor.name; | ||
} | ||
return json; | ||
@@ -478,0 +491,0 @@ } |
@@ -433,3 +433,8 @@ /// <reference types="node" /> | ||
*/ | ||
toJSON(): Object; | ||
toJSON(): object; | ||
/** | ||
* Inspect the context and dump out a JSON object representing the context | ||
* hierarchy | ||
*/ | ||
inspect(): object; | ||
} | ||
@@ -436,0 +441,0 @@ /** |
@@ -667,6 +667,21 @@ "use strict"; | ||
toJSON() { | ||
const json = {}; | ||
const bindings = {}; | ||
for (const [k, v] of this.registry) { | ||
json[k] = v.toJSON(); | ||
bindings[k] = v.toJSON(); | ||
} | ||
return bindings; | ||
} | ||
/** | ||
* Inspect the context and dump out a JSON object representing the context | ||
* hierarchy | ||
*/ | ||
// TODO(rfeng): Evaluate https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects | ||
inspect() { | ||
const json = { | ||
name: this.name, | ||
bindings: this.toJSON(), | ||
}; | ||
if (this._parent) { | ||
json.parent = this._parent.inspect(); | ||
} | ||
return json; | ||
@@ -673,0 +688,0 @@ } |
@@ -12,7 +12,8 @@ export * from '@loopback/metadata'; | ||
export * from './context-view'; | ||
export * from './interceptor-chain'; | ||
export * from './inject'; | ||
export * from './inject-config'; | ||
export * from './interception-proxy'; | ||
export * from './interceptor'; | ||
export * from './inject-config'; | ||
export * from './interceptor-chain'; | ||
export * from './invocation'; | ||
export * from './keys'; | ||
@@ -23,2 +24,1 @@ export * from './provider'; | ||
export * from './value-promise'; | ||
export * from './invocation'; |
@@ -20,7 +20,8 @@ "use strict"; | ||
__export(require("./context-view")); | ||
__export(require("./interceptor-chain")); | ||
__export(require("./inject")); | ||
__export(require("./inject-config")); | ||
__export(require("./interception-proxy")); | ||
__export(require("./interceptor")); | ||
__export(require("./inject-config")); | ||
__export(require("./interceptor-chain")); | ||
__export(require("./invocation")); | ||
__export(require("./keys")); | ||
@@ -30,3 +31,2 @@ __export(require("./resolution-session")); | ||
__export(require("./value-promise")); | ||
__export(require("./invocation")); | ||
//# sourceMappingURL=index.js.map |
@@ -160,3 +160,3 @@ import { MetadataMap } from '@loopback/metadata'; | ||
*/ | ||
const binding: (bindingKey: BindingAddress<unknown>, metadata?: InjectBindingMetadata | undefined) => (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
const binding: (bindingKey?: string | import("./binding-key").BindingKey<unknown> | undefined, metadata?: InjectBindingMetadata | undefined) => (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void; | ||
/** | ||
@@ -163,0 +163,0 @@ * Inject an array of values by a tag pattern string or regexp |
@@ -176,3 +176,3 @@ "use strict"; | ||
metadata = Object.assign({ decorator: '@inject.binding' }, metadata); | ||
return inject(bindingKey, metadata, resolveAsBinding); | ||
return inject(bindingKey || '', metadata, resolveAsBinding); | ||
}; | ||
@@ -273,2 +273,5 @@ /** | ||
} | ||
if (bindingSelector === '') { | ||
throw new Error('Binding key is not set for @inject.setter'); | ||
} | ||
// No resolution session should be propagated into the setter | ||
@@ -280,3 +283,3 @@ return function setter(value) { | ||
} | ||
function resolveAsBinding(ctx, injection) { | ||
function resolveAsBinding(ctx, injection, session) { | ||
const targetName = assertTargetType(injection, binding_1.Binding); | ||
@@ -287,5 +290,7 @@ const bindingSelector = injection.bindingSelector; | ||
} | ||
return findOrCreateBindingForInjection(ctx, injection); | ||
return findOrCreateBindingForInjection(ctx, injection, session); | ||
} | ||
function findOrCreateBindingForInjection(ctx, injection) { | ||
function findOrCreateBindingForInjection(ctx, injection, session) { | ||
if (injection.bindingSelector === '') | ||
return session && session.currentBinding; | ||
const bindingCreation = injection.metadata && | ||
@@ -292,0 +297,0 @@ injection.metadata.bindingCreation; |
{ | ||
"name": "@loopback/context", | ||
"version": "1.23.5", | ||
"version": "1.24.0", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -21,3 +21,3 @@ "engines": { | ||
"dependencies": { | ||
"@loopback/metadata": "^1.3.7", | ||
"@loopback/metadata": "^1.3.8", | ||
"debug": "^4.1.1", | ||
@@ -28,5 +28,5 @@ "p-event": "^4.1.0", | ||
"devDependencies": { | ||
"@loopback/build": "^2.0.16", | ||
"@loopback/eslint-config": "^4.1.4", | ||
"@loopback/testlab": "^1.9.4", | ||
"@loopback/build": "^2.1.0", | ||
"@loopback/eslint-config": "^4.1.5", | ||
"@loopback/testlab": "^1.9.5", | ||
"@types/bluebird": "^3.5.29", | ||
@@ -60,3 +60,3 @@ "@types/debug": "^4.1.5", | ||
}, | ||
"gitHead": "7f0ae6ec124c19357c3016cd11f9bc9c739dffcb" | ||
"gitHead": "c111543a6139c5a2999930c593aabbbdf10a838e" | ||
} |
@@ -183,5 +183,7 @@ // Copyright IBM Corp. 2017,2019. All Rights Reserved. | ||
private _valueConstructor?: Constructor<T>; | ||
private _providerConstructor?: Constructor<Provider<T>>; | ||
/** | ||
* For bindings bound via toClass, this property contains the constructor | ||
* function | ||
* For bindings bound via `toClass()`, this property contains the constructor | ||
* function of the class | ||
*/ | ||
@@ -192,2 +194,10 @@ public get valueConstructor(): Constructor<T> | undefined { | ||
/** | ||
* For bindings bound via `toProvider()`, this property contains the | ||
* constructor function of the provider class | ||
*/ | ||
public get providerConstructor(): Constructor<Provider<T>> | undefined { | ||
return this._providerConstructor; | ||
} | ||
constructor(key: BindingAddress<T>, public isLocked: boolean = false) { | ||
@@ -499,2 +509,3 @@ BindingKey.validate(key); | ||
this._type = BindingType.PROVIDER; | ||
this._providerConstructor = providerClass; | ||
this._setValueGetter((ctx, options) => { | ||
@@ -582,5 +593,4 @@ const providerOrPromise = instantiateClass<Provider<T>>( | ||
*/ | ||
toJSON(): Object { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const json: {[name: string]: any} = { | ||
toJSON(): object { | ||
const json: Record<string, unknown> = { | ||
key: this.key, | ||
@@ -594,2 +604,8 @@ scope: this.scope, | ||
} | ||
if (this._valueConstructor != null) { | ||
json.valueConstructor = this._valueConstructor.name; | ||
} | ||
if (this._providerConstructor != null) { | ||
json.providerConstructor = this._providerConstructor.name; | ||
} | ||
return json; | ||
@@ -596,0 +612,0 @@ } |
@@ -971,7 +971,23 @@ // Copyright IBM Corp. 2017,2019. All Rights Reserved. | ||
*/ | ||
toJSON(): Object { | ||
const json: {[key: string]: Object} = {}; | ||
toJSON(): object { | ||
const bindings: Record<string, object> = {}; | ||
for (const [k, v] of this.registry) { | ||
json[k] = v.toJSON(); | ||
bindings[k] = v.toJSON(); | ||
} | ||
return bindings; | ||
} | ||
/** | ||
* Inspect the context and dump out a JSON object representing the context | ||
* hierarchy | ||
*/ | ||
// TODO(rfeng): Evaluate https://nodejs.org/api/util.html#util_custom_inspection_functions_on_objects | ||
inspect(): object { | ||
const json: Record<string, unknown> = { | ||
name: this.name, | ||
bindings: this.toJSON(), | ||
}; | ||
if (this._parent) { | ||
json.parent = this._parent.inspect(); | ||
} | ||
return json; | ||
@@ -978,0 +994,0 @@ } |
@@ -17,7 +17,8 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
export * from './context-view'; | ||
export * from './interceptor-chain'; | ||
export * from './inject'; | ||
export * from './inject-config'; | ||
export * from './interception-proxy'; | ||
export * from './interceptor'; | ||
export * from './inject-config'; | ||
export * from './interceptor-chain'; | ||
export * from './invocation'; | ||
export * from './keys'; | ||
@@ -28,2 +29,1 @@ export * from './provider'; | ||
export * from './value-promise'; | ||
export * from './invocation'; |
@@ -314,7 +314,7 @@ // Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
export const binding = function injectBinding( | ||
bindingKey: BindingAddress, | ||
bindingKey?: BindingAddress, | ||
metadata?: InjectBindingMetadata, | ||
) { | ||
metadata = Object.assign({decorator: '@inject.binding'}, metadata); | ||
return inject(bindingKey, metadata, resolveAsBinding); | ||
return inject(bindingKey || '', metadata, resolveAsBinding); | ||
}; | ||
@@ -444,10 +444,17 @@ | ||
} | ||
if (bindingSelector === '') { | ||
throw new Error('Binding key is not set for @inject.setter'); | ||
} | ||
// No resolution session should be propagated into the setter | ||
return function setter(value: unknown) { | ||
const binding = findOrCreateBindingForInjection(ctx, injection); | ||
binding.to(value); | ||
binding!.to(value); | ||
}; | ||
} | ||
function resolveAsBinding(ctx: Context, injection: Injection) { | ||
function resolveAsBinding( | ||
ctx: Context, | ||
injection: Injection, | ||
session: ResolutionSession, | ||
) { | ||
const targetName = assertTargetType(injection, Binding); | ||
@@ -460,3 +467,3 @@ const bindingSelector = injection.bindingSelector; | ||
} | ||
return findOrCreateBindingForInjection(ctx, injection); | ||
return findOrCreateBindingForInjection(ctx, injection, session); | ||
} | ||
@@ -467,3 +474,6 @@ | ||
injection: Injection<unknown>, | ||
session?: ResolutionSession, | ||
) { | ||
if (injection.bindingSelector === '') | ||
return session && session.currentBinding; | ||
const bindingCreation = | ||
@@ -470,0 +480,0 @@ injection.metadata && |
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
532367
11744
Updated@loopback/metadata@^1.3.8