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

Simple sub-route controller delegation #1289

Closed
snowfrogdev opened this issue Nov 14, 2018 · 6 comments
Closed

Simple sub-route controller delegation #1289

snowfrogdev opened this issue Nov 14, 2018 · 6 comments

Comments

@snowfrogdev
Copy link

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Nested routes have been discussed in issues #255 and #389 which have led to the creation of the shekohex/nest-router package but I'm wondering if we couldn't implement something simpler, at the controller level. I really like how it is handled in the Jersey framework based on JAX-RS. Basically, inside your controller, you define delegation to another controller for a given sub-route. This short video gives a simple example of how it works: https://youtu.be/O4dAxOCYAUg?t=126.

In short, inside your controller, you define a path where, instead of returning a string or JSON object or whatever, you return an instance of another controller. The framework sees that and uses the "sub-controller" to handle the sub-route. How this works behind the scenes at the framework level, I do not know, but I'd be willing to investigate and see how something similar could be implemented in NestJS. Here is what it could look like:

cats.controller.ts

@Controller('cats')
export class CatsController {
  constructor(private readonly catsService: CatsService) {}

  @Post()
  async create(@Body() createCatDto: CreateCatDto) {
    this.catsService.create(createCatDto);
  }

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

  @Get(':catId')
  findOne(
    @Param('id', new ParseIntPipe())
    id,
  ) {
    // logic
  }

  @Path(':catId'/friends)
  getFriendsController(): FriendsController {
    return new FriendsController();
  }
}

friends.controller.ts

@Controller('/')
export class FriendsController {
  constructor() {}  

  @Get(':friendId')
  findOne(@Param('catId') catId, @Param('friendId') friendId {
    // logic
  }
}

I'd be willing to work on a PR for this feature if there is any interest for it.

@snowfrogdev snowfrogdev changed the title Simple route delegation Simple sub-route controller delegation Nov 15, 2018
@aspatari
Copy link

Mby django approach too will inspire someone https://www.django-rest-framework.org/api-guide/viewsets/#viewset-actions

@marcus-sa
Copy link

Have you tried using https://github.com/shekohex/nest-router ?

@snowfrogdev
Copy link
Author

@marcus-sa As mentioned above, I am aware of shekohex/nest-route. I'd prefer to deal with sub-routes at the controller level than having to define my routes at the module level. I opened this issue to see if others felt this way and if the maintainers would be willing to explore the possibility of including this functionality in NestJS.

@marcus-sa
Copy link

marcus-sa commented Nov 15, 2018

Hm, I fairly disagree at this point.
Nestjs is all about the module architecture which is inspired directly from Angular where everything is separated into modules, even the routing.
I find this to be far more manageable, and having the ability to only import what is necessary for your specific application is way better.

@kamilmysliwiec
Copy link
Member

I think that @marcus-sa response largely explains the reasons for closing this proposal.

@lock
Copy link

lock bot commented Sep 24, 2019

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 Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants