-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
### TBD | ||
Motivation | ||
=== | ||
|
||
Tidying up `@Controller` classes and `@Get` (etc.) handlers from excessive number of `@nestjs/swagger` | ||
api decorators. All api decorators exposed from `@nestjs.swagger` can be applied from singel decorator. | ||
|
||
|
||
## Requirements | ||
|
||
* `@nestjs/common@6.9` or newer - needs to have `applyDecorators` utility method | ||
* `@nestjs/swagger` any version | ||
|
||
## Usage | ||
|
||
### 1. Install dependency | ||
|
||
``` | ||
$ npm install ssukienn/nestjs-swagger-api-spec | ||
``` | ||
|
||
### 2. Use `@ApiSpecification` decorator | ||
|
||
Decorate controller or handler wih `@ApiSpecification`. It can apply all `@Api` decorators passed in. | ||
The order of appliance depends on the JS iteration order of keys and the order in which Nest applies decorators. | ||
|
||
|
||
```typescript | ||
import { ApiSpecification, ApiOptions } from 'nestjs-swagger-api-spec'; | ||
|
||
const exampleSpec: ApiOptions = { | ||
apiResponseOptions: apiDecorator => [apiDecorator({status: 201, type: Number}), apiDecorator(...), ...], | ||
//.. | ||
} | ||
|
||
@ApiSpecification(exampleSpec) | ||
@Controller() | ||
class Example { | ||
|
||
@Get() | ||
@ApiSpecification({ | ||
apiResponseOptions: apiDecorator => [apiDecorator({status: 200, type: Number}), apiDecorator(...), ...] | ||
//... | ||
}) | ||
getExample() { | ||
//... | ||
} | ||
} | ||
```` | ||
|
||
## Caveats | ||
|
||
The implementation depends on: | ||
- the decorator factories being named with `Api` prefix. So any decorator diverging from this convention will not be applied. | ||
- breaking the contract of `applyDecorators` in future Nest versions. | ||
|
||
There were no changes that would break the contract in `@nestjs/common` since `6.9.0` and each decorator in `@nestjs/swagger` has `Api` prefix | ||
so it should be safe to use. | ||
|
||
## Getting Support & Contributing | ||
|
||
- Open issue or pull request. | ||
|
||
## License | ||
|
||
- [MIT licensed](LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.