UNPKG

@protocolnebula/ts-openapi-generator

Version:

Build API and models from Swagger/OpenAPI to use in any project type

91 lines 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ComponentsParserService = void 0; const parameter_model_1 = require("../../../models/parameter.model"); const enum_model_1 = require("../../../models/enum.model"); const model_model_1 = require("../../../models/model.model"); const parser_base_service_1 = require("./parser-base.service"); const objects_util_1 = require("../../../utils/objects.util"); /** * This service Process all COMPONENTS (components/schemas/*) and convert it to ModelModel instances * */ class ComponentsParserService extends parser_base_service_1.ParserBaseService { constructor(document, store) { super(document, store); } process() { this.processParameters(); this.processSchemas(); } processParameters() { var _a; console.group("Processing parameters from 'components'"); const parameterRawList = (_a = this.document.components) === null || _a === void 0 ? void 0 : _a.parameters; for (let parameterName in parameterRawList) { console.group('Processing parameter', parameterName); const rawModel = parameterRawList[parameterName]; const parameter = new parameter_model_1.ParameterModel(parameterName, this.parseParameter(rawModel, parameterName)); this.parameterStore.add(parameter); console.groupEnd(); } console.groupEnd(); } processSchemas() { var _a; console.group("Processing models from 'components'"); const modelRawList = (_a = this.document.components) === null || _a === void 0 ? void 0 : _a.schemas; for (let modelName in modelRawList) { console.group('Processing model', modelName); const rawModel = modelRawList[modelName]; const schemaParsed = this.processSchema(modelName, rawModel); if (schemaParsed) { this.modelStore.add(schemaParsed); } console.groupEnd(); } console.groupEnd(); } processSchema(modelName, rawModel) { let elementRef; if (rawModel.type === 'object') { elementRef = new model_model_1.ModelModel(modelName); elementRef.addAttributes(this.parseAttributes(rawModel, elementRef.name)); } else if (this.isEnumObject(rawModel)) { console.debug(`${modelName} is ENUM of type ${rawModel.type}`); elementRef = new enum_model_1.EnumModel(modelName); elementRef.type = rawModel.type; elementRef.values = rawModel.enum; } else if (this.isRefObject(rawModel)) { const ref = rawModel.$ref; console.error(`${modelName} is a pure ref of ${ref}`); const refSchema = this.findRefSchema(ref); if (!refSchema) { throw `Schema ${ref} not found`; } return this.processSchema(modelName, refSchema); } else { console.error(`ERROR: ${modelName} not a correct Schema`); if ((rawModel === null || rawModel === void 0 ? void 0 : rawModel.type) === 'array') { console.error(`ERROR: ARRAY not supported on COMPONENTS SCHEMA`); } } if (elementRef) { elementRef.description = rawModel.description; elementRef.example = rawModel.example; elementRef.deprecated = rawModel.deprecated; } return elementRef; } findRefSchema($ref) { if ($ref.indexOf('#') === 0) { return (0, objects_util_1.findInObject)($ref, this.document); } throw `$ref: ${$ref} not supported yet!`; } } exports.ComponentsParserService = ComponentsParserService; //# sourceMappingURL=components-parser.service.js.map