Nest: Named routes

Created on 3 Jan 2020  路  9Comments  路  Source: nestjs/nest

Feature Request

Is your feature request related to a problem? Please describe.


So, the problem it self comes when you're build a view, paths can change,you can switch global prefix, or whatever but router name, will be static, stick to the dynamic path.

Describe the solution you'd like


So I would like to have a possibility to set a name to the route that I can use it in view.

Teachability, Documentation, Adoption, Migration Strategy

I general, there are 2 packages, that some1 might think they cover the problem, first one is: https://www.npmjs.com/package/named-routes which do what it should do, but only on the Router scope, which is useless in our case, because each of the controller have his own isolated router, so you don't know the full path, second one is https://www.npmjs.com/package/express-reverse which seems to do the full funny staff for us, but idk if if it's not working the same way as the previous one.

So right now that we have:

@Controller('admin')
export class Admin {
  @Get('/')
  public indexAction() {
    // ... logic
  }
}

I would like to propose to add a second optional parameter to METHOD decorators

that might looks like:

@Controller('admin')
export class Admin {
  @Get('/', 'admin_index') // where string will be a default name for route
  // or
  @Get('/', {name: 'admin_index'})
  public indexAction() {
    // ... logic
  }
}

and then in view, you will be able to use kind of function to get back the full url, fe:

<a href="{{ url('admin_index') }}">Admin Panel</a>

Where url function|helper might live in express.local if we're talking about express or in different location, regarding to adapter.

What is the motivation / use case for changing the behavior?

Problem here lives between a developer and web designer co we can call it PEKBAC in general, but I would like to solve it in different approach, kind of contracts approach, like when a developer who wrote a whole business logic make a deal with a web developer who build a views, that routes will be named in some way, web developer doesn't have to think about prefiex, or care about developer changes in paths. Even if you have in plenty places link to specific view, and you change its path, but you're using name path, until it won't change, you're safe.

Regards.

UPDATE

I have update a decorator proposition to handle it as well as with literal object notation, because maybe in future some1 might get other proposition, so this will allow to extend this variation.

common core type

Most helpful comment

This enhancement is a great idea . I suggest to add a new decorator like WithName in order to avoid breaking changes.
carbon

All 9 comments

Interesting feature! I'm taking a look at the source and i have already done the decorator part, seems really good!

This quite the same as in Django: https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#url It also allows injecting parameters to the URL. Would be very helpful in MVC projects with templates to be rendered.

A little surprised this feature doesn't exist, since it's fairly standard with other frameworks.

Sails.js has getUrlFor.

This enhancement is a great idea. Looking forward to seeing it get merged. 馃憤

I like this idea. However, I don't think that we should use @Get() (or any other HTTP related decorator) for this. Instead, we should provide a separate decorator that allow specifying the name of a route. Would you like to create a PR for this @cojack?

This enhancement is a great idea . I suggest to add a new decorator like WithName in order to avoid breaking changes.
carbon

PRs are more than welcome :)

@kamilmysliwiec , I've raised PR #5117 to add a @WithAlias decorator in line with the discussion here.

Looking for some feedback on the implementation as well as the method name exposed to views (currently getUrl).

Follows @getspooky 's idea, but uses @WithAlias for the decorator, rather than @WithName, as a route _alias_ felt clearer in intention. But again, open to alternatives.

(It's my first contribution to nest, so fully expecting feedback to address ... great framework 馃挴 )

Here's my use case: I want to disallow all requests except for a few API endpoints. @sjones6

Was this page helpful?
0 / 5 - 0 ratings