Skip to content

Commit

Permalink
Merge branch 'fix-global-prefix-middleware' of https://github.com/Cod…
Browse files Browse the repository at this point in the history
…yTseng/nest into CodyTseng-fix-global-prefix-middleware
  • Loading branch information
kamilmysliwiec committed Mar 18, 2024
2 parents 9dda2cd + 8769a9d commit 7d8822c
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ describe('Global prefix', () => {
await request(server).get('/health').expect(404);

await request(server).get('/api/v1/health').expect(200);

await request(server)
.get('/api/v1')
.expect(200, 'Root: Data attached in middleware');
});

it(`should exclude the path as string`, async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { Controller, Get, Post, Req } from '@nestjs/common';

@Controller()
export class AppController {
@Get()
root(@Req() req): string {
return 'Root: ' + req.extras?.data;
}

@Get('hello/:name')
getHello(@Req() req): string {
return 'Hello: ' + req.extras?.data;
Expand Down
12 changes: 8 additions & 4 deletions packages/core/middleware/route-info-path-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ export class RouteInfoPathExtractor {
if (this.isAWildcard(path)) {
const entries =
versionPaths.length > 0
? versionPaths.map(
versionPath =>
? versionPaths
.map(versionPath => [
this.prefixPath + versionPath,
this.prefixPath + versionPath + addLeadingSlash(path),
)
: [this.prefixPath + addLeadingSlash(path)];
])
.flat()
: this.prefixPath
? [this.prefixPath, this.prefixPath + addLeadingSlash(path)]
: [addLeadingSlash(path)];

return Array.isArray(this.excludedGlobalPrefixRoutes)
? [
Expand Down
3 changes: 2 additions & 1 deletion packages/core/middleware/routes-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Module } from '../injector/module';
import { MetadataScanner } from '../metadata-scanner';
import { PathsExplorer, RouteDefinition } from '../router/paths-explorer';
import { targetModulesByContainer } from '../router/router-module';
import { RequestMethod } from '@nestjs/common';

export class RoutesMapper {
private readonly pathsExplorer: PathsExplorer;
Expand All @@ -46,7 +47,7 @@ export class RoutesMapper {
}

private getRouteInfoFromPath(routePath: string): RouteInfo[] {
const defaultRequestMethod = -1;
const defaultRequestMethod = RequestMethod.ALL;
return [
{
path: addLeadingSlash(routePath),
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/middleware/builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ describe('MiddlewareBuilder', () => {
expect(proxy.getExcludedRoutes()).to.be.eql([
{
path,
method: -1 as any,
method: RequestMethod.ALL,
},
]);
});
Expand Down
10 changes: 5 additions & 5 deletions packages/core/test/middleware/route-info-path-extractor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('RouteInfoPathExtractor', () => {
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/v1/*']);
).to.eql(['/v1', '/v1/*']);
});

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

expect(
routeInfoPathExtractor.extractPathsFrom({
path: '*',
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/api/v1/*']);
).to.eql(['/api/v1', '/api/v1/*']);
});

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

expect(
routeInfoPathExtractor.extractPathsFrom({
path: '*',
method: RequestMethod.ALL,
version: '1',
}),
).to.eql(['/api/v1/*', '/v1/foo']);
).to.eql(['/api/v1', '/api/v1/*', '/v1/foo']);

expect(
routeInfoPathExtractor.extractPathsFrom({
Expand Down

0 comments on commit 7d8822c

Please sign in to comment.