typescript-rest icon indicating copy to clipboard operation
typescript-rest copied to clipboard

Can I use @Get('hello') instead of @GET and @Path('hello')?

Open vellengs opened this issue 6 years ago • 3 comments

Can I use @Get('hello') instead of @GET and @Path('hello'). That's simple and consistency with other framework.

vellengs avatar Jun 13 '18 09:06 vellengs

Unfortunately no... But I like the ideia. We can add this feature in next release

thiagobustamante avatar Jun 26 '18 14:06 thiagobustamante

In decorators.ts Could be as simple as:

export function GETMapping(path: string) {
  return function (...args: any[]) {
    args = _.without(args, undefined);
    if (args.length === 3 && typeof args[2] === 'object') {
      processHttpVerb(args[0], args[1], HttpMethod.GET);
      return PathMethodDecorator.apply(this, [args[0], args[1], args[2], path]);
    }

    throw new Error('Invalid @GETMapping Decorator declaration.');
  };
}

It only works for method decorator.

Before:

@Path('/hello')
export class HelloController {

  /**
   * Send a greeting message.
   * @param name The name that will receive our greeting message
   */
  @GET
  @Path(':name')
  sayHello(@PathParam('name') name: string): string {
    return 'Hello ' + name;
  }
}

After:

@Path('/hello')
export class HelloController {

  /**
   * Send a greeting message.
   * @param name The name that will receive our greeting message
   */
  @GETMapping(':name')
  sayHello(@PathParam('name') name: string): string {
    return 'Hello ' + name;
  }
}

d0whc3r avatar Oct 12 '18 02:10 d0whc3r

I just submited a pull request https://github.com/thiagobustamante/typescript-rest/pull/64

d0whc3r avatar Oct 12 '18 02:10 d0whc3r