How To Get Metadata of Current Controller Method

Friday, February 28, 2020

We can enhance the controller methods by creating new decorators but first of all we need to get the annotated controller method instance of the current request and then to retrieve the metadata from it, so the request-response data flow can be extended accordingly in the sequence.

export async function getDecoratorMetadata<T>(
  context: RequestContext,
  key: MetadataKey<T, MethodDecorator>,
): Promise<T | undefined> {
  const name = await context.get<string>(CoreBindings.CONTROLLER_METHOD_NAME, {
    optional: true,
  if (!name) {
  const ctor = await context.get<ControllerClass>(
    {optional: true},
  if (!ctor) {
  return MetadataInspector.getMethodMetadata<T>(key, ctor.prototype, name);

CoreBindings.CONTROLLER_METHOD_NAME is bound to the method name of the controller for the current request and CoreBindings.CONTROLLER_CLASS is bound to the corresponding controller class, and then with MetadataInspector we can therefore retrieve the key-specified metadata the custom decorator defines.