Page Contents

Home > @loopback/metadata > DecoratorFactory

DecoratorFactory class

Base factory class for decorator functions

Signature:

export declare class DecoratorFactory<T, // Type of the metadata spec for individual class/method/property/parameter
M extends T | MetadataMap<T> | MetadataMap<T[]>, // Type of the metadata
D extends DecoratorType> 

Constructors

Constructor Modifiers Description
(constructor)(key, spec, options)   Construct a new class decorator factory

Properties

Property Modifiers Type Description
key   string  
options   DecoratorOptions | undefined  
spec   T  
TARGET static string A constant to reference the target of a decoration

Methods

| Method | Modifiers | Description | | — | — | — | | _createDecorator(key, spec, options) | static | Create a decorator function | | allowInheritance() | | | | cloneDeep(val) | static | | | create() | | Create a decorator function of the given type. Each sub class MUST implement this method. | | decorate(target, member, descriptorOrIndex) | | Base implementation of the decorator function | | getNumberOfParameters(target, member) | static | Get the number of parameters for a given constructor or method | | getTarget(spec) | | Get the optional decoration target of a given spec | | getTargetName(target, member, descriptorOrIndex) | static | 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

| | inherit(inheritedMetadata) | | 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. | | mergeWithInherited(inheritedMetadata, target, member, descriptorOrIndex) | | 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. | | mergeWithOwn(ownMetadata, target, member, descriptorOrIndex) | | 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. | | withTarget(spec, target) | | Set a reference to the target class or prototype for a given spec if it’s an object |

Example

function classDecorator(spec: MySpec): ClassDecorator {
  return ClassDecoratorFactory.createDecorator('my-key', spec);
}

or

function classDecorator(spec: MySpec): ClassDecorator {
  const factory: ClassDecoratorFactory<MySpec>('my-key', spec);
  return factory.create();
}

These functions above declare @classDecorator that can be used as follows:

@classDecorator({x: 1})
class MyController {}