@loopback/context
Advanced tools
Comparing version 3.4.0 to 3.5.0
@@ -6,2 +6,15 @@ # Change Log | ||
# [3.5.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@3.4.0...@loopback/context@3.5.0) (2020-04-22) | ||
### Features | ||
* update package.json and .travis.yml for builds ([cb2b8e6](https://github.com/strongloop/loopback-next/commit/cb2b8e6a18616dda7783c0193091039d4e608131)) | ||
* **context:** allow explicit invocation source for proxies ([98115f2](https://github.com/strongloop/loopback-next/commit/98115f299e250f124791ff211ae686a4d8c964ce)) | ||
* **context:** improve generic typing for binding inspection ([819fabb](https://github.com/strongloop/loopback-next/commit/819fabb04390ce8268c1dc7869253b01eafde4cb)) | ||
# [3.4.0](https://github.com/strongloop/loopback-next/compare/@loopback/context@3.3.0...@loopback/context@3.4.0) (2020-04-11) | ||
@@ -8,0 +21,0 @@ |
@@ -8,12 +8,14 @@ import { MetadataAccessor } from '@loopback/metadata'; | ||
* Binding metadata from `@bind` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare type BindingMetadata = { | ||
export declare type BindingMetadata<T = unknown> = { | ||
/** | ||
* An array of template functions to configure a binding | ||
*/ | ||
templates: BindingTemplate[]; | ||
templates: BindingTemplate<T>[]; | ||
/** | ||
* The target class where binding metadata is decorated | ||
*/ | ||
target: Constructor<unknown>; | ||
target: Constructor<T>; | ||
}; | ||
@@ -23,3 +25,3 @@ /** | ||
*/ | ||
export declare const BINDING_METADATA_KEY: MetadataAccessor<BindingMetadata, ClassDecorator>; | ||
export declare const BINDING_METADATA_KEY: MetadataAccessor<BindingMetadata<unknown>, ClassDecorator>; | ||
/** | ||
@@ -35,8 +37,10 @@ * An object to configure binding scope and tags | ||
*/ | ||
export declare type BindingSpec = BindingTemplate | BindingScopeAndTags; | ||
export declare type BindingSpec<T = unknown> = BindingTemplate<T> | BindingScopeAndTags; | ||
/** | ||
* Check if a class implements `Provider` interface | ||
* @param cls - A class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function isProviderClass(cls: Constructor<unknown>): cls is Constructor<Provider<unknown>>; | ||
export declare function isProviderClass<T>(cls: Constructor<T | Provider<T>>): cls is Constructor<Provider<T>>; | ||
/** | ||
@@ -46,4 +50,6 @@ * A factory function to create a template function to bind the target class | ||
* @param target - Target provider class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function asProvider(target: Constructor<Provider<unknown>>): BindingTemplate; | ||
export declare function asProvider<T>(target: Constructor<Provider<T>>): BindingTemplate<T>; | ||
/** | ||
@@ -53,14 +59,20 @@ * A factory function to create a template function to bind the target class | ||
* @param target - Target class, which can be an implementation of `Provider` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function asClassOrProvider(target: Constructor<unknown>): BindingTemplate; | ||
export declare function asClassOrProvider<T>(target: Constructor<T | Provider<T>>): BindingTemplate<T>; | ||
/** | ||
* Convert binding scope and tags as a template function | ||
* @param scopeAndTags - Binding scope and tags | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function asBindingTemplate(scopeAndTags: BindingScopeAndTags): BindingTemplate; | ||
export declare function asBindingTemplate<T = unknown>(scopeAndTags: BindingScopeAndTags): BindingTemplate<T>; | ||
/** | ||
* Get binding metadata for a class | ||
* @param target - The target class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function getBindingMetadata(target: Function): BindingMetadata | undefined; | ||
export declare function getBindingMetadata<T = unknown>(target: Function): BindingMetadata<T> | undefined; | ||
/** | ||
@@ -74,4 +86,6 @@ * A binding template function to delete `name` and `key` tags | ||
* @param cls - A class with optional `@bind` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function bindingTemplateFor<T = unknown>(cls: Constructor<T | Provider<T>>): BindingTemplate<T>; | ||
export declare function bindingTemplateFor<T>(cls: Constructor<T | Provider<T>>): BindingTemplate<T>; | ||
/** | ||
@@ -129,3 +143,5 @@ * Mapping artifact types to binding key namespaces (prefixes). | ||
* @param options - Options to customize the binding key | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export declare function createBindingFromClass<T = unknown>(cls: Constructor<T | Provider<T>>, options?: BindingFromClassOptions): Binding<T>; | ||
export declare function createBindingFromClass<T>(cls: Constructor<T | Provider<T>>, options?: BindingFromClassOptions): Binding<T>; |
@@ -20,5 +20,8 @@ "use strict"; | ||
* @param cls - A class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
function isProviderClass(cls) { | ||
return cls && typeof cls.prototype.value === 'function'; | ||
var _a; | ||
return typeof ((_a = cls === null || cls === void 0 ? void 0 : cls.prototype) === null || _a === void 0 ? void 0 : _a.value) === 'function'; | ||
} | ||
@@ -30,2 +33,4 @@ exports.isProviderClass = isProviderClass; | ||
* @param target - Target provider class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -44,2 +49,4 @@ function asProvider(target) { | ||
* @param target - Target class, which can be an implementation of `Provider` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -61,2 +68,4 @@ function asClassOrProvider(target) { | ||
* @param scopeAndTags - Binding scope and tags | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -82,2 +91,4 @@ function asBindingTemplate(scopeAndTags) { | ||
* @param target - The target class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -102,2 +113,4 @@ function getBindingMetadata(target) { | ||
* @param cls - A class with optional `@bind` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -133,2 +146,4 @@ function bindingTemplateFor(cls) { | ||
* @param options - Options to customize the binding key | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -195,2 +210,4 @@ function createBindingFromClass(cls, options = {}) { | ||
* @param options - Options to customize how to build the key | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
@@ -197,0 +214,0 @@ function buildBindingKey(cls, options = {}) { |
@@ -65,3 +65,4 @@ import { Context } from './context'; | ||
private session?; | ||
constructor(context?: Context, session?: ResolutionSession | undefined); | ||
private source?; | ||
constructor(context?: Context, session?: ResolutionSession | undefined, source?: InvocationSource<unknown> | undefined); | ||
get(target: T, propertyName: PropertyKey, receiver: unknown): any; | ||
@@ -73,3 +74,5 @@ } | ||
* @param context - Context object | ||
* @param session - Resolution session | ||
* @param source - Invocation source | ||
*/ | ||
export declare function createProxyWithInterceptors<T extends object>(target: T, context?: Context, session?: ResolutionSession): AsyncProxy<T>; | ||
export declare function createProxyWithInterceptors<T extends object>(target: T, context?: Context, session?: ResolutionSession, source?: InvocationSource): AsyncProxy<T>; |
@@ -30,5 +30,6 @@ "use strict"; | ||
class InterceptionHandler { | ||
constructor(context = new context_1.Context(), session) { | ||
constructor(context = new context_1.Context(), session, source) { | ||
this.context = context; | ||
this.session = session; | ||
this.source = source; | ||
} | ||
@@ -43,3 +44,6 @@ get(target, propertyName, receiver) { | ||
return (...args) => { | ||
return interceptor_1.invokeMethodWithInterceptors(this.context, target, propertyName, args, { source: this.session && new ProxySource(this.session) }); | ||
var _a; | ||
return interceptor_1.invokeMethodWithInterceptors(this.context, target, propertyName, args, { | ||
source: (_a = this.source) !== null && _a !== void 0 ? _a : (this.session && new ProxySource(this.session)), | ||
}); | ||
}; | ||
@@ -57,7 +61,9 @@ } | ||
* @param context - Context object | ||
* @param session - Resolution session | ||
* @param source - Invocation source | ||
*/ | ||
function createProxyWithInterceptors(target, context, session) { | ||
return new Proxy(target, new InterceptionHandler(context, resolution_session_1.ResolutionSession.fork(session))); | ||
function createProxyWithInterceptors(target, context, session, source) { | ||
return new Proxy(target, new InterceptionHandler(context, resolution_session_1.ResolutionSession.fork(session), source)); | ||
} | ||
exports.createProxyWithInterceptors = createProxyWithInterceptors; | ||
//# sourceMappingURL=interception-proxy.js.map |
{ | ||
"name": "@loopback/context", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "LoopBack's container for Inversion of Control", | ||
@@ -11,3 +11,3 @@ "engines": { | ||
"build": "lb-tsc", | ||
"clean": "lb-clean loopback-context*.tgz dist tsconfig.build.tsbuildinfo package", | ||
"clean": "lb-clean loopback-context*.tgz dist *.tsbuildinfo package", | ||
"pretest": "npm run build", | ||
@@ -22,3 +22,3 @@ "test": "lb-mocha \"dist/__tests__/**/*.js\"", | ||
"dependencies": { | ||
"@loopback/metadata": "^2.0.4", | ||
"@loopback/metadata": "^2.1.0", | ||
"@types/debug": "^4.1.5", | ||
@@ -31,7 +31,7 @@ "debug": "^4.1.1", | ||
"devDependencies": { | ||
"@loopback/build": "^5.0.1", | ||
"@loopback/eslint-config": "^6.0.3", | ||
"@loopback/testlab": "^3.0.1", | ||
"@loopback/build": "^5.1.0", | ||
"@loopback/eslint-config": "^6.0.4", | ||
"@loopback/testlab": "^3.1.0", | ||
"@types/bluebird": "^3.5.30", | ||
"@types/node": "^10.17.19", | ||
"@types/node": "^10.17.21", | ||
"@types/uuid": "^7.0.2", | ||
@@ -62,3 +62,3 @@ "bluebird": "^3.7.2" | ||
}, | ||
"gitHead": "7784cf1ef0c58220b32e2d9e35e5b0ef34825483" | ||
"gitHead": "fa2fe1c81c7beb74eb7dfd0898e2b3f0b7c7f762" | ||
} |
@@ -18,12 +18,14 @@ // Copyright IBM Corp. 2018,2019. All Rights Reserved. | ||
* Binding metadata from `@bind` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export type BindingMetadata = { | ||
export type BindingMetadata<T = unknown> = { | ||
/** | ||
* An array of template functions to configure a binding | ||
*/ | ||
templates: BindingTemplate[]; | ||
templates: BindingTemplate<T>[]; | ||
/** | ||
* The target class where binding metadata is decorated | ||
*/ | ||
target: Constructor<unknown>; | ||
target: Constructor<T>; | ||
}; | ||
@@ -50,3 +52,3 @@ | ||
*/ | ||
export type BindingSpec = BindingTemplate | BindingScopeAndTags; | ||
export type BindingSpec<T = unknown> = BindingTemplate<T> | BindingScopeAndTags; | ||
@@ -56,7 +58,9 @@ /** | ||
* @param cls - A class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function isProviderClass( | ||
cls: Constructor<unknown>, | ||
): cls is Constructor<Provider<unknown>> { | ||
return cls && typeof cls.prototype.value === 'function'; | ||
export function isProviderClass<T>( | ||
cls: Constructor<T | Provider<T>>, | ||
): cls is Constructor<Provider<T>> { | ||
return typeof cls?.prototype?.value === 'function'; | ||
} | ||
@@ -68,6 +72,8 @@ | ||
* @param target - Target provider class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function asProvider( | ||
target: Constructor<Provider<unknown>>, | ||
): BindingTemplate { | ||
export function asProvider<T>( | ||
target: Constructor<Provider<T>>, | ||
): BindingTemplate<T> { | ||
return function bindAsProvider(binding) { | ||
@@ -84,6 +90,8 @@ binding.toProvider(target).tag(ContextTags.PROVIDER, { | ||
* @param target - Target class, which can be an implementation of `Provider` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function asClassOrProvider( | ||
target: Constructor<unknown>, | ||
): BindingTemplate { | ||
export function asClassOrProvider<T>( | ||
target: Constructor<T | Provider<T>>, | ||
): BindingTemplate<T> { | ||
// Add a template to bind to a class or provider | ||
@@ -94,3 +102,3 @@ return function bindAsClassOrProvider(binding) { | ||
} else { | ||
binding.toClass(target); | ||
binding.toClass(target as Constructor<T>); | ||
} | ||
@@ -103,6 +111,8 @@ }; | ||
* @param scopeAndTags - Binding scope and tags | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function asBindingTemplate( | ||
export function asBindingTemplate<T = unknown>( | ||
scopeAndTags: BindingScopeAndTags, | ||
): BindingTemplate { | ||
): BindingTemplate<T> { | ||
return function applyBindingScopeAndTag(binding) { | ||
@@ -125,7 +135,9 @@ if (scopeAndTags.scope) { | ||
* @param target - The target class | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function getBindingMetadata( | ||
export function getBindingMetadata<T = unknown>( | ||
target: Function, | ||
): BindingMetadata | undefined { | ||
return MetadataInspector.getClassMetadata<BindingMetadata>( | ||
): BindingMetadata<T> | undefined { | ||
return MetadataInspector.getClassMetadata<BindingMetadata<T>>( | ||
BINDING_METADATA_KEY, | ||
@@ -150,4 +162,6 @@ target, | ||
* @param cls - A class with optional `@bind` | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function bindingTemplateFor<T = unknown>( | ||
export function bindingTemplateFor<T>( | ||
cls: Constructor<T | Provider<T>>, | ||
@@ -225,4 +239,6 @@ ): BindingTemplate<T> { | ||
* @param options - Options to customize the binding key | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
export function createBindingFromClass<T = unknown>( | ||
export function createBindingFromClass<T>( | ||
cls: Constructor<T | Provider<T>>, | ||
@@ -293,5 +309,7 @@ options: BindingFromClassOptions = {}, | ||
* @param options - Options to customize how to build the key | ||
* | ||
* @typeParam T - Value type | ||
*/ | ||
function buildBindingKey( | ||
cls: Constructor<unknown>, | ||
function buildBindingKey<T>( | ||
cls: Constructor<T | Provider<T>>, | ||
options: BindingFromClassOptions = {}, | ||
@@ -298,0 +316,0 @@ ) { |
@@ -83,2 +83,3 @@ // Copyright IBM Corp. 2019,2020. All Rights Reserved. | ||
private session?: ResolutionSession, | ||
private source?: InvocationSource, | ||
) {} | ||
@@ -98,3 +99,6 @@ | ||
args, | ||
{source: this.session && new ProxySource(this.session)}, | ||
{ | ||
source: | ||
this.source ?? (this.session && new ProxySource(this.session)), | ||
}, | ||
); | ||
@@ -112,2 +116,4 @@ }; | ||
* @param context - Context object | ||
* @param session - Resolution session | ||
* @param source - Invocation source | ||
*/ | ||
@@ -118,7 +124,8 @@ export function createProxyWithInterceptors<T extends object>( | ||
session?: ResolutionSession, | ||
source?: InvocationSource, | ||
): AsyncProxy<T> { | ||
return new Proxy( | ||
target, | ||
new InterceptionHandler(context, ResolutionSession.fork(session)), | ||
new InterceptionHandler(context, ResolutionSession.fork(session), source), | ||
) as AsyncProxy<T>; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
623431
13774
Updated@loopback/metadata@^2.1.0