Skip to content

Commit 7d8822c

Browse files
Merge branch 'fix-global-prefix-middleware' of https://github.com/CodyTseng/nest into CodyTseng-fix-global-prefix-middleware
2 parents 9dda2cd + 8769a9d commit 7d8822c

File tree

6 files changed

+25
-11
lines changed

6 files changed

+25
-11
lines changed

integration/nest-application/global-prefix/e2e/global-prefix.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ describe('Global prefix', () => {
2828
await request(server).get('/health').expect(404);
2929

3030
await request(server).get('/api/v1/health').expect(200);
31+
32+
await request(server)
33+
.get('/api/v1')
34+
.expect(200, 'Root: Data attached in middleware');
3135
});
3236

3337
it(`should exclude the path as string`, async () => {

integration/nest-application/global-prefix/src/app.controller.ts

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { Controller, Get, Post, Req } from '@nestjs/common';
22

33
@Controller()
44
export class AppController {
5+
@Get()
6+
root(@Req() req): string {
7+
return 'Root: ' + req.extras?.data;
8+
}
9+
510
@Get('hello/:name')
611
getHello(@Req() req): string {
712
return 'Hello: ' + req.extras?.data;

packages/core/middleware/route-info-path-extractor.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ export class RouteInfoPathExtractor {
3535
if (this.isAWildcard(path)) {
3636
const entries =
3737
versionPaths.length > 0
38-
? versionPaths.map(
39-
versionPath =>
38+
? versionPaths
39+
.map(versionPath => [
40+
this.prefixPath + versionPath,
4041
this.prefixPath + versionPath + addLeadingSlash(path),
41-
)
42-
: [this.prefixPath + addLeadingSlash(path)];
42+
])
43+
.flat()
44+
: this.prefixPath
45+
? [this.prefixPath, this.prefixPath + addLeadingSlash(path)]
46+
: [addLeadingSlash(path)];
4347

4448
return Array.isArray(this.excludedGlobalPrefixRoutes)
4549
? [

packages/core/middleware/routes-mapper.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Module } from '../injector/module';
2020
import { MetadataScanner } from '../metadata-scanner';
2121
import { PathsExplorer, RouteDefinition } from '../router/paths-explorer';
2222
import { targetModulesByContainer } from '../router/router-module';
23+
import { RequestMethod } from '@nestjs/common';
2324

2425
export class RoutesMapper {
2526
private readonly pathsExplorer: PathsExplorer;
@@ -46,7 +47,7 @@ export class RoutesMapper {
4647
}
4748

4849
private getRouteInfoFromPath(routePath: string): RouteInfo[] {
49-
const defaultRequestMethod = -1;
50+
const defaultRequestMethod = RequestMethod.ALL;
5051
return [
5152
{
5253
path: addLeadingSlash(routePath),

packages/core/test/middleware/builder.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ describe('MiddlewareBuilder', () => {
193193
expect(proxy.getExcludedRoutes()).to.be.eql([
194194
{
195195
path,
196-
method: -1 as any,
196+
method: RequestMethod.ALL,
197197
},
198198
]);
199199
});

packages/core/test/middleware/route-info-path-extractor.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('RouteInfoPathExtractor', () => {
3131
method: RequestMethod.ALL,
3232
version: '1',
3333
}),
34-
).to.eql(['/v1/*']);
34+
).to.eql(['/v1', '/v1/*']);
3535
});
3636

3737
it(`should return correct paths when set global prefix`, () => {
@@ -42,15 +42,15 @@ describe('RouteInfoPathExtractor', () => {
4242
path: '*',
4343
method: RequestMethod.ALL,
4444
}),
45-
).to.eql(['/api/*']);
45+
).to.eql(['/api', '/api/*']);
4646

4747
expect(
4848
routeInfoPathExtractor.extractPathsFrom({
4949
path: '*',
5050
method: RequestMethod.ALL,
5151
version: '1',
5252
}),
53-
).to.eql(['/api/v1/*']);
53+
).to.eql(['/api/v1', '/api/v1/*']);
5454
});
5555

5656
it(`should return correct paths when set global prefix and global prefix options`, () => {
@@ -66,15 +66,15 @@ describe('RouteInfoPathExtractor', () => {
6666
path: '*',
6767
method: RequestMethod.ALL,
6868
}),
69-
).to.eql(['/api/*', '/foo']);
69+
).to.eql(['/api', '/api/*', '/foo']);
7070

7171
expect(
7272
routeInfoPathExtractor.extractPathsFrom({
7373
path: '*',
7474
method: RequestMethod.ALL,
7575
version: '1',
7676
}),
77-
).to.eql(['/api/v1/*', '/v1/foo']);
77+
).to.eql(['/api/v1', '/api/v1/*', '/v1/foo']);
7878

7979
expect(
8080
routeInfoPathExtractor.extractPathsFrom({

0 commit comments

Comments
 (0)