@loopback/metadata
Advanced tools
Comparing version 2.1.6 to 2.2.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [2.2.0](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@2.1.6...@loopback/metadata@2.2.0) (2020-06-11) | ||
### Features | ||
* **context:** improve error reporting with more contextual information ([2a30484](https://github.com/strongloop/loopback-next/commit/2a30484f90b08803f14669524f8eb64c35031da9)) | ||
## [2.1.6](https://github.com/strongloop/loopback-next/compare/@loopback/metadata@2.1.5...@loopback/metadata@2.1.6) (2020-05-28) | ||
@@ -8,0 +19,0 @@ |
@@ -35,271 +35,269 @@ "use strict"; | ||
*/ | ||
let DecoratorFactory = /** @class */ (() => { | ||
class DecoratorFactory { | ||
/** | ||
* Construct a new class decorator factory | ||
* @param key - Metadata key | ||
* @param spec - Metadata object from the decorator function | ||
* @param options - Options for the decorator. Default to | ||
* `{allowInheritance: true}` if not provided | ||
*/ | ||
constructor(key, spec, options = {}) { | ||
var _a; | ||
this.key = key; | ||
this.spec = spec; | ||
this.options = options; | ||
this.options = Object.assign({ | ||
allowInheritance: true, | ||
cloneInputSpec: true, | ||
}, options); | ||
const defaultDecoratorName = this.constructor.name.replace(/Factory$/, ''); | ||
this.decoratorName = (_a = this.options.decoratorName) !== null && _a !== void 0 ? _a : defaultDecoratorName; | ||
if (this.options.cloneInputSpec) { | ||
this.spec = DecoratorFactory.cloneDeep(spec); | ||
} | ||
class DecoratorFactory { | ||
/** | ||
* Construct a new class decorator factory | ||
* @param key - Metadata key | ||
* @param spec - Metadata object from the decorator function | ||
* @param options - Options for the decorator. Default to | ||
* `{allowInheritance: true}` if not provided | ||
*/ | ||
constructor(key, spec, options = {}) { | ||
var _a; | ||
this.key = key; | ||
this.spec = spec; | ||
this.options = options; | ||
this.options = Object.assign({ | ||
allowInheritance: true, | ||
cloneInputSpec: true, | ||
}, options); | ||
const defaultDecoratorName = this.constructor.name.replace(/Factory$/, ''); | ||
this.decoratorName = (_a = this.options.decoratorName) !== null && _a !== void 0 ? _a : defaultDecoratorName; | ||
if (this.options.cloneInputSpec) { | ||
this.spec = DecoratorFactory.cloneDeep(spec); | ||
} | ||
allowInheritance() { | ||
return !!(this.options && this.options.allowInheritance); | ||
} | ||
allowInheritance() { | ||
var _a; | ||
return !!((_a = this.options) === null || _a === void 0 ? void 0 : _a.allowInheritance); | ||
} | ||
/** | ||
* Inherit metadata from base classes. By default, this method merges base | ||
* metadata into the spec if `allowInheritance` is set to `true`. To customize | ||
* the behavior, this method can be overridden by sub classes. | ||
* | ||
* @param inheritedMetadata - Metadata from base classes for the member | ||
*/ | ||
inherit(inheritedMetadata) { | ||
if (!this.allowInheritance()) | ||
return this.spec; | ||
if (inheritedMetadata == null) | ||
return this.spec; | ||
if (this.spec == null) | ||
return inheritedMetadata; | ||
if (typeof inheritedMetadata !== 'object') | ||
return this.spec; | ||
if (Array.isArray(inheritedMetadata) || Array.isArray(this.spec)) { | ||
// For arrays, we don't merge | ||
return this.spec; | ||
} | ||
/** | ||
* Inherit metadata from base classes. By default, this method merges base | ||
* metadata into the spec if `allowInheritance` is set to `true`. To customize | ||
* the behavior, this method can be overridden by sub classes. | ||
* | ||
* @param inheritedMetadata - Metadata from base classes for the member | ||
*/ | ||
inherit(inheritedMetadata) { | ||
if (!this.allowInheritance()) | ||
return this.spec; | ||
if (inheritedMetadata == null) | ||
return this.spec; | ||
if (this.spec == null) | ||
return inheritedMetadata; | ||
if (typeof inheritedMetadata !== 'object') | ||
return this.spec; | ||
if (Array.isArray(inheritedMetadata) || Array.isArray(this.spec)) { | ||
// For arrays, we don't merge | ||
return this.spec; | ||
} | ||
return Object.assign(inheritedMetadata, this.spec); | ||
return Object.assign(inheritedMetadata, this.spec); | ||
} | ||
/** | ||
* 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 | ||
* ``` | ||
* @param target - Class or prototype of a class | ||
* @param member - Optional property/method name | ||
* @param descriptorOrIndex - Optional method descriptor or parameter index | ||
*/ | ||
static getTargetName(target, member, descriptorOrIndex) { | ||
let name = target instanceof Function | ||
? target.name | ||
: `${target.constructor.name}.prototype`; | ||
if (member == null && descriptorOrIndex == null) { | ||
return `class ${name}`; | ||
} | ||
/** | ||
* 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 | ||
* ``` | ||
* @param target - Class or prototype of a class | ||
* @param member - Optional property/method name | ||
* @param descriptorOrIndex - Optional method descriptor or parameter index | ||
*/ | ||
static getTargetName(target, member, descriptorOrIndex) { | ||
let name = target instanceof Function | ||
? target.name | ||
: `${target.constructor.name}.prototype`; | ||
if (member == null && descriptorOrIndex == null) { | ||
return `class ${name}`; | ||
} | ||
if (member == null) | ||
member = 'constructor'; | ||
const memberAccessor = typeof member === 'symbol' ? '[' + member.toString() + ']' : '.' + member; | ||
if (typeof descriptorOrIndex === 'number') { | ||
// Parameter | ||
name = `${name}${memberAccessor}[${descriptorOrIndex}]`; | ||
} | ||
else if (descriptorOrIndex != null) { | ||
name = `${name}${memberAccessor}()`; | ||
} | ||
else { | ||
name = `${name}${memberAccessor}`; | ||
} | ||
return name; | ||
if (member == null || member === '') | ||
member = 'constructor'; | ||
const memberAccessor = typeof member === 'symbol' ? '[' + member.toString() + ']' : '.' + member; | ||
if (typeof descriptorOrIndex === 'number') { | ||
// Parameter | ||
name = `${name}${memberAccessor}[${descriptorOrIndex}]`; | ||
} | ||
/** | ||
* Get the number of parameters for a given constructor or method | ||
* @param target - Class or the prototype | ||
* @param member - Method name | ||
*/ | ||
static getNumberOfParameters(target, member) { | ||
if (typeof target === 'function' && !member) { | ||
// constructor | ||
return target.length; | ||
} | ||
else { | ||
// target[member] is a function | ||
const method = target[member]; | ||
return method.length; | ||
} | ||
else if (descriptorOrIndex != null) { | ||
name = `${name}${memberAccessor}()`; | ||
} | ||
/** | ||
* Set a reference to the target class or prototype for a given spec if | ||
* it's an object | ||
* @param spec - Metadata spec | ||
* @param target - Target of the decoration. It is a class or the prototype of | ||
* a class. | ||
*/ | ||
withTarget(spec, target) { | ||
if (typeof spec === 'object' && spec != null) { | ||
// Add a hidden property for the `target` | ||
Object.defineProperty(spec, DecoratorFactory.TARGET, { | ||
value: target, | ||
enumerable: false, | ||
// Make sure it won't be redefined on the same object | ||
configurable: false, | ||
}); | ||
} | ||
return spec; | ||
else { | ||
name = `${name}${memberAccessor}`; | ||
} | ||
/** | ||
* Get the optional decoration target of a given spec | ||
* @param spec - Metadata spec | ||
*/ | ||
getTarget(spec) { | ||
if (typeof spec === 'object' && spec != null) { | ||
const specWithTarget = spec; | ||
return specWithTarget[DecoratorFactory.TARGET]; | ||
} | ||
else { | ||
return undefined; | ||
} | ||
return name; | ||
} | ||
/** | ||
* Get the number of parameters for a given constructor or method | ||
* @param target - Class or the prototype | ||
* @param member - Method name | ||
*/ | ||
static getNumberOfParameters(target, member) { | ||
if (typeof target === 'function' && !member) { | ||
// constructor | ||
return target.length; | ||
} | ||
/** | ||
* This method is called by the default implementation of the decorator | ||
* function to merge the spec argument from the decoration with the inherited | ||
* metadata for a class, all properties, all methods, or all method | ||
* parameters that are decorated by this decorator. | ||
* | ||
* It MUST be overridden by subclasses to process inherited metadata. | ||
* | ||
* @param inheritedMetadata - Metadata inherited from the base classes | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
mergeWithInherited(inheritedMetadata, target, member, descriptorOrIndex) { | ||
throw new Error(`mergeWithInherited() is not implemented for ${this.decoratorName}`); | ||
else { | ||
// target[member] is a function | ||
const method = target[member]; | ||
return method.length; | ||
} | ||
/** | ||
* This method is called by the default implementation of the decorator | ||
* function to merge the spec argument from the decoration with the own | ||
* metadata for a class, all properties, all methods, or all method | ||
* parameters that are decorated by this decorator. | ||
* | ||
* It MUST be overridden by subclasses to process own metadata. | ||
* | ||
* @param ownMetadata - Own Metadata exists locally on the target | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
mergeWithOwn(ownMetadata, target, member, descriptorOrIndex) { | ||
throw new Error(`mergeWithOwn() is not implemented for ${this.decoratorName}`); | ||
} | ||
/** | ||
* Set a reference to the target class or prototype for a given spec if | ||
* it's an object | ||
* @param spec - Metadata spec | ||
* @param target - Target of the decoration. It is a class or the prototype of | ||
* a class. | ||
*/ | ||
withTarget(spec, target) { | ||
if (typeof spec === 'object' && spec != null) { | ||
// Add a hidden property for the `target` | ||
Object.defineProperty(spec, DecoratorFactory.TARGET, { | ||
value: target, | ||
enumerable: false, | ||
// Make sure it won't be redefined on the same object | ||
configurable: false, | ||
}); | ||
} | ||
/** | ||
* Create an error to report if the decorator is applied to the target more | ||
* than once | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
duplicateDecorationError(target, member, descriptorOrIndex) { | ||
const targetName = DecoratorFactory.getTargetName(target, member, descriptorOrIndex); | ||
return new Error(`${this.decoratorName} cannot be applied more than once on ${targetName}`); | ||
return spec; | ||
} | ||
/** | ||
* Get the optional decoration target of a given spec | ||
* @param spec - Metadata spec | ||
*/ | ||
getTarget(spec) { | ||
if (typeof spec === 'object' && spec != null) { | ||
const specWithTarget = spec; | ||
return specWithTarget[DecoratorFactory.TARGET]; | ||
} | ||
/** | ||
* Create a decorator function of the given type. Each sub class MUST | ||
* implement this method. | ||
*/ | ||
create() { | ||
throw new Error(`create() is not implemented for ${this.decoratorName}`); | ||
else { | ||
return undefined; | ||
} | ||
/** | ||
* Base implementation of the decorator function | ||
* @param target - Decorator target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional method descriptor or parameter index | ||
*/ | ||
decorate(target, member, descriptorOrIndex) { | ||
const targetName = DecoratorFactory.getTargetName(target, member, descriptorOrIndex); | ||
let meta = reflect_1.Reflector.getOwnMetadata(this.key, target); | ||
if (meta == null && this.allowInheritance()) { | ||
// Clone the base metadata so that it won't be accidentally | ||
// mutated by sub classes | ||
meta = DecoratorFactory.cloneDeep(reflect_1.Reflector.getMetadata(this.key, target)); | ||
meta = this.mergeWithInherited(meta, target, member, descriptorOrIndex); | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('%s: %j', targetName, meta); | ||
} | ||
reflect_1.Reflector.defineMetadata(this.key, meta, target); | ||
} | ||
/** | ||
* This method is called by the default implementation of the decorator | ||
* function to merge the spec argument from the decoration with the inherited | ||
* metadata for a class, all properties, all methods, or all method | ||
* parameters that are decorated by this decorator. | ||
* | ||
* It MUST be overridden by subclasses to process inherited metadata. | ||
* | ||
* @param inheritedMetadata - Metadata inherited from the base classes | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
mergeWithInherited(inheritedMetadata, target, member, descriptorOrIndex) { | ||
throw new Error(`mergeWithInherited() is not implemented for ${this.decoratorName}`); | ||
} | ||
/** | ||
* This method is called by the default implementation of the decorator | ||
* function to merge the spec argument from the decoration with the own | ||
* metadata for a class, all properties, all methods, or all method | ||
* parameters that are decorated by this decorator. | ||
* | ||
* It MUST be overridden by subclasses to process own metadata. | ||
* | ||
* @param ownMetadata - Own Metadata exists locally on the target | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
mergeWithOwn(ownMetadata, target, member, descriptorOrIndex) { | ||
throw new Error(`mergeWithOwn() is not implemented for ${this.decoratorName}`); | ||
} | ||
/** | ||
* Create an error to report if the decorator is applied to the target more | ||
* than once | ||
* @param target - Decoration target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional parameter index or method descriptor | ||
*/ | ||
duplicateDecorationError(target, member, descriptorOrIndex) { | ||
const targetName = DecoratorFactory.getTargetName(target, member, descriptorOrIndex); | ||
return new Error(`${this.decoratorName} cannot be applied more than once on ${targetName}`); | ||
} | ||
/** | ||
* Create a decorator function of the given type. Each sub class MUST | ||
* implement this method. | ||
*/ | ||
create() { | ||
throw new Error(`create() is not implemented for ${this.decoratorName}`); | ||
} | ||
/** | ||
* Base implementation of the decorator function | ||
* @param target - Decorator target | ||
* @param member - Optional property or method | ||
* @param descriptorOrIndex - Optional method descriptor or parameter index | ||
*/ | ||
decorate(target, member, descriptorOrIndex) { | ||
const targetName = DecoratorFactory.getTargetName(target, member, descriptorOrIndex); | ||
let meta = reflect_1.Reflector.getOwnMetadata(this.key, target); | ||
if (meta == null && this.allowInheritance()) { | ||
// Clone the base metadata so that it won't be accidentally | ||
// mutated by sub classes | ||
meta = DecoratorFactory.cloneDeep(reflect_1.Reflector.getMetadata(this.key, target)); | ||
meta = this.mergeWithInherited(meta, target, member, descriptorOrIndex); | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('%s: %j', targetName, meta); | ||
} | ||
else { | ||
meta = this.mergeWithOwn(meta, target, member, descriptorOrIndex); | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('%s: %j', targetName, meta); | ||
} | ||
reflect_1.Reflector.defineMetadata(this.key, meta, target); | ||
reflect_1.Reflector.defineMetadata(this.key, meta, target); | ||
} | ||
else { | ||
meta = this.mergeWithOwn(meta, target, member, descriptorOrIndex); | ||
/* istanbul ignore if */ | ||
if (debug.enabled) { | ||
debug('%s: %j', targetName, meta); | ||
} | ||
reflect_1.Reflector.defineMetadata(this.key, meta, target); | ||
} | ||
/** | ||
* Create a decorator function | ||
* @param key - Metadata key | ||
* @param spec - Metadata object from the decorator function | ||
* @param options - Options for the decorator | ||
*/ | ||
static _createDecorator(key, spec, options) { | ||
const inst = new this(key.toString(), spec, options); | ||
return inst.create(); | ||
} | ||
static cloneDeep(val) { | ||
if (typeof val !== 'object') | ||
return val; | ||
return lodash_1.default.cloneDeepWith(val, v => { | ||
if (typeof v !== 'object') | ||
return v; | ||
if (v == null) | ||
return v; | ||
if (v.constructor != null && | ||
!DecoratorFactory._cloneableTypes.includes(v.constructor)) { | ||
// Do not clone instances of classes/constructors, such as Date | ||
return v; | ||
} | ||
return undefined; | ||
}); | ||
} | ||
} | ||
/** | ||
* A constant to reference the target of a decoration | ||
* Create a decorator function | ||
* @param key - Metadata key | ||
* @param spec - Metadata object from the decorator function | ||
* @param options - Options for the decorator | ||
*/ | ||
DecoratorFactory.TARGET = '__decoratorTarget'; | ||
// See https://github.com/lodash/lodash/blob/master/.internal/baseClone.js | ||
DecoratorFactory._cloneableTypes = [ | ||
Object, | ||
Array, | ||
Set, | ||
Map, | ||
RegExp, | ||
Date, | ||
Buffer, | ||
ArrayBuffer, | ||
Float32Array, | ||
Float64Array, | ||
Int8Array, | ||
Int16Array, | ||
Int32Array, | ||
Uint8Array, | ||
Uint8ClampedArray, | ||
Uint16Array, | ||
Uint32Array, | ||
]; | ||
return DecoratorFactory; | ||
})(); | ||
static _createDecorator(key, spec, options) { | ||
const inst = new this(key.toString(), spec, options); | ||
return inst.create(); | ||
} | ||
static cloneDeep(val) { | ||
if (typeof val !== 'object') | ||
return val; | ||
return lodash_1.default.cloneDeepWith(val, v => { | ||
if (typeof v !== 'object') | ||
return v; | ||
if (v == null) | ||
return v; | ||
if (v.constructor != null && | ||
!DecoratorFactory._cloneableTypes.includes(v.constructor)) { | ||
// Do not clone instances of classes/constructors, such as Date | ||
return v; | ||
} | ||
return undefined; | ||
}); | ||
} | ||
} | ||
exports.DecoratorFactory = DecoratorFactory; | ||
/** | ||
* A constant to reference the target of a decoration | ||
*/ | ||
DecoratorFactory.TARGET = '__decoratorTarget'; | ||
// See https://github.com/lodash/lodash/blob/master/.internal/baseClone.js | ||
DecoratorFactory._cloneableTypes = [ | ||
Object, | ||
Array, | ||
Set, | ||
Map, | ||
RegExp, | ||
Date, | ||
Buffer, | ||
ArrayBuffer, | ||
Float32Array, | ||
Float64Array, | ||
Int8Array, | ||
Int16Array, | ||
Int32Array, | ||
Uint8Array, | ||
Uint8ClampedArray, | ||
Uint16Array, | ||
Uint32Array, | ||
]; | ||
/** | ||
* Factory for class decorators | ||
@@ -306,0 +304,0 @@ */ |
@@ -19,143 +19,140 @@ "use strict"; | ||
*/ | ||
let MetadataInspector = /** @class */ (() => { | ||
class MetadataInspector { | ||
/** | ||
* Get the metadata associated with the given key for a given class | ||
* @param key - Metadata key | ||
* @param target - Class that contains the metadata | ||
* @param options - Options for inspection | ||
*/ | ||
static getClassMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Define metadata for the given target | ||
* @param key - Metadata key | ||
* @param value - Metadata value | ||
* @param target - Target for the metadata | ||
* @param member - Optional property or method name | ||
*/ | ||
static defineMetadata(key, value, target, member) { | ||
reflect_1.Reflector.defineMetadata(key.toString(), value, target, member); | ||
} | ||
/** | ||
* Get the metadata associated with the given key for all methods of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param options - Options for inspection | ||
*/ | ||
static getAllMethodMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a given method of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param options - Options for inspection | ||
*/ | ||
static getMethodMetadata(key, target, methodName, options) { | ||
methodName = methodName !== null && methodName !== void 0 ? methodName : ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for all properties of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param options - Options for inspection | ||
*/ | ||
static getAllPropertyMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a given property of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static properties or prototype for instance | ||
* properties | ||
* @param propertyName - Property name | ||
* @param options - Options for inspection | ||
*/ | ||
static getPropertyMetadata(key, target, propertyName, options) { | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[propertyName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for all parameters of a | ||
* given method | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param options - Options for inspection | ||
*/ | ||
static getAllParameterMetadata(key, target, methodName, options) { | ||
methodName = methodName !== null && methodName !== void 0 ? methodName : ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a parameter of a given | ||
* method by index | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param index - Index of the parameter, starting with 0 | ||
* @param options - Options for inspection | ||
*/ | ||
static getParameterMetadata(key, target, methodName, index, options) { | ||
methodName = methodName || ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
const params = meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
return params === null || params === void 0 ? void 0 : params[index]; | ||
} | ||
/** | ||
* Get TypeScript design time type for a property | ||
* @param target - Class or prototype | ||
* @param propertyName - Property name | ||
*/ | ||
static getDesignTypeForProperty(target, propertyName) { | ||
return TSReflector.getMetadata('design:type', target, propertyName); | ||
} | ||
/** | ||
* Get TypeScript design time type for a method | ||
* @param target - Class or prototype | ||
* @param methodName - Method name | ||
*/ | ||
static getDesignTypeForMethod(target, methodName) { | ||
const type = TSReflector.getMetadata('design:type', target, methodName); | ||
const parameterTypes = TSReflector.getMetadata('design:paramtypes', target, methodName); | ||
const returnType = TSReflector.getMetadata('design:returntype', target, methodName); | ||
return { | ||
type, | ||
parameterTypes, | ||
returnType, | ||
}; | ||
} | ||
class MetadataInspector { | ||
/** | ||
* Get the metadata associated with the given key for a given class | ||
* @param key - Metadata key | ||
* @param target - Class that contains the metadata | ||
* @param options - Options for inspection | ||
*/ | ||
static getClassMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Expose Reflector, which is a wrapper of `Reflect` and it uses `loopback` | ||
* as the namespace prefix for all metadata keys | ||
* Define metadata for the given target | ||
* @param key - Metadata key | ||
* @param value - Metadata value | ||
* @param target - Target for the metadata | ||
* @param member - Optional property or method name | ||
*/ | ||
MetadataInspector.Reflector = reflect_1.Reflector; | ||
static defineMetadata(key, value, target, member) { | ||
reflect_1.Reflector.defineMetadata(key.toString(), value, target, member); | ||
} | ||
/** | ||
* Expose the reflector for TypeScript design-time metadata | ||
* Get the metadata associated with the given key for all methods of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param options - Options for inspection | ||
*/ | ||
MetadataInspector.DesignTimeReflector = TSReflector; | ||
return MetadataInspector; | ||
})(); | ||
static getAllMethodMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a given method of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param options - Options for inspection | ||
*/ | ||
static getMethodMetadata(key, target, methodName, options) { | ||
methodName = methodName !== null && methodName !== void 0 ? methodName : ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for all properties of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param options - Options for inspection | ||
*/ | ||
static getAllPropertyMetadata(key, target, options) { | ||
return (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a given property of the | ||
* target class or prototype | ||
* @param key - Metadata key | ||
* @param target - Class for static properties or prototype for instance | ||
* properties | ||
* @param propertyName - Property name | ||
* @param options - Options for inspection | ||
*/ | ||
static getPropertyMetadata(key, target, propertyName, options) { | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[propertyName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for all parameters of a | ||
* given method | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param options - Options for inspection | ||
*/ | ||
static getAllParameterMetadata(key, target, methodName, options) { | ||
methodName = methodName !== null && methodName !== void 0 ? methodName : ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
return meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
} | ||
/** | ||
* Get the metadata associated with the given key for a parameter of a given | ||
* method by index | ||
* @param key - Metadata key | ||
* @param target - Class for static methods or prototype for instance methods | ||
* @param methodName - Method name. If not present, default to '' to use | ||
* the constructor | ||
* @param index - Index of the parameter, starting with 0 | ||
* @param options - Options for inspection | ||
*/ | ||
static getParameterMetadata(key, target, methodName, index, options) { | ||
methodName = methodName || ''; | ||
const meta = (options === null || options === void 0 ? void 0 : options.ownMetadataOnly) ? reflect_1.Reflector.getOwnMetadata(key.toString(), target) | ||
: reflect_1.Reflector.getMetadata(key.toString(), target); | ||
const params = meta === null || meta === void 0 ? void 0 : meta[methodName]; | ||
return params === null || params === void 0 ? void 0 : params[index]; | ||
} | ||
/** | ||
* Get TypeScript design time type for a property | ||
* @param target - Class or prototype | ||
* @param propertyName - Property name | ||
*/ | ||
static getDesignTypeForProperty(target, propertyName) { | ||
return TSReflector.getMetadata('design:type', target, propertyName); | ||
} | ||
/** | ||
* Get TypeScript design time type for a method | ||
* @param target - Class or prototype | ||
* @param methodName - Method name | ||
*/ | ||
static getDesignTypeForMethod(target, methodName) { | ||
const type = TSReflector.getMetadata('design:type', target, methodName); | ||
const parameterTypes = TSReflector.getMetadata('design:paramtypes', target, methodName); | ||
const returnType = TSReflector.getMetadata('design:returntype', target, methodName); | ||
return { | ||
type, | ||
parameterTypes, | ||
returnType, | ||
}; | ||
} | ||
} | ||
exports.MetadataInspector = MetadataInspector; | ||
/** | ||
* Expose Reflector, which is a wrapper of `Reflect` and it uses `loopback` | ||
* as the namespace prefix for all metadata keys | ||
*/ | ||
MetadataInspector.Reflector = reflect_1.Reflector; | ||
/** | ||
* Expose the reflector for TypeScript design-time metadata | ||
*/ | ||
MetadataInspector.DesignTimeReflector = TSReflector; | ||
//# sourceMappingURL=inspector.js.map |
{ | ||
"name": "@loopback/metadata", | ||
"version": "2.1.6", | ||
"description": "LoopBack's metadata utilities for reflection and decoration", | ||
"version": "2.2.0", | ||
"description": "Utilities to help developers implement TypeScript decorators, define/merge metadata, and inspect metadata", | ||
"main": "dist/index.js", | ||
@@ -22,2 +22,5 @@ "types": "dist/index.d.ts", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
@@ -30,8 +33,8 @@ "debug": "^4.1.1", | ||
"devDependencies": { | ||
"@loopback/build": "^5.4.2", | ||
"@loopback/eslint-config": "^8.0.0", | ||
"@loopback/testlab": "^3.1.6", | ||
"@loopback/build": "^5.4.3", | ||
"@loopback/eslint-config": "^8.0.1", | ||
"@loopback/testlab": "^3.1.7", | ||
"@types/debug": "^4.1.5", | ||
"@types/lodash": "^4.14.153", | ||
"@types/node": "^10.17.24" | ||
"@types/lodash": "^4.14.155", | ||
"@types/node": "^10.17.26" | ||
}, | ||
@@ -49,5 +52,2 @@ "keywords": [ | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": { | ||
@@ -58,3 +58,3 @@ "type": "git", | ||
}, | ||
"gitHead": "64afb3616e94b96703524aba8be722efefa7c2c5" | ||
"gitHead": "f31b7e6de5a405a015cdd774f63d699b35d943cc" | ||
} |
@@ -101,3 +101,3 @@ // Copyright IBM Corp. 2017,2020. All Rights Reserved. | ||
protected allowInheritance(): boolean { | ||
return !!(this.options && this.options.allowInheritance); | ||
return !!this.options?.allowInheritance; | ||
} | ||
@@ -152,3 +152,3 @@ | ||
} | ||
if (member == null) member = 'constructor'; | ||
if (member == null || member === '') member = 'constructor'; | ||
@@ -155,0 +155,0 @@ const memberAccessor = |
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
167409
2792