Skip to content

Commit c91eeff

Browse files
committed
fix(fastify-adapter): global prefix exclusion path handling w/middleware
1 parent 05d8ac7 commit c91eeff

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Module,
66
NestMiddleware,
77
NestModule,
8+
Param,
89
Query,
910
Req,
1011
RequestMethod,
@@ -439,6 +440,11 @@ describe('Middleware (FastifyAdapter)', () => {
439440
async rootPath(@Req() req: FastifyRequest['raw']) {
440441
return { success: true, root: true };
441442
}
443+
444+
@Get(':id')
445+
async record(@Req() req: FastifyRequest['raw'], @Param('id') id: string) {
446+
return { success: true, record: id };
447+
}
442448
}
443449

444450
@Module({
@@ -535,6 +541,32 @@ describe('Middleware (FastifyAdapter)', () => {
535541
.expect(200, { success: true, root: true });
536542
});
537543

544+
it(`GET forRoutes('{*path}') with global prefix and exclude pattern with wildcard`, async () => {
545+
app.setGlobalPrefix('/api', { exclude: ['/{*path}'] });
546+
await app.init();
547+
await app.getHttpAdapter().getInstance().ready();
548+
549+
await request(app.getHttpServer())
550+
.get('/pong')
551+
.expect(200, { success: true, pong: 'pong' });
552+
await request(app.getHttpServer())
553+
.get('/api/abc123')
554+
.expect(200, { success: true, pong: 'abc123' });
555+
});
556+
557+
it(`GET forRoutes('{*path}') with global prefix and exclude pattern with parameter`, async () => {
558+
app.setGlobalPrefix('/api', { exclude: ['/:id'] });
559+
await app.init();
560+
await app.getHttpAdapter().getInstance().ready();
561+
562+
await request(app.getHttpServer())
563+
.get('/abc123')
564+
.expect(200, { success: true, record: 'abc123' });
565+
await request(app.getHttpServer())
566+
.get('/api/pong')
567+
.expect(200, { success: true, pong: 'pong' });
568+
});
569+
538570
it(`GET forRoutes('{*path}') with global prefix and global prefix options`, async () => {
539571
app.setGlobalPrefix('/api', { exclude: ['/'] });
540572
await app.init();

packages/platform-fastify/adapters/fastify-adapter.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,12 @@ export class FastifyAdapter<
627627
normalizedPath = normalizedPath === '/*path' ? '*path' : normalizedPath;
628628

629629
// Normalize the path to support the prefix if it set in application
630-
normalizedPath =
631-
this._pathPrefix && !normalizedPath.startsWith(this._pathPrefix)
632-
? `${this._pathPrefix}${normalizedPath}*path`
633-
: normalizedPath;
630+
if (this._pathPrefix && !normalizedPath.startsWith(this._pathPrefix)) {
631+
normalizedPath = `${this._pathPrefix}${normalizedPath}`;
632+
if (normalizedPath.endsWith('/')) {
633+
normalizedPath = `${normalizedPath}{*path}`;
634+
}
635+
}
634636

635637
try {
636638
let { regexp: re } = pathToRegexp(normalizedPath);

0 commit comments

Comments
 (0)