nestjs-swagger-api-http-response-decorators
v3.2.0
Published
Small npm package that exports 'Api' decorators for all HTTP status codes available on @nestjs/common package, to use with @nestjs/swagger.
Downloads
5
Readme
nestjs-swagger-api-http-response-decorators
Installation
npm i @nestjs/swagger nestjs-swagger-api-http-response-decorators
# or
yarn add @nestjs/swagger nestjs-swagger-api-http-response-decorators
What
Taking the following TypeScript enumerate as an example:
enum HttpStatus {
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
PROCESSING = 102,
EARLYHINTS = 103
// ...
}
this small package exports @ApiXResponse(opts)
decorator factories (read more about them here) for each enum field listed above, including the ones already available on @nestjs/swagger
package.
The X
part will be the name of that enum field in pascal case.
eg: HTTP_VERSION_NOT_SUPPORTED
becomes HttpVersionNotSupported
In order to make this package as lean as possible, only decorators there are not available in @nestjs/swagger
are implemented.
The other ones are exported as-is via export * from '@nestjs/swagger'
.
Why
@nestjs/swagger
does not have @ApiX
decorators for all available HTTP status code out there (and this is why they export the @ApiResponse
), and "There are no plans to add more decorators.".
How index.ts
is being created?
I'm lazy, thus I made two simple scripts to generate the TypeScript code from missing @ApiX
decorators, relying on HttpStatus
enum exported from @nestjs/common
package.
Using the following tools:
wget
grep
sha1sum
- Node.js
This is how to generate the index.ts
(on root dir.) file:
$ npm ci
$ cd scripts
$ ./fetch-missing-decorators.sh
$ ./generate-index-ts-file.js
$ cd ../meta
$ sha1sum missing > missing.sha1
$ # ... later on, repeat the whole process and then
$ sha1sum -c missing.sha1
Development usage
The following doesn't cover properly how to develop the main script scripts/generate-index-ts-file.js
.
# To install all (dev)dependencies
npm ci
# To grab the latest version of the list of all missing enum fields from `@nestjs/swagger` and `@nestjs/common`
npm run download
# To generate the index.ts file
npm run create
# To compile the index.ts file everytime some change is made on it
npm run dev