Nest: Support generation of URLs for routes including URL params

Created on 1 Sep 2018  路  15Comments  路  Source: nestjs/nest

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.

Current behavior

I could not find a way to generate URLs to my configured routes. This is very useful in order to prevent hardcoding URLs all over templates and view code.

Expected behavior

There should be a function that receives a route alias and an optional list of params and returns the route URL.

core type

Most helpful comment

When we are developing web applications it happens frequently that a view has to return a redirect to another view, or we have to include a link in templates to another view. It would be unfortunate if we had to hardcode these URLs all over the place. We can make typos and if in the future we want to change the route url we would need to propagate these changes. Moreover, this method can take params when the route takes URL params and this provides a check for incorrect number of params passed.

All 15 comments

Could you share more details about your idea?

When we are developing web applications it happens frequently that a view has to return a redirect to another view, or we have to include a link in templates to another view. It would be unfortunate if we had to hardcode these URLs all over the place. We can make typos and if in the future we want to change the route url we would need to propagate these changes. Moreover, this method can take params when the route takes URL params and this provides a check for incorrect number of params passed.

I like the idea. ASP.net MVC has something called UrlHelper, perhaps you can look its documentation to realize how to implement it

It exists in Django, Phoenix and Rails, for example. It is very useful.

This is a PR against express鈥檚 router that aims to add a similar functionality https://github.com/pillarjs/router/pull/36. I鈥檓 on holidays right now but when I鈥檓 back to work I plan to help merge this PR.

I like the idea, Should i implement that in Nest-Router ?

@edevil not sure if we really have to care about express in this case. Nest has its own mechanism which responsibility is to reflect all routes. I think we could reuse it effortlessly :)

@kamilmysliwiec Ah, great. Since Nest makes use of express I thought it would be useful. So, as I understand it, Nest has its own router, Nest-Router, and it is there that this functionality should be implemented instead of express鈥檚 router?

No no. Nest makes use of the express router, but we don't really have to affect express ecosystem at all. It should be pretty easy to build auto-generation of all registered paths using RoutesResolver class.

How would they be specified though? I'm used to something like this:

@Controller('cats', 'not_cats')
class CatsController {
  @Get('', 'list') // probably not '', but something else
  getAll() {}
  @Get(':id')
  show() {}

}

and then being able to do:

urlGenerator.path('not_cats_list', {}, isAbsoluteUrl) (where isAbsoluteUrl = true)
which would generate this:
http://example.org/cats
and
urlGenerator.path('not_cats_show', {id: id}) where (id = 1)
which would generate:
/cats/1

Maybe create a @Named decorator?

Example:

@Controller('cats')
class CatsController {
    @Get()
    @Named('cats')
    getAll() {}
}

or you could pass in the controller and its method name instead.

Example:

@Controller('cats')
class CatsController {
    @Get()
    getAll() {}
    @Get(':id')
    show() {}
}

then something along the lines:

// For params only
// produces: <prefix?>/cats/1
urlGenerator.path(CatsController, 'show', 1, ...);

// For query and params using object
// produces: <prefix?>/cats/1?project=Nest
urlGenerator.path(CatsController, 'show', {
    param: { 
        id: 1,
    },
    query: {
        project: 'Nest',
    }
});

or both and use the reflect value of @Named decorator to do the above with

urlGenerator.namedPath('cats', ...)

for more flexibility

ah thanks for bringing up the query bit. I think query params should work automatically. everything not included in the route definition is automatically a query param.

I'd also rather pass a string for templates.

So before anyone begins working on this, we should determine exactly the API wanted.

I think this issue could be fixed with #1438 ?

It鈥檚 been awhile, hopefully you guys find it useful.

https://github.com/vh13294/nestjs-url-generator

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

VRspace4 picture VRspace4  路  3Comments

rlesniak picture rlesniak  路  3Comments

hackboy picture hackboy  路  3Comments

rafal-rudnicki picture rafal-rudnicki  路  3Comments