https://github.com/apexcaptain/nestjs-swagger-hide-on-prod
https://github.com/apexcaptain/nestjs-swagger-hide-on-prod
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/apexcaptain/nestjs-swagger-hide-on-prod
- Owner: ApexCaptain
- License: apache-2.0
- Created: 2022-08-24T04:42:26.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-09-27T04:22:05.000Z (over 3 years ago)
- Last Synced: 2025-04-08T18:47:13.423Z (about 1 year ago)
- Language: TypeScript
- Homepage:
- Size: 498 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A progressive Node.js framework for building efficient and scalable server-side applications.
# Nestjs Swagger Hide on Prod
## Description
Exclude your nestjs controller or endpoint from swagger documentization on production env.
## Installation
```bash
$ yarn add nestjs-swagger-hide-on-prod
```
or
```bash
$ npm i --save nestjs-swagger-hide-on-prod
```
## Getting Started
In default, when `NODE_ENV` is set to `PROD` or `PRODUCTION`, following decorators will exclude the whole controller or specific endpoint
- @SwaggerHideControllerOnProd
- @SwaggerHideEndpointOnProd
To exclude whole controller on prod :
```typescript
@Controller('foo')
@ApiTags('Foo API')
@SwaggerHideControllerOnProd() // Add this line to exclude FooController from swagger when process is on production environment
export class FooController {
constructor(private readonly fooService: FooService) {}
@Get(':fooId')
searchFoos(): Promise {
return Promise.resolve([]);
}
@Post()
createFoo(): Promise {
return Promise.resolve();
}
}
```
To Exclude specific endpoint only on prod :
```typescript
@Controller('foo')
@ApiTags('Foo API')
export class FooController {
constructor(private readonly fooService: FooService) {}
@Get(':fooId')
searchFoos(): Promise {
return Promise.resolve([]);
}
@Post()
@SwaggerHideEndpointOnProd() // Exclude POST /foo endpoint from swagger on prod env
createFoo(): Promise {
return Promise.resolve();
}
}
```
To customize exclusion condition you can use
- @SwaggerHideController
- @SwaggerHideEndpoint
instead.
Input parameter could be static `boolean` or a `function`, which returns a `boolean` value.
```typescript
@Controller('foo')
@ApiTags('Foo API')
export class FooController {
constructor(private readonly fooService: FooService) {}
@Get(':fooId')
@ApiOperation({
summary: 'Search foos info',
description: 'This method is depreacted. Please, use GET "v2" instaed',
})
@SwaggerHideEndpoint(() => {
return moment(new Date()).isAfter(`2022-12-31`);
// After year 2022, this method would be excluded from swagger documentization
})
veryOldFooSearchApi(): Promise {
return Promise.resolve([]);
}
@Get('v2/:fooId')
@ApiOperation({
summary: `Search foos info`,
description: `Get information matches id`,
})
brandNewFooSearchApi(): Promise {
return Promise.resolve([]);
}
}
```