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

Add a name to the routes #73

Open
amehat opened this issue Nov 20, 2019 · 3 comments
Open

Add a name to the routes #73

amehat opened this issue Nov 20, 2019 · 3 comments

Comments

@amehat
Copy link

amehat commented Nov 20, 2019

Hello,

it might be interesting to add names to each route, to use the name in the controllers and not a path.
So we could easily change a route without having to go into the controller.

In module:

const routes: Routes = [
    {
      path: '/api',
      name: 'api',
      children: [
        {
          path: '/cats',
          module: CatsModule,
          name: 'cats',
        },
        {
          path: '/cats/:id',
          module: CatsModule,
          name: 'catsById',
        },
        {
          path: '/dogs',
          module: DogsModule,
          name: 'dogs',
        },
      ],
    },
  ];

In controller:

// ...
@Controller()
export class CatsController {
  private cats: string[] = [];
  @Get('cats')
  sayHello() {
    return `Hello From CatsController`;
  }

  @Get('catsById')
  getCat(@Param('catId') id: string) {
    const idx = parseInt(id, 10) || 0;
    return { cat: this.cats[idx - 1] || null };
  }
@shekohex
Copy link
Member

Hi @amehat

I think it would be a little confusing for people to see that the path passed to the @Get() decorator isn't the real route of that method as a normal Nestjs controller would be?

@amehat
Copy link
Author

amehat commented Nov 22, 2019

The idea is to be able to work with naming and not just the path. This allows for greater maintainability.

A possibility for this is explicit and not confusing:

@Get(Routes['cats'])
// ...
@Get(Routes['catsById'])

@IzioDev
Copy link

IzioDev commented Mar 13, 2020

This allows for greater maintainability.

Is it ? really ?

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

3 participants