Skip to content

feat(core): exclude route from global prefix #5291

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

Merged

Conversation

Kiliandeca
Copy link
Contributor

@Kiliandeca Kiliandeca commented Aug 19, 2020

New global prefix option to exclude some routes as a string or a RouteInfo

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

You can specify a global route prefix but you can't exclude some routes

app.setGlobalPrefix('v1');

Issue Number: #963

What is the new behavior?

It add a new option object for the global prefix. You can exclude routes based on the path and the request method

app.setGlobalPrefix('v1', { exclude: [
  { path: '/health', method: RequestMethod.GET }
]});

You can also specify route as a string (it apply on every request method) or a mix of the 2 like so:

app.setGlobalPrefix('v1', { exclude: [
  'cats',
  { path: '/health', method: RequestMethod.GET }
]});

This is my first contribution to this awesome framework and there are probably things that need to be improved or moved so please give me some feedback.

I followed the syntax proposed in the issue (back in 2018) and I tried to follow the middleware route exclude code.

I would like to add support for wildcard parameters (like 'cats/(.*)') the same way it's done for the middleware but maybe it's better to do it step by step and create another PR ?

I can also write the doc for this if you want

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

Have a great day

New global prefix option to exclude some routes as a string or a RouteInfo
@Kiliandeca Kiliandeca force-pushed the feat/exclude-route-global-prefix branch from 26b41f6 to 47f05de Compare August 19, 2020 21:56
@coveralls
Copy link

coveralls commented Aug 19, 2020

Pull Request Test Coverage Report for Build 1a449be6-ea80-4e4c-87fd-c417f3816f9e

  • 16 of 26 (61.54%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 94.648%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/core/router/router-explorer.ts 12 22 54.55%
Totals Coverage Status
Change from base Build 0bd55d4e-9747-45b6-b626-b8e868f001b2: -0.2%
Covered Lines: 4934
Relevant Lines: 5213

💛 - Coveralls

app = module.createNestApplication();
});

it.only(`should use the global prefix`, async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are all of these it.only? Shouldn't they bet just it()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, I forgot to remove them after testing

import { AppModule } from '../src/app.module';
import * as request from 'supertest';
import { RequestMethod } from '@nestjs/common/enums/request-method.enum'
describe('Global prefix', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to add one more test showing that RequestMethod.GET works as described?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I just added the test you asked and found out it wasn't working correctly with RequestMethod.GET because it's the enum 0 so I changed a line in the code as well. Now everything should be working (I hope)

Copy link

@mseada94 mseada94 Aug 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I think some more test cases should be added.

  • Testing wildcard behaviour
  • Testing exclude:cats as string and request GET /cats/foo and GET /api/v1/cats/foo
  • Testing using a middleware to make sure its not being effected
    For now I am exploring the repo from mobile, so I decide to write a comment

@jmcdo29
Copy link
Member

jmcdo29 commented Aug 19, 2020

Looks like the failing test is due to a package installation problem. Other than that, to me, it's looking good. Let's wait to hear from Kamil too 😸

@in-live-md
Copy link

Is this PR going to be merged at some point?

@rubiin
Copy link
Contributor

rubiin commented Jan 19, 2021

this looks like a great feature to have

@sindilevich
Copy link

Thank you so much, it is one of long-awaited features of Nest.js.

What do you think about testing whether a nested path can be excluded? Like so:

app.setGlobalPrefix('v1', { exclude: [
  { path: '/health/heartbeat', method: RequestMethod.GET }
]});

@kamilmysliwiec kamilmysliwiec added this to the 8.0.0 milestone Feb 2, 2021
@kamilmysliwiec kamilmysliwiec changed the base branch from master to 8.0.0 February 2, 2021 09:37
@kamilmysliwiec kamilmysliwiec merged commit c5d47bb into nestjs:8.0.0 Feb 2, 2021
@kamilmysliwiec
Copy link
Member

LGTM, thanks! This will be added in 8.0.0

@ackvf

This comment has been minimized.

@nestjs nestjs locked and limited conversation to collaborators Oct 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants