nest icon indicating copy to clipboard operation
nest copied to clipboard

Global prefix for a module / route tree

Open lukgri opened this issue 9 years ago • 41 comments

Hi,

there is an older issue, which got implemented: https://github.com/nestjs/nest/issues/40 - allowing to setGlobalPrefix('api') on the app instance.

This is a very neat feature and since I'm looking to create some sort of a "route tree", it would be great to set some "route extending strategy".

Say I want to create a route tree like this: -> api (from app.setGlobalPrefix) --> /users (UsersModule with prefix 'users' for all of it's 'children' modules and controllers) ---> /posts (PostsModule with prefix 'posts' for all of it's 'children' modules and controllers) ----> /comments (Now I could declare a CommentsController with route 'comments' and this controller's route would be composed from all of the ancestors above, in this case: 'api/users/posts/comments' ----> /upvotes (this one would then be 'api/users/posts/upvotes')

that means I'd like to somehow set the 'module route prefix' for all of it's children's modules & controllers.

If there's already a way to achieve this, please point me in the right direction, otherwise it would be pretty nice to have this.

Thanks

lukgri avatar Nov 18 '17 19:11 lukgri

I'm also looking for something like this , It would be awesome to have a Control of Module prefix or another way to achieve this

shekohex avatar Nov 18 '17 20:11 shekohex

I agree, this will help keep our applications modular. At the moment I must specify my controller base as /api/users, /api/posts, /api/news etc. It's only a small inconvenience for small apps, but a redundancy nevertheless.

Zeldaze avatar Nov 18 '17 21:11 Zeldaze

I think there should be a path property on parent modules, like id in Angular modules.

wbhob avatar Nov 18 '17 23:11 wbhob

A reference/example on pure Javascript + Express of how to do it: http://katieleonard.ca/blog/2016/nested-routes-with-expressjs/

It's quite important with the issue #257 because in some cases you have nested elements:

\book:bookHash\chapter:chatperHash

And you want to test that bookHash exists (into the BookController) and also chapterHash exists (into the ChapterController)

gelito avatar Nov 19 '17 10:11 gelito

The issue is that the modules are not always just a tree. Sometimes they look more like a graph, for example, there might be a module, UsersModule, which contains users business logic and the UsersController inside. The module prefix is users. Now few modules may depend on the UsersModule, but the UsersModule should not 'extend' they API prefixes. Irrelevant whether this is a good practice or not, but it's possible.

kamilmysliwiec avatar Nov 20 '17 20:11 kamilmysliwiec

@kamilmysliwiec I don't know if it's a good or bad practice, but Angular has the ability of nested routes.

It's not obligatory and is easy to have a workaround, of course, but could be a good improvement.

gelito avatar Nov 20 '17 23:11 gelito

@gelito nested routes are a good idea, but I can't see a good way to achieve this now. As I said above, module prefix may cause a lot of issues 🙁

kamilmysliwiec avatar Nov 21 '17 07:11 kamilmysliwiec

Understood!

BTW great job for all!

gelito avatar Nov 21 '17 10:11 gelito

isn't there any hack or some sort of middleware that would let us do that?

lukgri avatar Nov 21 '17 10:11 lukgri

What is the possible issue with a module prefix?

wbhob avatar Nov 21 '17 14:11 wbhob

@wbhob i have explained it below ^

kamilmysliwiec avatar Nov 21 '17 14:11 kamilmysliwiec

Would it be possible to separate routing/controllers and things like services? The same way it's done in Angular for example, where you declare where to mount which modules into the routing tree but you can import and use a module in another without using any of it's routes?

ValentinFunk avatar Nov 22 '17 16:11 ValentinFunk

@Kamshak hmm.. any ideas about the API? I'm trying to imagine how we could do it well

kamilmysliwiec avatar Nov 22 '17 18:11 kamilmysliwiec

I think this will help @kamilmysliwiec https://github.com/angular/angular/blob/5.0.2/packages/router/src/router.ts#L190-L760 good luck :smiley: and also take a look add this article , it should give us any idea :+1: https://vsavkin.com/angular-2-router-d9e30599f9ea

shekohex avatar Nov 22 '17 19:11 shekohex

@shekohex haha 😄 well, I know how it's working in Angular, I was asking about the Nest API proposal

kamilmysliwiec avatar Nov 22 '17 19:11 kamilmysliwiec

Hahah 😂, sorry for misunderstand but here is what I'm trying to do right now I'm here trying to make a nested routes like a Parent route and a childs the simplest idea get in my mind is to make the ability to add a Parent controller to another one like this

@Controller('foo') class Foo { }
@Controller('bar', Foo) class Bar { }

then if I checked the Bar class route now it will be foo/bar instead of bar what I did , is just played with reflect metadata

export function Controller(prefix?: string, parentController?: Controller): ClassDecorator {
    let path = isUndefined(prefix) ? '/' : prefix;
    if(parentController) {
       const parentPath = Reflect.getMetadata(PATH_METADATA, parentController);
       path = parentPath + '/' + path; 
    }
    return (target: object) => {
        Reflect.defineMetadata(PATH_METADATA, path, target);
        Reflect.defineMetadata(PARENT_ROUTE, parentController, target);
    };
}

However, I have it now working , but I think it is not the best solution for this issue so any ideas ?

shekohex avatar Nov 22 '17 19:11 shekohex

@shekohex honestly, it's the best idea for now heh

kamilmysliwiec avatar Nov 22 '17 19:11 kamilmysliwiec

I think it's just a Hack :smile: anyway I'm now thinking how to make the child controller having access to his Parent eg: parms, body.. for example, If I have a 2 Controller PostsController, CommentsController and what I want to achieve is to have this routes

GET posts/:id
GET posts/:id/comments
@Controller('posts') 
  export class PostsController {
        @Get(':id')
        getPosts(@Param('id') id: number) {
            ...
            //go to database and returing the post
            ...
        }
    }

@Controller('comments', PostsController) 
   export class CommentsController {
        @Get()
        getPostComments() {
            // now I need to get the controle of the post
            // or post Id, to get it's comments
        }
    }

so any Help in this ? or any New Idea !

shekohex avatar Nov 22 '17 20:11 shekohex

@shekohex

  1. Use middleware instead
  2. Attach post to req and call next()

kamilmysliwiec avatar Nov 22 '17 20:11 kamilmysliwiec

@kamilmysliwiec: Will try to sketch out a quick consumer API. The challenge I see at the moment is that with the more declarative style you might loose some of the ease of the Decorators. (e.g. you'd always have to wire it up somewhere into the routing tree which might be tricky using decorators)

ValentinFunk avatar Nov 22 '17 22:11 ValentinFunk