-
Notifications
You must be signed in to change notification settings - Fork 36
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
Nested routes do not keep prefix #21
Comments
I just figured out that an alternative workaround is to repeat the @Module({
imports: [
RouterModule.forRoutes([
{ path: '/prefix', module: SubModule },
]),
SubModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {} Obviously this isn't desirable either, because of the duplication. |
Hi @y0hy0h , unfortunately that's not an option right now, it's one of Nest design gools, the isolations. when i was playing with nest, i have a fork of it, actually i achieved something similar to this nestjs/nest#255 (comment) but there is something you may not have noticed, the // root.module.ts
...
RouterModule.forRoutes([
{ path: '/prefix', module: AppModule },
{ path: '/prefix', module: SubModule },
]),
... you could just // root.module.ts
RouterModule.forRoutes([ { path: '/prefix', children: [AppModule, SubModule] } ]), i know, that's not what you are looking for, but, well.. hmmm... it will work as expected. |
That tip is still useful, thanks! Reads slightly better. I think for now I can manage without this. I just thought that it would work this way. Is there no way for the RouterModule to prefix the imports of the modules it is configured for? |
i think there is one way to do this, using the but that's not the problem, its about the design goal, maybe we could just open an issue in i will keep this issue opened until i can get this working. Regards. |
Hi @y0hy0h , unfortunately after thinking about that i just came to conclusion, for some reasons we can't do it this way . let me explain :1. Breaking Changes:this will make a very big breaking changes, it is obvious. 2. Unpredictable results:yes, it is another problem, this was tricky to get in, just imagine that RouterModule.forRoutes([
{ path: '/a', module: ModuleA },
{ path: '/c', module: ModuleC },
]),
3. Implementation:it's not a big issue, but it gets tricky when implementing such that thing, any help here ? so if anyone can make good solutions for these issues, and of course any PRs are more that welcomes |
Thanks for your explanation! I just wanted to check whether I didn't understand something, but I get why it's difficult to design and implement. Just a quick remark: In your 2. example I would expect Thanks for your answer! |
So let's see if someone have a good idea to solve this issue, and it will be awesome if there is PR too 😀 |
In addition to nested routes keeping their prefix...I think it would also be very useful to allow routes that have nested children to be parameterized. I am not sure how this would need to work, and if I find the time I'd like to take a look at nest-router and see if I could fix/enhance it...but I need the ability to handle both parameter-less routes, as well as prameterized routes. If I am looking up say users, and users can have comments. I might need the following routes:
So there are potentially multiple parent/child relationships here. In one case, we have all comments for all users. In the other, we have all comments for a specific user. These relationships need to be defined rather explicitly, as trying to get all comments for all users, sorted by date and possibly broken into pages, by querying each user's comments one at a time is largely untennable. I wonder if the following might be possible: RouterModule.forRoutes([
{ path: '/users', module: UserModule },
{ path: '/users/comments', module: CommentModule },
{ path: '/users/:userId/comments', module: UserCommentModule },
]) Or, something along these lines. The modules would then be able to support all the necessary routes, each:
This kind of hierarchical API is very common for the projects I work on. We prefer to hierarchically relate entities as appropriate, and the same entities may even be accessible from more than one path. As an even deeper example, we my also have
|
@techaks were you able to get nested child route parameterization working? This is a requirement for us in our app as well. |
@jordancue I've tried the example that you mentioned. It works fine but the swagger UI plugin doesn't properly prepare the URL as we expect for example when we try to do something as |
Is there any official word on the full range of hierarchical routs with nestjs? I listed a number of possible use cases we often utilize ourselves in my previous post... Would be nice to know if/when all of those options (and, of course, deeper nesting to the nth level) would be supported by nestjs. |
That's a bug in Swagger Module it self, see #3 i reopened that issue and made a PR fix to the upstream |
Hi @jrista I'm a little bit confused, could you please clarify what you mean ? And if I not mistaken, the |
Well, I may have to try this again, but in the recent past with a project that has wrapped up, the following kind of use case did not seem to be well supported: /users/comments?page&size&startDate&endDate: GET; list all comments within date range (optional paging) If you normally have |
@jrista, did you tried to do so ? const routes: Routes = [
{
path: '/users',
module: UsersModule,
children: [
{
path: 'posts',
module: PostsModule,
children: [{ path: ':postId/comments', module: CommentsModule }],
},
{ path: ':userId/comments', module: CommentsModule },
],
},
] and can you please take a look at this example |
This may be the ticket:
To map the same module twice...once with the parent id and once without. |
https://stackoverflow.com/questions/57346320/how-split-routes-by-controllers-in-nestjs-router |
I'm having a bit of trouble keeping my route definitions modular.
In this repro repo I have the
AppModule
that says hello when/prefix/hello
is called. But I intended theSubModule
to respond when/prefix/sub/hello
is called. But if I just use@Controller('sub')
in theSubController
, then it responds only to/sub/hello
.Now, I have read a bit of the original discussion with kamilmysliwiec and understand that modules are not meant to map directly to the paths. But right now it feels like needing to declare all routes in a single module hurts modularity.
Is there a way to prefix all controllers of a module as well as their imported controllers?
The text was updated successfully, but these errors were encountered: