@nestia/sdk
Version:
Nestia SDK and Swagger generator
120 lines • 6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NestiaSwaggerComposer = void 0;
const openapi_1 = require("@samchon/openapi");
const path_1 = __importDefault(require("path"));
const tstl_1 = require("tstl");
const AccessorAnalyzer_1 = require("./analyses/AccessorAnalyzer");
const ConfigAnalyzer_1 = require("./analyses/ConfigAnalyzer");
const PathAnalyzer_1 = require("./analyses/PathAnalyzer");
const ReflectControllerAnalyzer_1 = require("./analyses/ReflectControllerAnalyzer");
const TypedHttpRouteAnalyzer_1 = require("./analyses/TypedHttpRouteAnalyzer");
const SwaggerGenerator_1 = require("./generates/SwaggerGenerator");
const IReflectOperationError_1 = require("./structures/IReflectOperationError");
const VersioningStrategy_1 = require("./utils/VersioningStrategy");
var NestiaSwaggerComposer;
(function (NestiaSwaggerComposer) {
NestiaSwaggerComposer.document = (app, config) => __awaiter(this, void 0, void 0, function* () {
const input = yield ConfigAnalyzer_1.ConfigAnalyzer.application(app);
const document = yield SwaggerGenerator_1.SwaggerGenerator.compose({
config,
routes: analyze(input),
document: yield SwaggerGenerator_1.SwaggerGenerator.initialize(config),
});
return config.openapi === "2.0"
? openapi_1.OpenApi.downgrade(document, "2.0")
: config.openapi === "3.0"
? openapi_1.OpenApi.downgrade(document, "3.0")
: document;
});
const analyze = (input) => {
var _a, _b, _c, _d;
// GET REFLECT CONTROLLERS
const unique = new WeakSet();
const project = {
input,
checker: null,
errors: [],
warnings: [],
};
const controllers = project.input.controllers
.map((c) => ReflectControllerAnalyzer_1.ReflectControllerAnalyzer.analyze({ project, controller: c, unique }))
.filter((c) => c !== null);
if (project.errors.length)
throw report({ type: "error", errors: project.errors });
// METADATA COMPONENTS
const collection = TypedHttpRouteAnalyzer_1.TypedHttpRouteAnalyzer.dictionary(controllers);
// CONVERT TO TYPED OPERATIONS
const globalPrefix = (_b = (_a = project.input.globalPrefix) === null || _a === void 0 ? void 0 : _a.prefix) !== null && _b !== void 0 ? _b : "";
const routes = [];
for (const c of controllers)
for (const o of c.operations) {
const pathList = new Set();
const versions = VersioningStrategy_1.VersioningStrategy.merge(project)([
...((_c = c.versions) !== null && _c !== void 0 ? _c : []),
...((_d = o.versions) !== null && _d !== void 0 ? _d : []),
]);
for (const v of versions)
for (const prefix of wrapPaths(c.prefixes))
for (const cPath of wrapPaths(c.paths))
for (const filePath of wrapPaths(o.paths))
pathList.add(PathAnalyzer_1.PathAnalyzer.join(globalPrefix, v, prefix, cPath, filePath));
if (o.protocol === "http")
routes.push(...TypedHttpRouteAnalyzer_1.TypedHttpRouteAnalyzer.analyze({
controller: c,
errors: project.errors,
dictionary: collection,
operation: o,
paths: Array.from(pathList),
}));
}
AccessorAnalyzer_1.AccessorAnalyzer.analyze(routes);
return routes;
};
})(NestiaSwaggerComposer || (exports.NestiaSwaggerComposer = NestiaSwaggerComposer = {}));
const report = (props) => {
const map = new tstl_1.TreeMap();
for (const e of props.errors)
map.take(new IReflectOperationError_1.IReflectOperationError.Key(e), () => []).push(...e.contents);
const messages = [];
for (const { first: { error }, second: contents, } of map) {
if (error.contents.length === 0)
continue;
const location = path_1.default.relative(process.cwd(), error.file);
messages.push([
`${location} - `,
error.class,
...(error.function !== null ? [`.${error.function}()`] : [""]),
...(error.from !== null ? [` from ${error.from}`] : [""]),
":\n",
contents
.map((c) => {
if (typeof c === "string")
return ` - ${c}`;
else
return [
c.accessor
? ` - ${c.name}: `
: ` - ${c.name} (${c.accessor}): `,
...c.messages.map((msg) => ` - ${msg}`),
].join("\n");
})
.join("\n"),
].join(""));
}
throw new Error(`Error on NestiaSwaggerComposer.compose():\n${messages}`);
};
const wrapPaths = (paths) => paths.length === 0 ? [""] : paths;
//# sourceMappingURL=NestiaSwaggerComposer.js.map
;