mirror of
https://github.com/nestjs/nest.git
synced 2026-02-21 23:11:44 +00:00
feat(core): add version to middleware module
ensure middleware module considers path-versioning when applying middlewares
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import {
|
||||
Controller,
|
||||
Get,
|
||||
INestApplication,
|
||||
MiddlewareConsumer,
|
||||
Module,
|
||||
Version,
|
||||
RequestMethod,
|
||||
VersioningType,
|
||||
VERSION_NEUTRAL,
|
||||
} from '@nestjs/common';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import * as request from 'supertest';
|
||||
import { AppModule } from '../src/app.module';
|
||||
|
||||
const RETURN_VALUE = 'test';
|
||||
const VERSIONED_VALUE = 'test_versioned';
|
||||
|
||||
@Controller()
|
||||
class TestController {
|
||||
@Version('1')
|
||||
@Get('versioned')
|
||||
versionedTest() {
|
||||
return RETURN_VALUE;
|
||||
}
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [AppModule],
|
||||
controllers: [TestController],
|
||||
})
|
||||
class TestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer
|
||||
.apply((req, res, next) => res.send(VERSIONED_VALUE))
|
||||
.forRoutes({
|
||||
path: '/versioned',
|
||||
version: '1',
|
||||
method: RequestMethod.ALL,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
describe('Middleware', () => {
|
||||
let app: INestApplication;
|
||||
|
||||
beforeEach(async () => {
|
||||
app = (
|
||||
await Test.createTestingModule({
|
||||
imports: [TestModule],
|
||||
}).compile()
|
||||
).createNestApplication();
|
||||
|
||||
app.enableVersioning({
|
||||
type: VersioningType.URI,
|
||||
defaultVersion: VERSION_NEUTRAL,
|
||||
});
|
||||
await app.init();
|
||||
});
|
||||
|
||||
it(`forRoutes({ path: 'tests/versioned', version: '1', method: RequestMethod.ALL })`, () => {
|
||||
return request(app.getHttpServer())
|
||||
.get('/v1/versioned')
|
||||
.expect(200, VERSIONED_VALUE);
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
@@ -174,8 +174,7 @@ export class MiddlewareModule {
|
||||
await this.bindHandler(
|
||||
instanceWrapper,
|
||||
applicationRef,
|
||||
routeInfo.method,
|
||||
routeInfo.path,
|
||||
routeInfo,
|
||||
moduleRef,
|
||||
collection,
|
||||
);
|
||||
@@ -185,8 +184,7 @@ export class MiddlewareModule {
|
||||
private async bindHandler(
|
||||
wrapper: InstanceWrapper<NestMiddleware>,
|
||||
applicationRef: HttpServer,
|
||||
method: RequestMethod,
|
||||
path: string,
|
||||
routeInfo: RouteInfo,
|
||||
moduleRef: Module,
|
||||
collection: Map<InstanceToken, InstanceWrapper>,
|
||||
) {
|
||||
@@ -197,12 +195,11 @@ export class MiddlewareModule {
|
||||
const isStatic = wrapper.isDependencyTreeStatic();
|
||||
if (isStatic) {
|
||||
const proxy = await this.createProxy(instance);
|
||||
return this.registerHandler(applicationRef, method, path, proxy);
|
||||
return this.registerHandler(applicationRef, routeInfo, proxy);
|
||||
}
|
||||
await this.registerHandler(
|
||||
applicationRef,
|
||||
method,
|
||||
path,
|
||||
routeInfo,
|
||||
async <TRequest, TResponse>(
|
||||
req: TRequest,
|
||||
res: TResponse,
|
||||
@@ -266,8 +263,7 @@ export class MiddlewareModule {
|
||||
|
||||
private async registerHandler(
|
||||
applicationRef: HttpServer,
|
||||
method: RequestMethod,
|
||||
path: string,
|
||||
{ path, method, version }: RouteInfo,
|
||||
proxy: <TRequest, TResponse>(
|
||||
req: TRequest,
|
||||
res: TResponse,
|
||||
@@ -291,6 +287,11 @@ export class MiddlewareModule {
|
||||
}
|
||||
path = basePath + path;
|
||||
}
|
||||
|
||||
if (version) {
|
||||
path = `/v${version.toString()}${path}`;
|
||||
}
|
||||
|
||||
const isMethodAll = isRequestMethodAll(method);
|
||||
const requestMethod = RequestMethod[method];
|
||||
const router = await applicationRef.createMiddlewareFactory(method);
|
||||
|
||||
Reference in New Issue
Block a user