UNPKG

@solid-nestjs/rest-api

Version:

solid-nestjs Rest-API utilities

74 lines 3.45 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiResponses = void 0; const common_1 = require("@nestjs/common"); const swagger_1 = require("@nestjs/swagger"); const common_2 = require("@solid-nestjs/common"); class ErrorResponse { } __decorate([ (0, swagger_1.ApiProperty)(), __metadata("design:type", Number) ], ErrorResponse.prototype, "statusCode", void 0); __decorate([ (0, swagger_1.ApiProperty)(), __metadata("design:type", String) ], ErrorResponse.prototype, "message", void 0); __decorate([ (0, swagger_1.ApiProperty)(), __metadata("design:type", String) ], ErrorResponse.prototype, "error", void 0); /** * Composite decorator for defining multiple API response types for a controller method. * * @param options - Configuration options for the API responses. * @param options.type - The type of the successful response. Can be a class or a function. * @param options.schema - The OpenAPI schema object for the response. * @param options.isArray - Indicates if the response is an array of the specified type. * @param options.successCodes - Array of HTTP status codes to be treated as successful responses. Defaults to [HttpStatus.OK]. * @param options.errorCodes - Array of HTTP status codes to be treated as error responses. Defaults to [HttpStatus.BAD_REQUEST]. * * @remarks * This decorator applies multiple `@ApiResponse` decorators for both success and error HTTP status codes. * For error responses, it uses a standard `ErrorResponse` type and provides an example payload. * * @example * ```typescript * @ApiResponses({ * type: UserDto, * isArray: true, * successCodes: [HttpStatus.OK, HttpStatus.CREATED], * errorCodes: [HttpStatus.BAD_REQUEST, HttpStatus.UNAUTHORIZED] * }) * ``` */ const ApiResponses = ({ type, schema, isArray, successCodes, errorCodes, }) => (0, common_2.applyMethodDecorators)([ ...(successCodes ?? [common_1.HttpStatus.OK]).map(successCode => () => (0, swagger_1.ApiResponse)({ status: successCode, description: (0, common_2.getHttpStatusDescription)(successCode), type: type, schema: schema, isArray, })), ...(errorCodes ?? [common_1.HttpStatus.BAD_REQUEST]).map(errorCode => () => (0, swagger_1.ApiResponse)({ status: errorCode, description: (0, common_2.getHttpStatusDescription)(errorCode), type: ErrorResponse, example: { statusCode: errorCode, error: (0, common_2.getHttpStatusDescription)(errorCode), message: 'error message', }, })), ]); exports.ApiResponses = ApiResponses; //# sourceMappingURL=api-responses.decorator.js.map