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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance forRoutes RouteInfo object to specify API versions #8844

Closed
1 task done
NitnekB opened this issue Dec 22, 2021 · 13 comments
Closed
1 task done

Enhance forRoutes RouteInfo object to specify API versions #8844

NitnekB opened this issue Dec 22, 2021 · 13 comments
Labels
needs triage This issue has not been looked into type: enhancement 馃惡

Comments

@NitnekB
Copy link

NitnekB commented Dec 22, 2021

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

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

I currently use NestJS versioning feature, which is nice BTW 馃殌

I also use a custom middleware the most common way:

consumer
      .apply(CustomMiddleware)
      .forRoutes(PricesController, ProductsController);

But this caused a regression and my middleware doesn't works anymore.

The only way to make it works is to specify each route, or exclude the one we doesn't want:

consumer
      .apply(CustomMiddleware)
      .exclude({ path: '/v0/products', method: RequestMethod.POST })
      .forRoutes({ path: '/*', method: RequestMethod.ALL });

This works well, but we have to fill in the version for each route: /v0/products, ...

Describe the solution you'd like

I dont' know if we should consider this as a bug, but I think we could use another parameter versions when declaring routes on forRoutes method enhancing RouteInfo type.

Teachability, documentation, adoption, migration strategy

Here an example of what I'm thinking about:

consumer
      .apply(CustomMiddleware)
      .exclude({ path: '/products', method: RequestMethod.POST, versions: ['0', '1'] })
      .forRoutes({ path: '/*', method: RequestMethod.ALL });

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

Prevent lots of works introducing or deprecating API versions

// products.controller.ts

export const PRODUCTS_API_VERSIONS = ['0', '1'];

@Controller({ path: 'products', version: PRODUCTS_API_VERSIONS })
export class ProductsController {
}
// app.module.ts

consumer
      .apply(CustomMiddleware)
      .exclude({ path: '/products', method: RequestMethod.POST, versions: PRODUCTS_API_VERSIONS })
      .forRoutes({ path: '/*', method: RequestMethod.ALL });
@NitnekB NitnekB added needs triage This issue has not been looked into type: enhancement 馃惡 labels Dec 22, 2021
@micalevisk
Copy link
Member

micalevisk commented Dec 26, 2021

I believe this feat will help on making HTTP versioning a first-class citizen on NestJS. That's cool.


@NitnekB As we can use multiple versions on version/defaultVersion field already, wouldn't be better to use the singular here as well?

.exclude({ ..., version: '1' })

.exclude({ ..., version: ['1'] })

@NitnekB
Copy link
Author

NitnekB commented Jan 20, 2022

@micalevisk Seems legit to me 馃憤

@Cyril-zip
Copy link

May I ask for any update on this issue?

@micalevisk
Copy link
Member

@chanpaul1234572 I didn't have time to try to implement this :/

PRs are more than welcomed

@princechauhan1992
Copy link
Contributor

@kamilmysliwiec I would like to implement this.

@micalevisk
Copy link
Member

@princechauhan1992 PRs are more than welcomed 馃樃

@ologbonowiwi
Copy link

Guys, I will get this one! 馃榿

@thiagomini
Copy link
Contributor

I'm taking this one, @kamilmysliwiec. I've been already able to reproduce the issue in an integration test.

@kamilmysliwiec
Copy link
Member

Sounds great @thiagomini!

@mareksuscak
Copy link

I couldn't find the original PR that introduced version-aware middleware but the implementation is currently buggy when global prefix is configured.

It produced:

/v1/api/users/:userId

instead of:

/api/v1/users/:userId

I.e. the version number is prepended before the global prefix, not the other way around. I'll create an issue.

@thiagomini
Copy link
Contributor

thiagomini commented Nov 15, 2022

Hey @mareksuscak, this is the PR that introduced that. Feel free to add the necessary fix to it! I can work on that as well as soon as I have the time. Thanks for lettings us know 馃槉

@thiagomini
Copy link
Contributor

thiagomini commented Nov 15, 2022

My approach to resolving this issue would be to reproduce the same problem in this test. As soon as you get the same error, work on your way down the layers to make it pass with the least amount of changes. Then, you can refactor it to a cleaner solution.

@mareksuscak
Copy link

Here's the ticket: #10566

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into type: enhancement 馃惡
Projects
None yet
Development

No branches or pull requests

8 participants