@loopback/metadata
Advanced tools
Comparing version 2.2.6 to 3.0.0
@@ -6,2 +6,26 @@ # Change Log | ||
# [3.0.0](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@2.2.6...@loopback/metadata@3.0.0) (2020-09-15) | ||
### Features | ||
* **metadata:** improve handling of missing design-time type metadata ([4816cae](https://github.com/strongloop/loopback-next/commit/4816caee73fe8f4b99b246bc8544f90037791b6f)) | ||
### BREAKING CHANGES | ||
* **metadata:** If you are consuming `@loopback/metadata` directly, | ||
then you may need to update your code to handle the case when | ||
design-time type metadata is not available. (The compiler will tell you | ||
what places to fix.) | ||
Regular LoopBack users should not be affected as long as they update | ||
all LB packages together. | ||
Signed-off-by: Miroslav Bajtoš <mbajtoss@gmail.com> | ||
## [2.2.6](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@2.2.5...@loopback/metadata@2.2.6) (2020-08-27) | ||
@@ -8,0 +32,0 @@ |
@@ -76,13 +76,17 @@ import { DecoratorType, MetadataKey, MetadataMap } from './types'; | ||
/** | ||
* Get the qualified name of a decoration target. For example: | ||
* ``` | ||
* class MyClass | ||
* MyClass.constructor[0] // First parameter of the constructor | ||
* MyClass.myStaticProperty | ||
* MyClass.myStaticMethod() | ||
* MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* MyClass.prototype.myProperty | ||
* MyClass.prototype.myMethod() | ||
* MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* ``` | ||
* Get the qualified name of a decoration target. | ||
* | ||
* @remarks | ||
* | ||
* Example of target names: | ||
* | ||
* - class MyClass | ||
* - MyClass.constructor[0] // First parameter of the constructor | ||
* - MyClass.myStaticProperty | ||
* - MyClass.myStaticMethod() | ||
* - MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* - MyClass.prototype.myProperty | ||
* - MyClass.prototype.myMethod() | ||
* - MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* | ||
* @param target - Class or prototype of a class | ||
@@ -166,3 +170,3 @@ * @param member - Optional property/method name | ||
*/ | ||
protected static _createDecorator<T, M extends T | MetadataMap<T> | MetadataMap<T[]>, D extends DecoratorType>(key: MetadataKey<T, D>, spec: T, options?: DecoratorOptions): D; | ||
protected static _createDecorator<S, MT extends S | MetadataMap<S> | MetadataMap<S[]>, DT extends DecoratorType>(key: MetadataKey<S, DT>, spec: S, options?: DecoratorOptions): DT; | ||
private static _cloneableTypes; | ||
@@ -184,3 +188,3 @@ static cloneDeep<V>(val: Readonly<V>): V; | ||
*/ | ||
static createDecorator<T>(key: MetadataKey<T, ClassDecorator>, spec: T, options?: DecoratorOptions): ClassDecorator; | ||
static createDecorator<S>(key: MetadataKey<S, ClassDecorator>, spec: S, options?: DecoratorOptions): ClassDecorator; | ||
} | ||
@@ -200,3 +204,3 @@ /** | ||
*/ | ||
static createDecorator<T>(key: MetadataKey<T, PropertyDecorator>, spec: T, options?: DecoratorOptions): PropertyDecorator; | ||
static createDecorator<S>(key: MetadataKey<S, PropertyDecorator>, spec: S, options?: DecoratorOptions): PropertyDecorator; | ||
} | ||
@@ -216,3 +220,3 @@ /** | ||
*/ | ||
static createDecorator<T>(key: MetadataKey<T, MethodDecorator>, spec: T, options?: DecoratorOptions): MethodDecorator; | ||
static createDecorator<S>(key: MetadataKey<S, MethodDecorator>, spec: S, options?: DecoratorOptions): MethodDecorator; | ||
} | ||
@@ -233,3 +237,3 @@ /** | ||
*/ | ||
static createDecorator<T>(key: MetadataKey<T, ParameterDecorator>, spec: T, options?: DecoratorOptions): ParameterDecorator; | ||
static createDecorator<S>(key: MetadataKey<S, ParameterDecorator>, spec: S, options?: DecoratorOptions): ParameterDecorator; | ||
} | ||
@@ -267,3 +271,3 @@ /** | ||
*/ | ||
static createDecorator<T>(key: MetadataKey<T, MethodDecorator>, spec: T, options?: DecoratorOptions): MethodDecorator; | ||
static createDecorator<S>(key: MetadataKey<S, MethodDecorator>, spec: S, options?: DecoratorOptions): MethodDecorator; | ||
} | ||
@@ -270,0 +274,0 @@ /** |
@@ -85,13 +85,17 @@ "use strict"; | ||
/** | ||
* Get the qualified name of a decoration target. For example: | ||
* ``` | ||
* class MyClass | ||
* MyClass.constructor[0] // First parameter of the constructor | ||
* MyClass.myStaticProperty | ||
* MyClass.myStaticMethod() | ||
* MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* MyClass.prototype.myProperty | ||
* MyClass.prototype.myMethod() | ||
* MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* ``` | ||
* Get the qualified name of a decoration target. | ||
* | ||
* @remarks | ||
* | ||
* Example of target names: | ||
* | ||
* - class MyClass | ||
* - MyClass.constructor[0] // First parameter of the constructor | ||
* - MyClass.myStaticProperty | ||
* - MyClass.myStaticMethod() | ||
* - MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* - MyClass.prototype.myProperty | ||
* - MyClass.prototype.myMethod() | ||
* - MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* | ||
* @param target - Class or prototype of a class | ||
@@ -98,0 +102,0 @@ * @param member - Optional property/method name |
@@ -105,10 +105,18 @@ import { NamespacedReflect } from './reflect'; | ||
* @param propertyName - Property name | ||
* @returns Design time metadata. The return value is `undefined` when: | ||
* - The property has type `undefined`, `null` or a complex type like | ||
* `Partial<MyModel>`, `string | number`, `string[]`. | ||
* - The TypeScript project has not enabled the compiler option `emitDecoratorMetadata`. | ||
* - The code is written in vanilla JavaScript. | ||
*/ | ||
static getDesignTypeForProperty(target: Object, propertyName: string): Function; | ||
static getDesignTypeForProperty(target: Object, propertyName: string): Function | undefined; | ||
/** | ||
* Get TypeScript design time type for a method | ||
* Get TypeScript design time type for a method. | ||
* @param target - Class or prototype | ||
* @param methodName - Method name | ||
* @returns Design time metadata. The return value is `undefined` | ||
* in projects that do not enable `emitDecoratorMetadata` | ||
* in TypeScript compiler options or are written in vanilla JavaScript. | ||
*/ | ||
static getDesignTypeForMethod(target: Object, methodName: string): DesignTimeMethodMetadata; | ||
static getDesignTypeForMethod(target: Object, methodName: string): DesignTimeMethodMetadata | undefined; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MetadataInspector = void 0; | ||
const tslib_1 = require("tslib"); | ||
// Copyright IBM Corp. 2017,2019. All Rights Reserved. | ||
@@ -6,5 +9,6 @@ // Node module: @loopback/metadata | ||
// License text available at https://opensource.org/licenses/MIT | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.MetadataInspector = void 0; | ||
const debug_1 = tslib_1.__importDefault(require("debug")); | ||
const decorator_factory_1 = require("./decorator-factory"); | ||
const reflect_1 = require("./reflect"); | ||
const debug = debug_1.default('loopback:metadata:inspector'); | ||
/** | ||
@@ -128,2 +132,7 @@ * TypeScript reflector without a namespace. The TypeScript compiler can be | ||
* @param propertyName - Property name | ||
* @returns Design time metadata. The return value is `undefined` when: | ||
* - The property has type `undefined`, `null` or a complex type like | ||
* `Partial<MyModel>`, `string | number`, `string[]`. | ||
* - The TypeScript project has not enabled the compiler option `emitDecoratorMetadata`. | ||
* - The code is written in vanilla JavaScript. | ||
*/ | ||
@@ -134,5 +143,8 @@ static getDesignTypeForProperty(target, propertyName) { | ||
/** | ||
* Get TypeScript design time type for a method | ||
* Get TypeScript design time type for a method. | ||
* @param target - Class or prototype | ||
* @param methodName - Method name | ||
* @returns Design time metadata. The return value is `undefined` | ||
* in projects that do not enable `emitDecoratorMetadata` | ||
* in TypeScript compiler options or are written in vanilla JavaScript. | ||
*/ | ||
@@ -143,2 +155,13 @@ static getDesignTypeForMethod(target, methodName) { | ||
const returnType = TSReflector.getMetadata('design:returntype', target, methodName); | ||
if (type === undefined && | ||
parameterTypes === undefined && | ||
returnType === undefined) { | ||
/* istanbul ignore next */ | ||
if (debug.enabled) { | ||
const targetName = decorator_factory_1.DecoratorFactory.getTargetName(target, methodName); | ||
debug('No design-time type metadata found while inspecting %s. ' + | ||
'Did you forget to enable TypeScript compiler option `emitDecoratorMetadata`?', targetName); | ||
} | ||
return undefined; | ||
} | ||
return { | ||
@@ -145,0 +168,0 @@ type, |
@@ -17,6 +17,6 @@ /** | ||
* @param key - The metadata key | ||
* @typeParam T - Type of the metadata value | ||
* @typeParam D - Type of the decorator | ||
* @typeParam V - Type of the metadata value | ||
* @typeParam DT - Type of the decorator | ||
*/ | ||
static create<T, D extends DecoratorType>(key: string): MetadataAccessor<T, D>; | ||
static create<V, DT extends DecoratorType>(key: string): MetadataAccessor<V, DT>; | ||
} | ||
@@ -60,13 +60,13 @@ /** | ||
/** | ||
* Type of the method itself. It is `Function` | ||
* Type of the method itself. It is `Function` for methods, `undefined` for the constructor. | ||
*/ | ||
type: Function; | ||
type: Function | undefined; | ||
/** | ||
* An array of parameter types | ||
* An array of parameter types. | ||
*/ | ||
parameterTypes: Function[]; | ||
/** | ||
* Return type | ||
* Return type, may be `undefined` (e.g. for constructors). | ||
*/ | ||
returnType: Function; | ||
returnType: Function | undefined; | ||
} |
@@ -13,2 +13,3 @@ "use strict"; | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
class MetadataAccessor { | ||
@@ -24,4 +25,4 @@ constructor(key) { | ||
* @param key - The metadata key | ||
* @typeParam T - Type of the metadata value | ||
* @typeParam D - Type of the decorator | ||
* @typeParam V - Type of the metadata value | ||
* @typeParam DT - Type of the decorator | ||
*/ | ||
@@ -28,0 +29,0 @@ static create(key) { |
{ | ||
"name": "@loopback/metadata", | ||
"version": "2.2.6", | ||
"version": "3.0.0", | ||
"description": "Utilities to help developers implement TypeScript decorators, define/merge metadata, and inspect metadata", | ||
@@ -32,8 +32,8 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@loopback/build": "^6.2.2", | ||
"@loopback/eslint-config": "^9.0.2", | ||
"@loopback/testlab": "^3.2.4", | ||
"@loopback/build": "^6.2.3", | ||
"@loopback/eslint-config": "^10.0.0", | ||
"@loopback/testlab": "^3.2.5", | ||
"@types/debug": "^4.1.5", | ||
"@types/lodash": "^4.14.160", | ||
"@types/node": "^10.17.28" | ||
"@types/lodash": "^4.14.161", | ||
"@types/node": "^10.17.34" | ||
}, | ||
@@ -56,3 +56,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "a3f54273814de63819e0d8bc86509f8a737800bb" | ||
"gitHead": "2b7d2ef44be0e3c19aee3316a2776d7fff6b0051" | ||
} |
@@ -124,13 +124,17 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
/** | ||
* Get the qualified name of a decoration target. For example: | ||
* ``` | ||
* class MyClass | ||
* MyClass.constructor[0] // First parameter of the constructor | ||
* MyClass.myStaticProperty | ||
* MyClass.myStaticMethod() | ||
* MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* MyClass.prototype.myProperty | ||
* MyClass.prototype.myMethod() | ||
* MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* ``` | ||
* Get the qualified name of a decoration target. | ||
* | ||
* @remarks | ||
* | ||
* Example of target names: | ||
* | ||
* - class MyClass | ||
* - MyClass.constructor[0] // First parameter of the constructor | ||
* - MyClass.myStaticProperty | ||
* - MyClass.myStaticMethod() | ||
* - MyClass.myStaticMethod[0] // First parameter of the myStaticMethod | ||
* - MyClass.prototype.myProperty | ||
* - MyClass.prototype.myMethod() | ||
* - MyClass.prototype.myMethod[1] // Second parameter of myMethod | ||
* | ||
* @param target - Class or prototype of a class | ||
@@ -341,7 +345,7 @@ * @param member - Optional property/method name | ||
protected static _createDecorator< | ||
T, | ||
M extends T | MetadataMap<T> | MetadataMap<T[]>, | ||
D extends DecoratorType | ||
>(key: MetadataKey<T, D>, spec: T, options?: DecoratorOptions): D { | ||
const inst = new this<T, M, D>(key.toString(), spec, options); | ||
S, | ||
MT extends S | MetadataMap<S> | MetadataMap<S[]>, | ||
DT extends DecoratorType | ||
>(key: MetadataKey<S, DT>, spec: S, options?: DecoratorOptions): DT { | ||
const inst = new this<S, MT, DT>(key.toString(), spec, options); | ||
return inst.create(); | ||
@@ -427,8 +431,8 @@ } | ||
*/ | ||
static createDecorator<T>( | ||
key: MetadataKey<T, ClassDecorator>, | ||
spec: T, | ||
static createDecorator<S>( | ||
key: MetadataKey<S, ClassDecorator>, | ||
spec: S, | ||
options?: DecoratorOptions, | ||
) { | ||
return super._createDecorator<T, T, ClassDecorator>(key, spec, options); | ||
return super._createDecorator<S, S, ClassDecorator>(key, spec, options); | ||
} | ||
@@ -489,8 +493,8 @@ } | ||
*/ | ||
static createDecorator<T>( | ||
key: MetadataKey<T, PropertyDecorator>, | ||
spec: T, | ||
static createDecorator<S>( | ||
key: MetadataKey<S, PropertyDecorator>, | ||
spec: S, | ||
options?: DecoratorOptions, | ||
) { | ||
return super._createDecorator<T, MetadataMap<T>, PropertyDecorator>( | ||
return super._createDecorator<S, MetadataMap<S>, PropertyDecorator>( | ||
key, | ||
@@ -556,8 +560,8 @@ spec, | ||
*/ | ||
static createDecorator<T>( | ||
key: MetadataKey<T, MethodDecorator>, | ||
spec: T, | ||
static createDecorator<S>( | ||
key: MetadataKey<S, MethodDecorator>, | ||
spec: S, | ||
options?: DecoratorOptions, | ||
) { | ||
return super._createDecorator<T, MetadataMap<T>, MethodDecorator>( | ||
return super._createDecorator<S, MetadataMap<S>, MethodDecorator>( | ||
key, | ||
@@ -650,8 +654,8 @@ spec, | ||
*/ | ||
static createDecorator<T>( | ||
key: MetadataKey<T, ParameterDecorator>, | ||
spec: T, | ||
static createDecorator<S>( | ||
key: MetadataKey<S, ParameterDecorator>, | ||
spec: S, | ||
options?: DecoratorOptions, | ||
) { | ||
return super._createDecorator<T, MetadataMap<T[]>, ParameterDecorator>( | ||
return super._createDecorator<S, MetadataMap<S[]>, ParameterDecorator>( | ||
key, | ||
@@ -786,8 +790,8 @@ spec, | ||
*/ | ||
static createDecorator<T>( | ||
key: MetadataKey<T, MethodDecorator>, | ||
spec: T, | ||
static createDecorator<S>( | ||
key: MetadataKey<S, MethodDecorator>, | ||
spec: S, | ||
options?: DecoratorOptions, | ||
) { | ||
return super._createDecorator<T, MetadataMap<T[]>, MethodDecorator>( | ||
return super._createDecorator<S, MetadataMap<S[]>, MethodDecorator>( | ||
key, | ||
@@ -794,0 +798,0 @@ spec, |
@@ -5,3 +5,4 @@ // Copyright IBM Corp. 2017,2019. All Rights Reserved. | ||
// License text available at https://opensource.org/licenses/MIT | ||
import debugModule from 'debug'; | ||
import {DecoratorFactory} from './decorator-factory'; | ||
import {NamespacedReflect, Reflector} from './reflect'; | ||
@@ -15,2 +16,4 @@ import { | ||
const debug = debugModule('loopback:metadata:inspector'); | ||
/** | ||
@@ -212,2 +215,7 @@ * TypeScript reflector without a namespace. The TypeScript compiler can be | ||
* @param propertyName - Property name | ||
* @returns Design time metadata. The return value is `undefined` when: | ||
* - The property has type `undefined`, `null` or a complex type like | ||
* `Partial<MyModel>`, `string | number`, `string[]`. | ||
* - The TypeScript project has not enabled the compiler option `emitDecoratorMetadata`. | ||
* - The code is written in vanilla JavaScript. | ||
*/ | ||
@@ -217,3 +225,3 @@ static getDesignTypeForProperty( | ||
propertyName: string, | ||
): Function { | ||
): Function | undefined { | ||
return TSReflector.getMetadata('design:type', target, propertyName); | ||
@@ -223,5 +231,8 @@ } | ||
/** | ||
* Get TypeScript design time type for a method | ||
* Get TypeScript design time type for a method. | ||
* @param target - Class or prototype | ||
* @param methodName - Method name | ||
* @returns Design time metadata. The return value is `undefined` | ||
* in projects that do not enable `emitDecoratorMetadata` | ||
* in TypeScript compiler options or are written in vanilla JavaScript. | ||
*/ | ||
@@ -231,3 +242,3 @@ static getDesignTypeForMethod( | ||
methodName: string, | ||
): DesignTimeMethodMetadata { | ||
): DesignTimeMethodMetadata | undefined { | ||
const type = TSReflector.getMetadata('design:type', target, methodName); | ||
@@ -244,2 +255,21 @@ const parameterTypes = TSReflector.getMetadata( | ||
); | ||
if ( | ||
type === undefined && | ||
parameterTypes === undefined && | ||
returnType === undefined | ||
) { | ||
/* istanbul ignore next */ | ||
if (debug.enabled) { | ||
const targetName = DecoratorFactory.getTargetName(target, methodName); | ||
debug( | ||
'No design-time type metadata found while inspecting %s. ' + | ||
'Did you forget to enable TypeScript compiler option `emitDecoratorMetadata`?', | ||
targetName, | ||
); | ||
} | ||
return undefined; | ||
} | ||
return { | ||
@@ -246,0 +276,0 @@ type, |
@@ -20,2 +20,3 @@ // Copyright IBM Corp. 2018,2019. All Rights Reserved. | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export class MetadataAccessor<T, D extends DecoratorType = DecoratorType> { | ||
@@ -31,7 +32,7 @@ private constructor(public readonly key: string) {} | ||
* @param key - The metadata key | ||
* @typeParam T - Type of the metadata value | ||
* @typeParam D - Type of the decorator | ||
* @typeParam V - Type of the metadata value | ||
* @typeParam DT - Type of the decorator | ||
*/ | ||
static create<T, D extends DecoratorType>(key: string) { | ||
return new MetadataAccessor<T, D>(key); | ||
static create<V, DT extends DecoratorType>(key: string) { | ||
return new MetadataAccessor<V, DT>(key); | ||
} | ||
@@ -81,13 +82,16 @@ } | ||
/** | ||
* Type of the method itself. It is `Function` | ||
* Type of the method itself. It is `Function` for methods, `undefined` for the constructor. | ||
*/ | ||
type: Function; | ||
type: Function | undefined; | ||
/** | ||
* An array of parameter types | ||
* An array of parameter types. | ||
*/ | ||
parameterTypes: Function[]; | ||
/** | ||
* Return type | ||
* Return type, may be `undefined` (e.g. for constructors). | ||
*/ | ||
returnType: Function; | ||
returnType: Function | undefined; | ||
} |
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
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
173464
2864