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

Support generation of URLs for routes including URL params #1030

Closed
edevil opened this issue Sep 1, 2018 · 16 comments
Closed

Support generation of URLs for routes including URL params #1030

edevil opened this issue Sep 1, 2018 · 16 comments

Comments

@edevil
Copy link

edevil commented Sep 1, 2018

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.

@kamilmysliwiec
Copy link
Member

Could you share more details about your idea?

@edevil
Copy link
Author

edevil commented Sep 2, 2018

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.

@jselesan
Copy link

jselesan commented Sep 2, 2018

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

@edevil
Copy link
Author

edevil commented Sep 2, 2018

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

@edevil
Copy link
Author

edevil commented Sep 2, 2018

This is a PR against express’s router that aims to add a similar functionality pillarjs/router#36. I’m on holidays right now but when I’m back to work I plan to help merge this PR.

@shekohex
Copy link
Contributor

shekohex commented Sep 3, 2018

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

@kamilmysliwiec
Copy link
Member

@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 :)

@edevil
Copy link
Author

edevil commented Sep 3, 2018

@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’s router?

@kamilmysliwiec
Copy link
Member

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.

@ghost
Copy link

ghost commented Oct 9, 2018

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

@marcus-sa
Copy link

marcus-sa commented Oct 9, 2018

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

@ghost
Copy link

ghost commented Oct 9, 2018

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.

@marcus-sa
Copy link

marcus-sa commented Oct 10, 2018

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

@BrunnerLivio
Copy link
Member

I think this issue could be fixed with #1438 ?

@vh13294
Copy link

vh13294 commented Oct 28, 2020

It’s been awhile, hopefully you guys find it useful.

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

@kamilmysliwiec
Copy link
Member

#5117

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants