mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
fix(fastify-adapter): global prefix exclusion path handling w/middleware
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
Module,
|
||||
NestMiddleware,
|
||||
NestModule,
|
||||
Param,
|
||||
Query,
|
||||
Req,
|
||||
RequestMethod,
|
||||
@@ -439,6 +440,11 @@ describe('Middleware (FastifyAdapter)', () => {
|
||||
async rootPath(@Req() req: FastifyRequest['raw']) {
|
||||
return { success: true, root: true };
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async record(@Req() req: FastifyRequest['raw'], @Param('id') id: string) {
|
||||
return { success: true, record: id };
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
@@ -535,6 +541,32 @@ describe('Middleware (FastifyAdapter)', () => {
|
||||
.expect(200, { success: true, root: true });
|
||||
});
|
||||
|
||||
it(`GET forRoutes('{*path}') with global prefix and exclude pattern with wildcard`, async () => {
|
||||
app.setGlobalPrefix('/api', { exclude: ['/{*path}'] });
|
||||
await app.init();
|
||||
await app.getHttpAdapter().getInstance().ready();
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get('/pong')
|
||||
.expect(200, { success: true, pong: 'pong' });
|
||||
await request(app.getHttpServer())
|
||||
.get('/api/abc123')
|
||||
.expect(200, { success: true, pong: 'abc123' });
|
||||
});
|
||||
|
||||
it(`GET forRoutes('{*path}') with global prefix and exclude pattern with parameter`, async () => {
|
||||
app.setGlobalPrefix('/api', { exclude: ['/:id'] });
|
||||
await app.init();
|
||||
await app.getHttpAdapter().getInstance().ready();
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.get('/abc123')
|
||||
.expect(200, { success: true, record: 'abc123' });
|
||||
await request(app.getHttpServer())
|
||||
.get('/api/pong')
|
||||
.expect(200, { success: true, pong: 'pong' });
|
||||
});
|
||||
|
||||
it(`GET forRoutes('{*path}') with global prefix and global prefix options`, async () => {
|
||||
app.setGlobalPrefix('/api', { exclude: ['/'] });
|
||||
await app.init();
|
||||
|
||||
@@ -627,10 +627,12 @@ export class FastifyAdapter<
|
||||
normalizedPath = normalizedPath === '/*path' ? '*path' : normalizedPath;
|
||||
|
||||
// Normalize the path to support the prefix if it set in application
|
||||
normalizedPath =
|
||||
this._pathPrefix && !normalizedPath.startsWith(this._pathPrefix)
|
||||
? `${this._pathPrefix}${normalizedPath}*path`
|
||||
: normalizedPath;
|
||||
if (this._pathPrefix && !normalizedPath.startsWith(this._pathPrefix)) {
|
||||
normalizedPath = `${this._pathPrefix}${normalizedPath}`;
|
||||
if (normalizedPath.endsWith('/')) {
|
||||
normalizedPath = `${normalizedPath}{*path}`;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
let { regexp: re } = pathToRegexp(normalizedPath);
|
||||
|
||||
Reference in New Issue
Block a user