Skip to content

Commit

Permalink
Merge pull request #2523 from thiagomini/patch-6
Browse files Browse the repository at this point in the history
docs(middlewares): add versioning integration docs
  • Loading branch information
kamilmysliwiec committed Nov 8, 2022
2 parents 16d3201 + 9761c1f commit 4bdf82a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions content/techniques/versioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,30 @@ app.enableVersioning({
defaultVersion: VERSION_NEUTRAL
});
```

#### Middleware versioning

[Middlewares](https://docs.nestjs.com/middleware) can also use versioning metadata to configure the middleware for a specific route's version. To do so, provide the version number as one of the parameters for the `MiddlewareConsumer.forRoutes()` method:

```typescript
@@filename(app.module)
import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { LoggerMiddleware } from './common/middleware/logger.middleware';
import { CatsModule } from './cats/cats.module';
import { CatsController } from './cats/cats.controller';

@Module({
imports: [CatsModule],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(LoggerMiddleware)
.forRoutes({ path: 'cats', method: RequestMethod.GET, version: '2' );
}
}
```
With the code above, the `LoggerMiddleware` will only be applied to the version '2' of `/cats` endpoint.
> info **Notice** Middlewares work with any versioning type described in the this section: `URI`, `Header`, `Media Type` or `Custom`.

0 comments on commit 4bdf82a

Please sign in to comment.