nestjs-swagger-api-http-response-decorators
TypeScript icon, indicating that this package has built-in type declarations

3.2.0 • Public • Published

nestjs-swagger-api-http-response-decorators

npm version npm downloads supported nestjs versions

demo

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

Readme

Keywords

Package Sidebar

Install

npm i nestjs-swagger-api-http-response-decorators

Weekly Downloads

7

Version

3.2.0

License

ISC

Unpacked Size

14.6 kB

Total Files

4

Last publish

Collaborators

  • micalevisk