Reflector

Helper class providing Nest reflection capabilities.

  
class Reflector {
  static createDecorator<TParam, TTransformed = TParam>(options: CreateDecoratorOptions<TParam, TTransformed> = {}): ReflectableDecorator<TParam, TTransformed>
  get<TResult = any, TKey = any>(metadataKeyOrDecorator: TKey, target: any): TResult
  getAll<TResult extends any[] = any[], TKey = any>(metadataKeyOrDecorator: TKey, targets: any[]): TResult
  getAllAndMerge<TResult extends any[] | object = any[], TKey = any>(metadataKeyOrDecorator: TKey, targets: any[]): TResult
  getAllAndOverride<TResult = any, TKey = any>(metadataKeyOrDecorator: TKey, targets: any[]): TResult | undefined
}

See also

Static methods

createDecorator()

Creates a decorator that can be used to decorate classes and methods with metadata. Can be used as a strongly-typed alternative to @SetMetadata.


static createDecorator<TParam>(options?: CreateDecoratorOptions<TParam, TParam>): ReflectableDecorator<TParam>

Parameters

Option Type Description
options CreateDecoratorOptions

Decorator options.


Optional. Default is `undefined`.

Returns

ReflectableDecorator<TParam> A decorator function.


static createDecorator<TParam, TTransformed>(options: CreateDecoratorWithTransformOptions<TParam, TTransformed>): ReflectableDecorator<TParam, TTransformed>

Parameters

Option Type Description
options CreateDecoratorWithTransformOptions

Returns

ReflectableDecorator<TParam, TTransformed>

Methods

get()

Retrieve metadata for a specified key or decorator for a specified target.

Retrieve metadata for a reflectable decorator for a specified target.


get<T extends ReflectableDecorator<any>>(decorator: T, target: any): T extends ReflectableDecorator<any, infer R> ? R : unknown

Parameters

Option Type Description
decorator T

reflectable decorator created through Reflector.createDecorator

target any

context (decorated object) to retrieve metadata from

Examples

    
`const roles = this.reflector.get('roles', context.getHandler());`

Returns

T extends ReflectableDecorator<any, infer R> ? R : unknown

Retrieve metadata for a specified key for a specified target.


get<TResult = any, TKey = any>(metadataKey: TKey, target: any): TResult

Parameters

Option Type Description
metadataKey TKey

lookup key for metadata to retrieve

target any

context (decorated object) to retrieve metadata from

Examples

    
`const roles = this.reflector.get('roles', context.getHandler());`

Returns

TResult

getAll()

Retrieve metadata for a specified key or decorator for a specified set of targets.

Retrieve metadata for a specified decorator for a specified set of targets.


getAll<TParam = any, TTransformed = TParam>(decorator: ReflectableDecorator<TParam, TTransformed>, targets: any[]): TTransformed extends Array<any> ? TTransformed : TTransformed[]

Parameters

Option Type Description
decorator ReflectableDecorator

lookup decorator for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TTransformed extends Array<any> ? TTransformed : TTransformed[]

Retrieve metadata for a specified key for a specified set of targets.


getAll<TResult extends any[] = any[], TKey = any>(metadataKey: TKey, targets: any[]): TResult

Parameters

Option Type Description
metadataKey TKey

lookup key for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TResult

getAllAndMerge()

Retrieve metadata for a specified key or decorator for a specified set of targets and merge results.

Retrieve metadata for a specified decorator for a specified set of targets and merge results.


getAllAndMerge<TParam = any, TTransformed = TParam>(decorator: ReflectableDecorator<TParam, TTransformed>, targets: any[]): TTransformed extends Array<any> ? TTransformed : TTransformed extends object ? TTransformed : TTransformed[]

Parameters

Option Type Description
decorator ReflectableDecorator

lookup decorator for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TTransformed extends Array<any> ? TTransformed : TTransformed extends object ? TTransformed : TTransformed[]

Retrieve metadata for a specified key for a specified set of targets and merge results.


getAllAndMerge<TResult extends any[] | object = any[], TKey = any>(metadataKey: TKey, targets: any[]): TResult

Parameters

Option Type Description
metadataKey TKey

lookup key for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TResult

getAllAndOverride()

Retrieve metadata for a specified key or decorator for a specified set of targets and return a first not undefined value.

Retrieve metadata for a specified decorator for a specified set of targets and return a first not undefined value.


getAllAndOverride<TParam = any, TTransformed = TParam>(decorator: ReflectableDecorator<TParam, TTransformed>, targets: any[]): TTransformed

Parameters

Option Type Description
decorator ReflectableDecorator

lookup decorator for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TTransformed

Retrieve metadata for a specified key for a specified set of targets and return a first not undefined value.


getAllAndOverride<TResult = any, TKey = any>(metadataKey: TKey, targets: any[]): TResult

Parameters

Option Type Description
metadataKey TKey

lookup key for metadata to retrieve

targets any[]

context (decorated objects) to retrieve metadata from

Returns

TResult