question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom method decorator

See original GitHub issue

Hi, Im created a custom decorator for the controller method, this decorator print the function arguments and the result.

Issue: Swagger lose the query parameters

Screenshot from 2019-03-20 21-22-46

Minimal reproduction of the problem with instructions

import { Controller, Get, Query, Logger } from '@nestjs/common';
import { ApiModelPropertyOptional } from '@nestjs/swagger';

export const PrintLog = (target, name, descriptor) => {
  const className = target.constructor.name;
  const original = descriptor.value;

  descriptor.value = function(...args) {
    Logger.log(
      `Call with args: ${JSON.stringify(args)}`,
      `${className}#${name}`,
    );
    const result = original.apply(this, args);
    Logger.log(`Return: ${JSON.stringify(result)}`, `${className}#${name}`);
    return result;
  };
};

export class HelloDto {
  @ApiModelPropertyOptional()
  readonly name?: string;
}

@Controller()
export class AppController {
  @Get()
  @PrintLog
  getHello(@Query() input: HelloDto): string {
    return `Hello World! ${input.name}`;
  }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
MiguelSavignanocommented, Oct 21, 2019

@creaux Use middleware it’s a solution for this issue but I solved this issue using Javascript Proxy

export const PrintLog = (target, methodName, descriptor) => {
  const className = target.constructor.name;
  const original = descriptor.value;

  descriptor.value = new Proxy(original, {
    apply: function(target, thisArg, args) {
      Logger.log(
        `Call with args: ${JSON.stringify(args)}`,
        `${className}#${methodName}`,
      );

      const result = target.apply(thisArg, args);

      Logger.log(
        `Return: ${JSON.stringify(result)}`,
        `${className}#${methodName}`,
      );
      return result;
    },
  });
};

With Javascript Proxy I can override a descriptor.value and SwaggerModule would be able to access the metadata defined on the method.

0reactions
lock[bot]commented, Apr 25, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Method Decorators In Angular — A HTTP Cache ...
In Angular, Decorators are Normal Function which should be returning another function to be called. There are few types of decorators like Class...
Read more >
Angular - Custom method decorator which triggers console ...
A method decorator does exactly what you want to do. It intercepts the call of the decorated method. So you are able to...
Read more >
Documentation - Decorators - TypeScript
If we want to customize how a decorator is applied to a declaration, we can write a decorator factory. A Decorator Factory is...
Read more >
Building custom typescript decorators for angular
As mentioned earlier, decorators are actually just functions, it's as simple as that, and are called with whatever they are decorating. A method...
Read more >
How To Use Decorators in TypeScript - DigitalOcean
For a decorator called @decoratorA , you tell TypeScript it should call the function decoratorA . The decoratorA function will be called with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found