@nestia/sdk
Version:
Nestia SDK and Swagger generator
115 lines • 5.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReflectHttpOperationResponseAnalyzer = void 0;
const constants_1 = require("@nestjs/common/constants");
const legacy_1 = require("../internal/legacy");
const HttpResponseContentTypeUtil_1 = require("../utils/HttpResponseContentTypeUtil");
const TextPlainValidator_1 = require("../validators/TextPlainValidator");
var ReflectHttpOperationResponseAnalyzer;
(function (ReflectHttpOperationResponseAnalyzer) {
ReflectHttpOperationResponseAnalyzer.analyze = (ctx) => {
var _a, _b, _c;
var _d, _e, _f, _g;
const errors = [];
const report = () => {
ctx.errors.push({
file: ctx.controller.file,
class: ctx.controller.class.name,
function: ctx.functionName,
from: "return",
contents: errors,
});
return null;
};
const encrypted = hasInterceptor({
name: "EncryptedRouteInterceptor",
function: ctx.function,
});
const contentType = encrypted
? "text/plain"
: hasInterceptor({
name: "TypedQueryRouteInterceptor",
function: ctx.function,
})
? "application/x-www-form-urlencoded"
: ((_e = (_d = (_b = (_a = Reflect.getMetadata(constants_1.HEADERS_METADATA, ctx.function)) === null || _a === void 0 ? void 0 : _a.find((h) => typeof (h === null || h === void 0 ? void 0 : h.name) === "string" &&
typeof (h === null || h === void 0 ? void 0 : h.value) === "string" &&
h.name.toLowerCase() === "content-type")) === null || _b === void 0 ? void 0 : _b.value) !== null && _d !== void 0 ? _d : (_c = Reflect.getMetadata("swagger/apiProduces", ctx.function)) === null || _c === void 0 ? void 0 : _c[0]) !== null && _e !== void 0 ? _e : (ctx.httpMethod === "HEAD" ? null : "application/json"));
const binary = HttpResponseContentTypeUtil_1.HttpResponseContentTypeUtil.isBinary(contentType);
const schema = binary
? { success: true, data: EMPTY_SCHEMA }
: contentType === "application/json"
? ctx.metadata.success.primitive
: ctx.metadata.success.resolved;
if (schema.success === false)
errors.push(...schema.errors);
if (ctx.httpMethod === "HEAD" && contentType !== null)
errors.push(`HEAD method must not have a content type.`);
if (HttpResponseContentTypeUtil_1.HttpResponseContentTypeUtil.isSupported(contentType) === false)
errors.push(`@nestia/sdk does not support ${JSON.stringify(contentType)} content type.`);
if (errors.length)
return report();
else if ((binary === false && ctx.metadata.success.type === null) ||
schema.success === false ||
!HttpResponseContentTypeUtil_1.HttpResponseContentTypeUtil.isSupported(contentType))
return null;
const example = Reflect.getMetadata("nestia/SwaggerExample/Response", ctx.function);
return Object.assign(Object.assign({ contentType,
binary,
encrypted, status: (_f = getStatus(ctx.function)) !== null && _f !== void 0 ? _f : (ctx.httpMethod === "POST" ? 201 : 200), type: (_g = ctx.metadata.success.type) !== null && _g !== void 0 ? _g : { name: "ReadableStream" } }, schema.data), { validate: binary === true
? () => []
: contentType === "application/json" || encrypted === true
? legacy_1.JsonMetadataFactory.validate
: contentType === "application/x-www-form-urlencoded"
? legacy_1.HttpQueryProgrammer.validate
: contentType === "text/plain"
? TextPlainValidator_1.TextPlainValidator.validate
: (next) => (0, legacy_1.sizeOf)(next.metadata) !== 0
? ["HEAD method must not have any return value."]
: [], example: example === null || example === void 0 ? void 0 : example.example, examples: example === null || example === void 0 ? void 0 : example.examples });
};
const getStatus = (func) => {
const text = Reflect.getMetadata(constants_1.HTTP_CODE_METADATA, func);
if (text === undefined)
return null;
const value = Number(text);
return isNaN(value) ? null : value;
};
const hasInterceptor = (props) => {
const meta = Reflect.getMetadata(constants_1.INTERCEPTORS_METADATA, props.function);
if (Array.isArray(meta) === false)
return false;
return meta.some((elem) => { var _a; return ((_a = elem === null || elem === void 0 ? void 0 : elem.constructor) === null || _a === void 0 ? void 0 : _a.name) === props.name; });
};
const EMPTY_SCHEMA = {
components: {
aliases: [],
arrays: [],
objects: [],
tuples: [],
},
metadata: {
aliases: [],
any: false,
arrays: [],
atomics: [],
constants: [],
escaped: null,
functions: [],
maps: [],
natives: [],
nullable: false,
objects: [],
optional: false,
required: true,
rest: null,
sets: [],
templates: [],
tuples: [],
size: 0,
name: "void",
empty: true,
},
};
})(ReflectHttpOperationResponseAnalyzer || (exports.ReflectHttpOperationResponseAnalyzer = ReflectHttpOperationResponseAnalyzer = {}));
//# sourceMappingURL=ReflectHttpOperationResponseAnalyzer.js.map