Skip to content
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

fix(core): allow optional named wildcard groups #14522

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading