Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom method decorator #217

Closed
MiguelSavignano opened this issue Mar 20, 2019 · 5 comments
Closed

Custom method decorator #217

MiguelSavignano opened this issue Mar 20, 2019 · 5 comments

Comments

@MiguelSavignano
Copy link

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}`;
  }
}
@kamilmysliwiec
Copy link
Member

If you override descriptor.value, SwaggerModule wouldn't be able to access the metadata defined on the method.

@creaux
Copy link

creaux commented Oct 18, 2019

@MiguelSavignano
Copy link
Author

@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.

@creaux
Copy link

creaux commented Oct 21, 2019

Looks like quiet cool solution.

@lock
Copy link

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.

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants