1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.exploreApiResponseMetadata = exports.exploreGlobalApiResponseMetadata = void 0;
|
4 | const common_1 = require("@nestjs/common");
|
5 | const constants_1 = require("@nestjs/common/constants");
|
6 | const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
7 | const lodash_1 = require("lodash");
|
8 | const constants_2 = require("../constants");
|
9 | const response_object_factory_1 = require("../services/response-object-factory");
|
10 | const merge_and_uniq_util_1 = require("../utils/merge-and-uniq.util");
|
11 | const responseObjectFactory = new response_object_factory_1.ResponseObjectFactory();
|
12 | const 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 | };
|
21 | exports.exploreGlobalApiResponseMetadata = exploreGlobalApiResponseMetadata;
|
22 | const 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 | };
|
36 | exports.exploreApiResponseMetadata = exploreApiResponseMetadata;
|
37 | const 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 | };
|
50 | const omitParamType = (param) => lodash_1.omit(param, 'type');
|
51 | const 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 | };
|