Skip to content

Commit

Permalink
chore: resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 18, 2024
2 parents d5e2879 + 81404bb commit 6752144
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ describe('Global prefix', () => {
.expect(200, { '0': 'params', tenantId: 'test' });
});

it(`should execute middleware only once`, async () => {
app.setGlobalPrefix('/api', { exclude: ['/'] });

server = app.getHttpServer();
await app.init();

await request(server).get('/').expect(200, '1');
await request(server).get('/api/count').expect(200, '2');
});

afterEach(async () => {
await app.close();
});
Expand Down
10 changes: 10 additions & 0 deletions integration/nest-application/global-prefix/src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,14 @@ export class AppController {
postTest(): string {
return 'test';
}

@Get()
getHome(@Req() req) {
return req.count;
}

@Get('count')
getCount(@Req() req) {
return req.count;
}
}
9 changes: 8 additions & 1 deletion integration/nest-application/global-prefix/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const MIDDLEWARE_PARAM_VALUE = 'middleware_param';
controllers: [AppController],
})
export class AppModule {
private count = 0;
configure(consumer: MiddlewareConsumer) {
consumer
.apply((req, res, next) => res.end(MIDDLEWARE_VALUE))
Expand All @@ -27,6 +28,12 @@ export class AppModule {
req.middlewareParams = req.params;
next();
})
.forRoutes({ path: '*', method: RequestMethod.GET });
.forRoutes({ path: '*', method: RequestMethod.GET })
.apply((req, res, next) => {
this.count += 1;
req.count = this.count;
next();
})
.forRoutes('*');
}
}
6 changes: 5 additions & 1 deletion packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ export class MiddlewareModule<
}
return next();
};
paths.forEach(path => router(path, middlewareFunction));
const pathsToApplyMiddleware = [];
paths.some(path => path.match(/^\/?$/))
? pathsToApplyMiddleware.push('/')
: pathsToApplyMiddleware.push(...paths);
pathsToApplyMiddleware.forEach(path => router(path, middlewareFunction));
}

private getContextId(request: unknown, isTreeDurable: boolean): ContextId {
Expand Down

0 comments on commit 6752144

Please sign in to comment.