UNPKG

2.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.exploreApiResponseMetadata = exports.exploreGlobalApiResponseMetadata = void 0;
4const common_1 = require("@nestjs/common");
5const constants_1 = require("@nestjs/common/constants");
6const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
7const lodash_1 = require("lodash");
8const constants_2 = require("../constants");
9const response_object_factory_1 = require("../services/response-object-factory");
10const merge_and_uniq_util_1 = require("../utils/merge-and-uniq.util");
11const responseObjectFactory = new response_object_factory_1.ResponseObjectFactory();
12const exploreGlobalApiResponseMetadata = (schemas, metatype) => {
13 const responses = Reflect.getMetadata(constants_2.DECORATORS.API_RESPONSE, metatype);
14 const produces = Reflect.getMetadata(constants_2.DECORATORS.API_PRODUCES, metatype);
15 return responses
16 ? {
17 responses: mapResponsesToSwaggerResponses(responses, schemas, produces)
18 }
19 : undefined;
20};
21exports.exploreGlobalApiResponseMetadata = exploreGlobalApiResponseMetadata;
22const exploreApiResponseMetadata = (schemas, instance, prototype, method) => {
23 const responses = Reflect.getMetadata(constants_2.DECORATORS.API_RESPONSE, method);
24 if (responses) {
25 const classProduces = Reflect.getMetadata(constants_2.DECORATORS.API_PRODUCES, prototype);
26 const methodProduces = Reflect.getMetadata(constants_2.DECORATORS.API_PRODUCES, method);
27 const produces = merge_and_uniq_util_1.mergeAndUniq(lodash_1.get(classProduces, 'produces'), methodProduces);
28 return mapResponsesToSwaggerResponses(responses, schemas, produces);
29 }
30 const status = getStatusCode(method);
31 if (status) {
32 return { [status]: { description: '' } };
33 }
34 return undefined;
35};
36exports.exploreApiResponseMetadata = exploreApiResponseMetadata;
37const getStatusCode = (method) => {
38 const status = Reflect.getMetadata(constants_1.HTTP_CODE_METADATA, method);
39 if (status) {
40 return status;
41 }
42 const requestMethod = Reflect.getMetadata(constants_1.METHOD_METADATA, method);
43 switch (requestMethod) {
44 case common_1.RequestMethod.POST:
45 return common_1.HttpStatus.CREATED;
46 default:
47 return common_1.HttpStatus.OK;
48 }
49};
50const omitParamType = (param) => lodash_1.omit(param, 'type');
51const mapResponsesToSwaggerResponses = (responses, schemas, produces = ['application/json']) => {
52 produces = shared_utils_1.isEmpty(produces) ? ['application/json'] : produces;
53 const openApiResponses = lodash_1.mapValues(responses, (response) => responseObjectFactory.create(response, produces, schemas));
54 return lodash_1.mapValues(openApiResponses, omitParamType);
55};