Skip to content

Commit 45fa078

Browse files
fix(core): allow optional named wildcard groups #14520
1 parent 7c35ca3 commit 45fa078

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

integration/hello-world/e2e/middleware-fastify.spec.ts

+30-6
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,14 @@ describe('Middleware (FastifyAdapter)', () => {
450450
.apply((req, res, next) => {
451451
req.extras = { data: 'Data attached in middleware' };
452452
req.headers['ping'] = 'pong';
453+
454+
// When global prefix is set and the route is the root path
455+
if (req.originalUrl === '/api') {
456+
return res.end(JSON.stringify({ success: true, pong: 'pong' }));
457+
}
453458
next();
454459
})
455-
.forRoutes('*');
460+
.forRoutes('{*path}');
456461
}
457462
}
458463

@@ -464,7 +469,7 @@ describe('Middleware (FastifyAdapter)', () => {
464469
).createNestApplication<NestFastifyApplication>(new FastifyAdapter());
465470
});
466471

467-
it(`GET forRoutes('*') with global prefix`, async () => {
472+
it(`GET forRoutes('{*path}') with global prefix (route: /api/pong)`, async () => {
468473
app.setGlobalPrefix('/api');
469474
await app.init();
470475
await app.getHttpAdapter().getInstance().ready();
@@ -483,7 +488,26 @@ describe('Middleware (FastifyAdapter)', () => {
483488
);
484489
});
485490

486-
it(`GET forRoutes('*') without prefix config`, async () => {
491+
it(`GET forRoutes('{*path}') with global prefix (route: /api)`, async () => {
492+
app.setGlobalPrefix('/api');
493+
await app.init();
494+
await app.getHttpAdapter().getInstance().ready();
495+
return app
496+
.inject({
497+
method: 'GET',
498+
url: '/api',
499+
})
500+
.then(({ payload }) =>
501+
expect(payload).to.be.eql(
502+
JSON.stringify({
503+
success: true,
504+
pong: 'pong',
505+
}),
506+
),
507+
);
508+
});
509+
510+
it(`GET forRoutes('{*path}') without prefix config`, async () => {
487511
await app.init();
488512
await app.getHttpAdapter().getInstance().ready();
489513
return app
@@ -501,7 +525,7 @@ describe('Middleware (FastifyAdapter)', () => {
501525
);
502526
});
503527

504-
it(`GET forRoutes('*') with global prefix and exclude patterns`, async () => {
528+
it(`GET forRoutes('{*path}') with global prefix and exclude patterns`, async () => {
505529
app.setGlobalPrefix('/api', { exclude: ['/'] });
506530
await app.init();
507531
await app.getHttpAdapter().getInstance().ready();
@@ -511,7 +535,7 @@ describe('Middleware (FastifyAdapter)', () => {
511535
.expect(200, { success: true, root: true });
512536
});
513537

514-
it(`GET forRoutes('*') with global prefix and global prefix options`, async () => {
538+
it(`GET forRoutes('{*path}') with global prefix and global prefix options`, async () => {
515539
app.setGlobalPrefix('/api', { exclude: ['/'] });
516540
await app.init();
517541
await app.getHttpAdapter().getInstance().ready();
@@ -528,7 +552,7 @@ describe('Middleware (FastifyAdapter)', () => {
528552
.expect(200, { success: true, root: true });
529553
});
530554

531-
it(`GET forRoutes('*') with global prefix that not starts with /`, async () => {
555+
it(`GET forRoutes('{*path}') with global prefix that not starts with /`, async () => {
532556
app.setGlobalPrefix('api');
533557
await app.init();
534558
await app.getHttpAdapter().getInstance().ready();

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,13 @@ export class RouteInfoPathExtractor {
7171
}
7272

7373
private isAWildcard(path: string): boolean {
74-
return ['*', '/*', '/*/', '*path', '/*path', '(.*)', '/(.*)'].includes(
75-
path,
76-
);
74+
const isSimpleWildcard = ['*', '/*', '/*/', '(.*)', '/(.*)'];
75+
if (isSimpleWildcard.includes(path)) {
76+
return true;
77+
}
78+
79+
const wildcardRegexp = /^\/\{.*\}.*|^\/\*.*$/;
80+
return wildcardRegexp.test(path);
7781
}
7882

7983
private extractNonWildcardPathsFrom({

0 commit comments

Comments
 (0)