UNPKG

@ivamuno/nestjs-openapi

Version:
66 lines (65 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AsyncapiScanner = void 0; const constants_1 = require("@nestjs/common/constants"); const model_properties_accessor_1 = require("@nestjs/swagger/dist/services/model-properties-accessor"); const schema_object_factory_1 = require("@nestjs/swagger/dist/services/schema-object-factory"); const swagger_types_mapper_1 = require("@nestjs/swagger/dist/services/swagger-types-mapper"); const strip_last_slash_util_1 = require("@nestjs/swagger/dist/utils/strip-last-slash.util"); const lodash_1 = require("lodash"); const _1 = require("."); const asyncapi_transformer_1 = require("./asyncapi.transformer"); class AsyncapiScanner { constructor() { this.transfomer = new asyncapi_transformer_1.AsyncapiTransformer(); this.explorer = new _1.AsyncApiExplorer(); this.modelPropertiesAccessor = new model_properties_accessor_1.ModelPropertiesAccessor(); this.swaggerTypesMapper = new swagger_types_mapper_1.SwaggerTypesMapper(); this.schemaObjectFactory = new schema_object_factory_1.SchemaObjectFactory(this.modelPropertiesAccessor, this.swaggerTypesMapper); } scanApplication(app, options) { const { deepScanRoutes, include: includedModules = [], extraModels = [], ignoreGlobalPrefix = false, operationIdFactory } = options; const container = app.container; const modules = this.getModules(container.getModules(), includedModules); const globalPrefix = !ignoreGlobalPrefix ? strip_last_slash_util_1.stripLastSlash(this.getGlobalPrefix(app)) : ''; const denormalizedChannels = modules.map(({ components, metatype, relatedModules }) => { let allComponents = new Map(components); if (deepScanRoutes) { const isGlobal = (module) => !container.isGlobalModule(module); Array.from(relatedModules.values()) .filter(isGlobal) .map(({ components: relatedComponents }) => relatedComponents) .forEach(relatedComponents => { allComponents = new Map([...allComponents, ...relatedComponents]); }); } const path = metatype ? Reflect.getMetadata(constants_1.MODULE_PATH, metatype) : undefined; return this.scanModuleComponents(allComponents, path, globalPrefix, operationIdFactory); }); const schemas = this.explorer.getSchemas(); this.addExtraModels(schemas, extraModels); const normalizedChannels = this.transfomer.normalizeChannels(lodash_1.flatten(denormalizedChannels)); const newComponents = { schemas: lodash_1.reduce(schemas, lodash_1.extend) }; return Object.assign(Object.assign({}, normalizedChannels), { components: newComponents }); } scanModuleComponents(components, modulePath, globalPrefix, operationIdFactory) { const denormalizedArray = [...components.values()].map(comp => this.explorer.exploreChannel(comp, modulePath, globalPrefix, operationIdFactory)); return lodash_1.flatten(denormalizedArray); } getModules(modulesContainer, include) { if (!include || lodash_1.isEmpty(include)) { return [...modulesContainer.values()]; } return [...modulesContainer.values()].filter(({ metatype }) => include.some(item => item === metatype)); } getGlobalPrefix(app) { const internalConfigRef = app.config; return (internalConfigRef && internalConfigRef.getGlobalPrefix()) || ''; } addExtraModels(schemas, extraModels) { extraModels.forEach(item => { this.schemaObjectFactory.exploreModelSchema(item, schemas); }); } } exports.AsyncapiScanner = AsyncapiScanner;