Does nestjs support Get and Post simultaneously on an action?

Viewed 796

I am new to nestjs and tring to apply Get and Post simultaneously on a method in my controller. For simplicity, I just post the core logic code snippet:

Customized Decorator

import { Get, Post } from "@nestjs/common";
import { RenderReact } from 'my-personal-package';

export function Page(path: string, view?: React.ComponentType, methodDecorators?: ((path?: string | string[]) => MethodDecorator)[]): MethodDecorator {
  return (target: any, key: string, desc: PropertyDescriptor) => {
    const decorators = [
      Get(path), // Add Get first.
      Post(path) // Add Post then.
    ];

    if (view) {
      decorators.push(RenderReact(view)); // RenderReact will return a MethodDecorator as well.
    }
    decorators.forEach(decorate => decorate(target, key, desc));
    return desc;
  };
}

Controller method:

@Page("my-path", ThisIsMyPageFunctionalComponent, [Post]) // Post was from @nestjs/common
async return() {
  // method logic
}

The array "decorators" at the very beginning in the Page function,

  1. Add Get, then Post, Only Post works.
  2. Add Post, then Get, Only Get works.

How can we apply Get/Post simultaneously here?

1 Answers

As @Micael Levi mentioned above, as the machanism of how decorator factory works, we can not apply both Get and Post in this way. I have tried it for a long time.

Please refer to the question here, like @Kim Kern posted

  1. We extracted common logic into a method.
  2. Separate the method Get and Post which will call the common logic.
Related