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

feat(core): middleware runs once for matching route #9809

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
103 changes: 103 additions & 0 deletions integration/hello-world/e2e/middleware-run-match-route.ts
@@ -0,0 +1,103 @@
import {
Controller,
Get,
INestApplication,
Injectable,
MiddlewareConsumer,
NestMiddleware,
Module,
} from '@nestjs/common';
import { Test } from '../../../packages/testing';
import * as request from 'supertest';
import { expect } from 'chai';

/**
* Number of times that the middleware was executed.
*/
let triggerCounter = 0;
@Injectable()
class Middleware implements NestMiddleware {
use(req, res, next) {
triggerCounter++;
next();
}
}

@Controller()
class TestController {
@Get('/test')
testA() {}

@Get('/:id')
testB() {}

@Get('/static/route')
testC() {}

@Get('/:id/:nested')
testD() {}
}

@Module({
controllers: [TestController],
})
class TestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(Middleware).forRoutes(TestController);
}
}

describe('Middleware (run on route match)', () => {
let app: INestApplication;

beforeEach(async () => {
triggerCounter = 0;
app = (
await Test.createTestingModule({
imports: [TestModule],
}).compile()
).createNestApplication();

await app.init();
});

it(`forRoutes(TestController) should execute middleware once when request url is equal match`, () => {
return request(app.getHttpServer())
.get('/test')
.expect(200)
.then(() => {
expect(triggerCounter).to.be.eq(1);
});
});

it(`forRoutes(TestController) should execute middleware once when request url is not equal match`, () => {
return request(app.getHttpServer())
.get('/1')
.expect(200)
.then(() => {
expect(triggerCounter).to.be.eq(1);
});
});

it(`forRoutes(TestController) should execute middleware once when request url is not of nested params`, () => {
return request(app.getHttpServer())
.get('/static/route')
.expect(200)
.then(() => {
expect(triggerCounter).to.be.eq(1);
});
});

it(`forRoutes(TestController) should execute middleware once when request url is of nested params`, () => {
return request(app.getHttpServer())
.get('/1/abc')
.expect(200)
.then(() => {
expect(triggerCounter).to.be.eq(1);
});
});

afterEach(async () => {
await app.close();
});
});
27 changes: 26 additions & 1 deletion packages/core/middleware/builder.ts
Expand Up @@ -59,7 +59,8 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
): MiddlewareConsumer {
const { middlewareCollection } = this.builder;

const forRoutes = this.getRoutesFlatList(routes);
const flattedRoutes = this.getRoutesFlatList(routes);
const forRoutes = this.removeOverlappedRoutes(flattedRoutes);
const configuration = {
middleware: filterMiddleware(
this.middleware,
Expand All @@ -82,5 +83,29 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
.flatten()
.toArray();
}

private removeOverlappedRoutes(routes: RouteInfo[]) {
const regexMatchParams = /(:[^\/]*)/g;
const wildcard = '([^/]*)';
const routesWithRegex = routes
.filter(route => route.path.includes(':'))
.map(route => ({
path: route.path,
regex: new RegExp(
'^(' + route.path.replace(regexMatchParams, wildcard) + ')$',
'g',
),
}));
return routes.filter(route => {
const isOverlapped = (v: { path: string; regex: RegExp }) => {
return route.path !== v.path && route.path.match(v.regex);
};
const routeMatch = routesWithRegex.find(isOverlapped);

if (routeMatch === undefined) {
return route;
}
});
}
};
}