Overloaded routes in NestJS controller?

丶灬走出姿态 提交于 2019-12-11 19:32:26

问题


Is there a nice-looking way to create overloaded routes in NestJS application? I have some thoughts, but maybe I'm inventing a wheel. I couldn't find any ready approach though...

What I'm talking about is something like this (lets take https://github.com/nestjs/nest/blob/master/sample/01-cats-app/src/cats/cats.controller.ts as start point):

@Get()
async findAll(): Promise<Cat[]> {
  return this.catsService.findAll();
}

@Get()
@Roles('admin')
async findAllAdmin(): Promise<Cat[]> {
  return this.catsService.findAllAdmin();
}

In other words, I want have two routes with same URL, but distinguished by some other values (like role here).

My idea was to create my own decorator, instead of Get, which will fill some weight map, assign to each overloaded method unique path. And then, add middleware, which will get parameters from request, compare them against map, and do internal redirect (with next('route') or req.app.handle(req, res)) to appropriate new path.

But in that approach I couldn't get user from request, if they should be authenticated with AuthGuard on one of methods...

来源:https://stackoverflow.com/questions/54636005/overloaded-routes-in-nestjs-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!