Skip to content

Commit b6bc35e

Browse files
Revert "fix(core): middlewareConsumer forRoutes controller"
1 parent 9c7dfd9 commit b6bc35e

6 files changed

Lines changed: 20 additions & 220 deletions

File tree

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

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,13 @@ import {
55
Injectable,
66
MiddlewareConsumer,
77
Module,
8-
Param,
98
RequestMethod,
109
} from '@nestjs/common';
1110
import { Test } from '@nestjs/testing';
1211
import * as request from 'supertest';
1312
import { ApplicationModule } from '../src/app.module';
1413
import { Response } from 'express';
1514

16-
const OPTIONAL_PARAM_VALUE = 'test_optional_param';
17-
const FOR_ROUTE_CONTROLLER_VALUE = 'test_for_route_controller';
18-
const FOR_ROUTE_PATH_VALUE = 'test_for_route_path';
1915
const INCLUDED_VALUE = 'test_included';
2016
const RETURN_VALUE = 'test';
2117
const WILDCARD_VALUE = 'test_wildcard';
@@ -35,44 +31,16 @@ class TestController {
3531
}
3632
}
3733

38-
@Controller(OPTIONAL_PARAM_VALUE)
39-
class TestParamController {
40-
@Get(':test')
41-
[OPTIONAL_PARAM_VALUE]() {
42-
return RETURN_VALUE;
43-
}
44-
}
45-
46-
@Controller()
47-
class ForRouteController {
48-
@Get('for_route_controller')
49-
forRouteController() {
50-
return RETURN_VALUE;
51-
}
52-
53-
@Get('for_route_controller/required_param/:param')
54-
requiredParam() {
55-
return RETURN_VALUE;
56-
}
57-
}
58-
5934
@Module({
6035
imports: [ApplicationModule],
61-
controllers: [TestController, TestParamController, ForRouteController],
36+
controllers: [TestController],
6237
})
6338
class TestModule {
6439
configure(consumer: MiddlewareConsumer) {
6540
consumer
6641
.apply((req, res: Response, next) => res.status(201).end(INCLUDED_VALUE))
6742
.forRoutes({ path: 'tests/included', method: RequestMethod.POST })
68-
.apply((req, res: Response, next) => res.end(FOR_ROUTE_PATH_VALUE))
69-
.forRoutes({ path: 'for_route_path', method: RequestMethod.GET })
70-
.apply((req, res: Response, next) => res.end(FOR_ROUTE_CONTROLLER_VALUE))
71-
.forRoutes(ForRouteController)
72-
.apply((req, res, next) => res.end(OPTIONAL_PARAM_VALUE))
73-
.forRoutes({ path: `${OPTIONAL_PARAM_VALUE}/(:test)?`, method: RequestMethod.GET })
7443
.apply(Middleware)
75-
.exclude({ path: `${OPTIONAL_PARAM_VALUE}/(.*)`, method: RequestMethod.ALL })
7644
.forRoutes('*');
7745
}
7846
}
@@ -114,54 +82,6 @@ describe('Middleware (class)', () => {
11482
.expect(201, INCLUDED_VALUE);
11583
});
11684

117-
it(`/for_route_path forRoutes(/for_route_path)`, () => {
118-
return request(app.getHttpServer())
119-
.get('/for_route_path')
120-
.expect(200, FOR_ROUTE_PATH_VALUE);
121-
});
122-
123-
it(`/for_route_path/test forRoutes(/for_route_path)`, () => {
124-
return request(app.getHttpServer())
125-
.get('/for_route_path/test')
126-
.expect(200, FOR_ROUTE_PATH_VALUE);
127-
});
128-
129-
it(`/for_route_controller forRoutes(ForRouteController)`, () => {
130-
return request(app.getHttpServer())
131-
.get('/for_route_controller')
132-
.expect(200, FOR_ROUTE_CONTROLLER_VALUE);
133-
});
134-
135-
it(`/for_route_controller/test forRoutes(ForRouteController)`, () => {
136-
return request(app.getHttpServer())
137-
.get('/for_route_controller/test')
138-
.expect(200, WILDCARD_VALUE);
139-
});
140-
141-
it(`/for_route_controller/required_param/ forRoutes(ForRouteController)`, () => {
142-
return request(app.getHttpServer())
143-
.get('/for_route_controller/required_param/')
144-
.expect(200, WILDCARD_VALUE);
145-
});
146-
147-
it(`/for_route_controller/required_param/test forRoutes(ForRouteController)`, () => {
148-
return request(app.getHttpServer())
149-
.get('/for_route_controller/required_param/test')
150-
.expect(200, FOR_ROUTE_CONTROLLER_VALUE);
151-
});
152-
153-
it(`/${OPTIONAL_PARAM_VALUE}/ forRoutes(${OPTIONAL_PARAM_VALUE}/(:test)?)`, () => {
154-
return request(app.getHttpServer())
155-
.get(`/${OPTIONAL_PARAM_VALUE}/`)
156-
.expect(200, OPTIONAL_PARAM_VALUE);
157-
});
158-
159-
it(`/${OPTIONAL_PARAM_VALUE}/test forRoutes(${OPTIONAL_PARAM_VALUE}/(:test)?)`, () => {
160-
return request(app.getHttpServer())
161-
.get(`/${OPTIONAL_PARAM_VALUE}/test`)
162-
.expect(200, OPTIONAL_PARAM_VALUE);
163-
});
164-
16585
afterEach(async () => {
16686
await app.close();
16787
});

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

Lines changed: 4 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
Get,
44
MiddlewareConsumer,
55
Module,
6-
Param,
76
Query,
87
RequestMethod,
98
} from '@nestjs/common';
@@ -15,16 +14,12 @@ import { Test } from '@nestjs/testing';
1514
import { expect } from 'chai';
1615
import { ApplicationModule } from '../src/app.module';
1716

18-
const OPTIONAL_PARAM_VALUE = 'test_optional_param';
19-
const FOR_ROUTE_CONTROLLER_VALUE = 'test_for_route_controller';
20-
const FOR_ROUTE_PATH_VALUE = 'test_for_route_path';
2117
const INCLUDED_VALUE = 'test_included';
2218
const QUERY_VALUE = 'test_query';
2319
const REQ_URL_VALUE = 'test_req_url';
2420
const RETURN_VALUE = 'test';
2521
const SCOPED_VALUE = 'test_scoped';
2622
const WILDCARD_VALUE = 'test_wildcard';
27-
const CATCH_ALL_VALUE = 'test_catch_all';
2823

2924
@Controller()
3025
class TestController {
@@ -62,35 +57,9 @@ class TestQueryController {
6257
}
6358
}
6459

65-
@Controller(OPTIONAL_PARAM_VALUE)
66-
class TestParamController {
67-
@Get(':test')
68-
[OPTIONAL_PARAM_VALUE]() {
69-
return RETURN_VALUE;
70-
}
71-
}
72-
73-
@Controller()
74-
class ForRouteController {
75-
@Get('for_route_controller')
76-
forRouteController() {
77-
return RETURN_VALUE;
78-
}
79-
80-
@Get('for_route_controller/required_param/:param')
81-
requiredParam() {
82-
return RETURN_VALUE;
83-
}
84-
}
85-
8660
@Module({
8761
imports: [ApplicationModule],
88-
controllers: [
89-
TestController,
90-
TestQueryController,
91-
TestParamController,
92-
ForRouteController,
93-
],
62+
controllers: [TestController, TestQueryController],
9463
})
9564
class TestModule {
9665
configure(consumer: MiddlewareConsumer) {
@@ -107,17 +76,8 @@ class TestModule {
10776
.forRoutes(TestQueryController)
10877
.apply((req, res, next) => res.end(SCOPED_VALUE))
10978
.forRoutes(TestController)
110-
.apply((req, res, next) => res.end(FOR_ROUTE_PATH_VALUE))
111-
.forRoutes({ path: 'for_route_path', method: RequestMethod.GET })
112-
.apply((req, res, next) => res.end(FOR_ROUTE_CONTROLLER_VALUE))
113-
.forRoutes(ForRouteController)
114-
.apply((req, res, next) => res.end(OPTIONAL_PARAM_VALUE))
115-
.forRoutes({ path: `${OPTIONAL_PARAM_VALUE}/:test?`, method: RequestMethod.GET })
116-
.apply((req, res, next) => res.end(CATCH_ALL_VALUE))
117-
.exclude(
118-
{ path: QUERY_VALUE, method: RequestMethod.ALL },
119-
{ path: `${OPTIONAL_PARAM_VALUE}/(.*)`, method: RequestMethod.ALL },
120-
)
79+
.apply((req, res, next) => res.end(RETURN_VALUE))
80+
.exclude({ path: QUERY_VALUE, method: -1 })
12181
.forRoutes('(.*)');
12282
}
12383
}
@@ -141,7 +101,7 @@ describe('Middleware (FastifyAdapter)', () => {
141101
method: 'GET',
142102
url: '/hello',
143103
})
144-
.then(({ payload }) => expect(payload).to.be.eql(CATCH_ALL_VALUE));
104+
.then(({ payload }) => expect(payload).to.be.eql(RETURN_VALUE));
145105
});
146106

147107
it(`forRoutes(TestController)`, () => {
@@ -225,82 +185,6 @@ describe('Middleware (FastifyAdapter)', () => {
225185
.then(({ payload }) => expect(payload).to.be.eql(INCLUDED_VALUE));
226186
});
227187

228-
it(`/for_route_path forRoutes(/for_route_path)`, () => {
229-
return app
230-
.inject({
231-
method: 'GET',
232-
url: '/for_route_path',
233-
})
234-
.then(({ payload }) => expect(payload).to.be.eql(FOR_ROUTE_PATH_VALUE));
235-
});
236-
237-
it(`/for_route_path/test forRoutes(/for_route_path)`, () => {
238-
return app
239-
.inject({
240-
method: 'GET',
241-
url: '/for_route_path/test',
242-
})
243-
.then(({ payload }) => expect(payload).to.be.eql(FOR_ROUTE_PATH_VALUE));
244-
});
245-
246-
it(`/for_route_controller forRoutes(ForRouteController)`, () => {
247-
return app
248-
.inject({
249-
method: 'GET',
250-
url: '/for_route_controller',
251-
})
252-
.then(({ payload }) =>
253-
expect(payload).to.be.eql(FOR_ROUTE_CONTROLLER_VALUE),
254-
);
255-
});
256-
257-
it(`/for_route_controller/test forRoutes(ForRouteController)`, () => {
258-
return app
259-
.inject({
260-
method: 'GET',
261-
url: '/for_route_controller/test',
262-
})
263-
.then(({ payload }) => expect(payload).to.be.eql(CATCH_ALL_VALUE));
264-
});
265-
266-
it(`/for_route_controller/required_param/ forRoutes(ForRouteController)`, () => {
267-
return app
268-
.inject({
269-
method: 'GET',
270-
url: '/for_route_controller/required_param/',
271-
})
272-
.then(({ payload }) => expect(payload).to.be.eql(CATCH_ALL_VALUE));
273-
});
274-
275-
it(`/for_route_controller/required_param/test forRoutes(ForRouteController)`, () => {
276-
return app
277-
.inject({
278-
method: 'GET',
279-
url: '/for_route_controller/required_param/test',
280-
})
281-
.then(({ payload }) =>
282-
expect(payload).to.be.eql(FOR_ROUTE_CONTROLLER_VALUE),
283-
);
284-
});
285-
286-
it(`/${OPTIONAL_PARAM_VALUE}/ forRoutes(${OPTIONAL_PARAM_VALUE}/:test?)`, () => {
287-
return app
288-
.inject({
289-
method: 'GET',
290-
url: `/${OPTIONAL_PARAM_VALUE}/`,
291-
})
292-
.then(({ payload }) => expect(payload).to.be.eql(OPTIONAL_PARAM_VALUE));
293-
});
294-
295-
it(`/${OPTIONAL_PARAM_VALUE}/test forRoutes(${OPTIONAL_PARAM_VALUE}/:test?)`, () => {
296-
return app
297-
.inject({
298-
method: 'GET',
299-
url: `/${OPTIONAL_PARAM_VALUE}/test`,
300-
})
301-
.then(({ payload }) => expect(payload).to.be.eql(OPTIONAL_PARAM_VALUE));
302-
});
303-
304188
afterEach(async () => {
305189
await app.close();
306190
});

packages/core/middleware/routes-mapper.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ export class RoutesMapper {
5656
.map(item =>
5757
item.path?.map(p => {
5858
let path = modulePath ?? '';
59-
path +=
60-
this.normalizeGlobalPath(routePath) + addLeadingSlash(p) + '$';
59+
path += this.normalizeGlobalPath(routePath) + addLeadingSlash(p);
6160

6261
return {
6362
path,

packages/core/test/middleware/builder.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('MiddlewareBuilder', () => {
4545
},
4646
{
4747
method: 0,
48-
path: '/path/route$',
48+
path: '/path/route',
4949
},
5050
],
5151
},

packages/core/test/middleware/routes-mapper.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ describe('RoutesMapper', () => {
3030
{ path: '/test', method: RequestMethod.GET },
3131
]);
3232
expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([
33-
{ path: '/test/test$', method: RequestMethod.GET },
34-
{ path: '/test/another$', method: RequestMethod.DELETE },
33+
{ path: '/test/test', method: RequestMethod.GET },
34+
{ path: '/test/another', method: RequestMethod.DELETE },
3535
]);
3636
});
3737
@Controller(['test', 'test2'])
@@ -56,10 +56,10 @@ describe('RoutesMapper', () => {
5656
{ path: '/test', method: RequestMethod.GET },
5757
]);
5858
expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([
59-
{ path: '/test/test$', method: RequestMethod.GET },
60-
{ path: '/test/another$', method: RequestMethod.DELETE },
61-
{ path: '/test2/test$', method: RequestMethod.GET },
62-
{ path: '/test2/another$', method: RequestMethod.DELETE },
59+
{ path: '/test/test', method: RequestMethod.GET },
60+
{ path: '/test/another', method: RequestMethod.DELETE },
61+
{ path: '/test2/test', method: RequestMethod.GET },
62+
{ path: '/test2/another', method: RequestMethod.DELETE },
6363
]);
6464
});
6565
});

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import {
3737
InjectOptions,
3838
Response as LightMyRequestResponse,
3939
} from 'light-my-request';
40-
import * as pathToRegexp from 'path-to-regexp';
4140
import {
4241
FastifyStaticOptions,
4342
PointOfViewOptions,
@@ -415,18 +414,16 @@ export class FastifyAdapter<
415414
await this.registerMiddie();
416415
}
417416
return (path: string, callback: Function) => {
418-
if (path.endsWith('/*')) {
419-
path = `${path.slice(0, -1)}(.*)`
420-
} else if (path.endsWith('$')) {
421-
path = pathToRegexp(path.slice(0, -1), [], {
422-
end: true,
423-
strict: false
424-
}) as any
425-
}
417+
const normalizedPath = path.endsWith('/*')
418+
? `${path.slice(0, -1)}(.*)`
419+
: path;
426420

427421
// The following type assertion is valid as we use import('middie') rather than require('middie')
428422
// ref https://github.com/fastify/middie/pull/55
429-
this.instance.use(path, callback as Parameters<TInstance['use']>['1']);
423+
this.instance.use(
424+
normalizedPath,
425+
callback as Parameters<TInstance['use']>['1'],
426+
);
430427
};
431428
}
432429

0 commit comments

Comments
 (0)