Skip to content

Commit

Permalink
fix(core): allow optional named wildcard groups #14520
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jan 27, 2025
1 parent 7c35ca3 commit 45fa078
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
36 changes: 30 additions & 6 deletions integration/hello-world/e2e/middleware-fastify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,14 @@ describe('Middleware (FastifyAdapter)', () => {
.apply((req, res, next) => {
req.extras = { data: 'Data attached in middleware' };
req.headers['ping'] = 'pong';

// When global prefix is set and the route is the root path
if (req.originalUrl === '/api') {
return res.end(JSON.stringify({ success: true, pong: 'pong' }));
}
next();
})
.forRoutes('*');
.forRoutes('{*path}');
}
}

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

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

it(`GET forRoutes('*') without prefix config`, async () => {
it(`GET forRoutes('{*path}') with global prefix (route: /api)`, async () => {
app.setGlobalPrefix('/api');
await app.init();
await app.getHttpAdapter().getInstance().ready();
return app
.inject({
method: 'GET',
url: '/api',
})
.then(({ payload }) =>
expect(payload).to.be.eql(
JSON.stringify({
success: true,
pong: 'pong',
}),
),
);
});

it(`GET forRoutes('{*path}') without prefix config`, async () => {
await app.init();
await app.getHttpAdapter().getInstance().ready();
return app
Expand All @@ -501,7 +525,7 @@ describe('Middleware (FastifyAdapter)', () => {
);
});

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

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

it(`GET forRoutes('*') with global prefix that not starts with /`, async () => {
it(`GET forRoutes('{*path}') with global prefix that not starts with /`, async () => {
app.setGlobalPrefix('api');
await app.init();
await app.getHttpAdapter().getInstance().ready();
Expand Down
10 changes: 7 additions & 3 deletions packages/core/middleware/route-info-path-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ export class RouteInfoPathExtractor {
}

private isAWildcard(path: string): boolean {
return ['*', '/*', '/*/', '*path', '/*path', '(.*)', '/(.*)'].includes(
path,
);
const isSimpleWildcard = ['*', '/*', '/*/', '(.*)', '/(.*)'];
if (isSimpleWildcard.includes(path)) {
return true;
}

const wildcardRegexp = /^\/\{.*\}.*|^\/\*.*$/;
return wildcardRegexp.test(path);
}

private extractNonWildcardPathsFrom({
Expand Down

0 comments on commit 45fa078

Please sign in to comment.