UNPKG

3.95 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.SwaggerScanner = void 0;
4const constants_1 = require("@nestjs/common/constants");
5const lodash_1 = require("lodash");
6const model_properties_accessor_1 = require("./services/model-properties-accessor");
7const schema_object_factory_1 = require("./services/schema-object-factory");
8const swagger_types_mapper_1 = require("./services/swagger-types-mapper");
9const swagger_explorer_1 = require("./swagger-explorer");
10const swagger_transformer_1 = require("./swagger-transformer");
11const get_global_prefix_1 = require("./utils/get-global-prefix");
12const strip_last_slash_util_1 = require("./utils/strip-last-slash.util");
13class SwaggerScanner {
14 constructor() {
15 this.transfomer = new swagger_transformer_1.SwaggerTransformer();
16 this.schemaObjectFactory = new schema_object_factory_1.SchemaObjectFactory(new model_properties_accessor_1.ModelPropertiesAccessor(), new swagger_types_mapper_1.SwaggerTypesMapper());
17 this.explorer = new swagger_explorer_1.SwaggerExplorer(this.schemaObjectFactory);
18 }
19 scanApplication(app, options) {
20 const { deepScanRoutes, include: includedModules = [], extraModels = [], ignoreGlobalPrefix = false, operationIdFactory } = options;
21 const container = app.container;
22 const internalConfigRef = app.config;
23 const modules = this.getModules(container.getModules(), includedModules);
24 const globalPrefix = !ignoreGlobalPrefix
25 ? strip_last_slash_util_1.stripLastSlash(get_global_prefix_1.getGlobalPrefix(app))
26 : '';
27 const denormalizedPaths = modules.map(({ routes, metatype, relatedModules }) => {
28 let result = [];
29 if (deepScanRoutes) {
30 const isGlobal = (module) => !container.isGlobalModule(module);
31 Array.from(relatedModules.values())
32 .filter(isGlobal)
33 .forEach(({ metatype, routes }) => {
34 const modulePath = this.getModulePathMetadata(container, metatype);
35 result = result.concat(this.scanModuleRoutes(routes, modulePath, globalPrefix, internalConfigRef, operationIdFactory));
36 });
37 }
38 const modulePath = this.getModulePathMetadata(container, metatype);
39 return result.concat(this.scanModuleRoutes(routes, modulePath, globalPrefix, internalConfigRef, operationIdFactory));
40 });
41 const schemas = this.explorer.getSchemas();
42 this.addExtraModels(schemas, extraModels);
43 return Object.assign(Object.assign({}, this.transfomer.normalizePaths(lodash_1.flatten(denormalizedPaths))), { components: {
44 schemas: schemas
45 } });
46 }
47 scanModuleRoutes(routes, modulePath, globalPrefix, applicationConfig, operationIdFactory) {
48 const denormalizedArray = [...routes.values()].map((ctrl) => this.explorer.exploreController(ctrl, applicationConfig, modulePath, globalPrefix, operationIdFactory));
49 return lodash_1.flatten(denormalizedArray);
50 }
51 getModules(modulesContainer, include) {
52 if (!include || lodash_1.isEmpty(include)) {
53 return [...modulesContainer.values()];
54 }
55 return [...modulesContainer.values()].filter(({ metatype }) => include.some((item) => item === metatype));
56 }
57 addExtraModels(schemas, extraModels) {
58 extraModels.forEach((item) => {
59 this.schemaObjectFactory.exploreModelSchema(item, schemas);
60 });
61 }
62 getModulePathMetadata(container, metatype) {
63 const modulesContainer = container.getModules();
64 const modulePath = Reflect.getMetadata(constants_1.MODULE_PATH + modulesContainer.applicationId, metatype);
65 return modulePath !== null && modulePath !== void 0 ? modulePath : Reflect.getMetadata(constants_1.MODULE_PATH, metatype);
66 }
67}
68exports.SwaggerScanner = SwaggerScanner;