Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/micalevisk/nestjs-swagger-api-http-response-decorators

Small Node.js package that exports 'Api' decorators for all HTTP status codes available on @nestjs/common package, to use with @nestjs/swagger (>= v4)
https://github.com/micalevisk/nestjs-swagger-api-http-response-decorators

hacktoberfest nest nestjs nestjs-library nodejs swagger

Last synced: about 1 month ago
JSON representation

Small Node.js package that exports 'Api' decorators for all HTTP status codes available on @nestjs/common package, to use with @nestjs/swagger (>= v4)

Awesome Lists containing this project

README

        

# `nestjs-swagger-api-http-response-decorators`

[![npm version](https://img.shields.io/npm/v/nestjs-swagger-api-http-response-decorators.svg)](https://www.npmjs.com/package/nestjs-swagger-api-http-response-decorators)
[![npm downloads](https://img.shields.io/npm/dt/nestjs-swagger-api-http-response-decorators.svg)](https://www.npmjs.com/package/nestjs-swagger-api-http-response-decorators)
[![supported nestjs versions](https://img.shields.io/badge/supported_versions-7|8|9|10-873131.svg?logo=nestjs)](https://nestjs.com)

![demo](https://user-images.githubusercontent.com/13461315/137850427-8c36ba4c-1067-4153-92dd-113c042ea21a.png)

## Installation

```bash
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:

```ts
enum HttpStatus {
CONTINUE = 100,
SWITCHING_PROTOCOLS = 101,
PROCESSING = 102,
EARLYHINTS = 103
// ...
}
```

this small package exports `@ApiXResponse(opts)` decorator factories (read more about them [here](https://docs.nestjs.com/openapi/decorators)) for each enum field listed above, **including** the ones already available on [`@nestjs/swagger`](https://github.com/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."](https://github.com/nestjs/swagger/issues/1501#issuecomment-945644971)**.

## 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`.

```bash
# 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
```