UNPKG

@globalart/nestjs-swagger

Version:

A simple documentation builder for NestJS Swagger Module

104 lines (103 loc) 3.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwaggerDocumentation = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const constants_1 = require("../constants"); const dtos_1 = require("../dtos"); const crypto_1 = require("crypto"); // SwaggerDocumentation is a decorator function to generate Swagger documentation for endpoints based on the provided options. const SwaggerDocumentation = (data) => { const operationId = data.operationId || (0, crypto_1.createHash)("md5").update(`${data.endpointSummary}`).digest("hex"); const decorators = [ (0, swagger_1.ApiOperation)({ operationId, description: data.endpointDescription, summary: data.endpointSummary, }), ]; if (data.error400Description) { decorators.push((0, swagger_1.ApiBadRequestResponse)({ description: data.error400Description, })); } if (data.error401Description) { decorators.push((0, swagger_1.ApiUnauthorizedResponse)({ description: data.error401Description, })); } if (data.error403Description) { decorators.push((0, swagger_1.ApiForbiddenResponse)({ description: data.error403Description, })); } if (data.error404Description) { decorators.push((0, swagger_1.ApiNotFoundResponse)({ description: data.error404Description, })); } if (data.error409Description) { decorators.push((0, swagger_1.ApiConflictResponse)({ description: data.error409Description, })); } if (data.error422Description) { decorators.push((0, swagger_1.ApiUnprocessableEntityResponse)({ description: data.error422Description, })); } if (data.error429Description) { decorators.push((0, swagger_1.ApiTooManyRequestsResponse)({ description: data.error429Description, })); } if (data.error500Description) { decorators.push((0, swagger_1.ApiInternalServerErrorResponse)({ description: data.error500Description, })); } if (data.error503Description) { decorators.push((0, swagger_1.ApiServiceUnavailableResponse)({ description: data.error503Description, })); } if (data.isPaginated && data.responseDto) { decorators.push((0, swagger_1.ApiOkResponse)({ schema: { allOf: [ { $ref: (0, swagger_1.getSchemaPath)(dtos_1.PaginatedResponseDto) }, { properties: { data: { type: "array", items: { $ref: (0, swagger_1.getSchemaPath)(data.responseDto), }, }, }, }, ], }, description: constants_1.API_RESPONSE_DESCRIPTION, }), (0, swagger_1.ApiExtraModels)(data.responseDto, dtos_1.PaginatedResponseDto)); } else if (data.responseDto) { decorators.push((0, swagger_1.ApiOkResponse)({ schema: data.isArray ? { type: "array", items: { $ref: (0, swagger_1.getSchemaPath)(data.responseDto) }, } : { $ref: (0, swagger_1.getSchemaPath)(data.responseDto) }, description: constants_1.API_RESPONSE_DESCRIPTION, }), (0, swagger_1.ApiExtraModels)(data.responseDto)); } else { decorators.push((0, swagger_1.ApiOkResponse)({ description: constants_1.API_RESPONSE_DESCRIPTION, })); } return (0, common_1.applyDecorators)(...decorators); }; exports.SwaggerDocumentation = SwaggerDocumentation;