UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

69 lines 3.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReflectHttpOperationExceptionAnalyzer = void 0; const legacy_1 = require("../internal/legacy"); var ReflectHttpOperationExceptionAnalyzer; (function (ReflectHttpOperationExceptionAnalyzer) { ReflectHttpOperationExceptionAnalyzer.analyze = (ctx) => { // TypeScript applies decorators bottom-up, so Reflect's metadata array // is the reverse of declaration order. Reverse it once here so the // resulting status-code → exception map iterates in declaration order // and lines up with the Go transformer's metadata.exceptions[], which // is emitted in AST/declaration order (see // packages/core/native/cmd/ttsc-nestia/sdk_transform.go). const preconfigured = analyzePreconfigured(ctx.function) .slice() .reverse(); const errors = []; const exceptions = preconfigured .map((pre, i) => { var _a; const matched = ctx.metadata.exceptions[i]; if (matched === undefined) { errors.push({ file: ctx.controller.file, class: ctx.controller.class.name, function: ctx.functionName, from: `exception (status: ${pre.status})`, contents: ["Unable to find exception type."], }); return null; } else if (matched.type === null) { errors.push({ file: ctx.controller.file, class: ctx.controller.class.name, function: ctx.functionName, from: `exception (status: ${pre.status})`, contents: ["Failed to get the type info."], }); } const schema = matched.primitive .success ? matched.primitive.data : null; if (matched.primitive.success === false) { errors.push({ file: ctx.controller.file, class: ctx.controller.class.name, function: ctx.functionName, from: `exception (status: ${pre.status})`, contents: matched.primitive.errors.map((e) => ({ name: e.name, accessor: e.accessor, messages: e.messages, })), }); } if (schema === null || matched.type === null) return null; return Object.assign(Object.assign({ status: pre.status, description: (_a = pre.description) !== null && _a !== void 0 ? _a : null, example: pre.example, examples: pre.examples, type: matched.type }, schema), { validate: legacy_1.JsonMetadataFactory.validate }); }) .filter((e) => e !== null); if (errors.length) ctx.errors.push(...errors); return Object.fromEntries(exceptions.map((e) => [e.status, e])); }; const analyzePreconfigured = (func) => { var _a; return (_a = Reflect.getMetadata("nestia/TypedException", func)) !== null && _a !== void 0 ? _a : []; }; })(ReflectHttpOperationExceptionAnalyzer || (exports.ReflectHttpOperationExceptionAnalyzer = ReflectHttpOperationExceptionAnalyzer = {})); //# sourceMappingURL=ReflectHttpOperationExceptionAnalyzer.js.map