UNPKG

@protocolnebula/ts-openapi-generator

Version:

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

214 lines 9.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ParserBaseService = void 0; const case_1 = require("case"); const enum_model_1 = require("../../../models/enum.model"); const model_attributes_model_1 = require("../../../models/model-attributes.model"); const model_model_1 = require("../../../models/model.model"); /** * This service Process all COMPONENTS (components/schemas/*) and convert it to ModelModel instances * */ class ParserBaseService { constructor(document, store) { this.document = document; this.store = store; } get modelStore() { return this.store.models; } get parameterStore() { return this.store.parameters; } getParameterUsedInType(inValue) { switch (inValue) { case 'query': case 'param': return inValue; default: console.error(`Parameter "in" ${inValue} not found. Query used.`); return 'query'; } } isEnumObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.enum) { return true; } return false; } isRefObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.$ref) { return true; } return false; } isSchemaObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.type) { return true; } return false; } isParameterObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.name) { return true; } return false; } isRequestBodyObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.content) { return true; } return false; } isResponseObject(rawAttribute) { if (rawAttribute === null || rawAttribute === void 0 ? void 0 : rawAttribute.content) { return true; } return false; } // Generic parsers /** * Get an Schema and get the REF * If is a custom model (scheme), it will create a new model * @param schema * @param modelName Name for the model if is not a "ref" */ parseApiParameters(parameters, modelName) { const newModel = new model_model_1.ModelModel(modelName); for (const rawParameter of parameters) { if (this.isRefObject(rawParameter)) { console.debug('Parameter object:', rawParameter.$ref); const parameter = this.store.parameters.getByUri(rawParameter.$ref); newModel.addAttribute(parameter.getAttribute()); } else if (this.isParameterObject(rawParameter)) { const parameter = this.parseParameter(rawParameter, modelName); newModel.addAttribute(parameter); } else { throw 'No schema available for this schema'; } } if (newModel.name) { this.store.models.add(newModel); } const instance = new model_attributes_model_1.ModelAttributessModel(null); instance.typeURI = newModel.uri; return instance; } parseParameter(rawParameter, modelName) { var _a; const parameter = new model_attributes_model_1.ModelAttributessModel(rawParameter.name); parameter.typeURI = (_a = this.parseSchema(rawParameter.schema, (0, case_1.capital)(`${modelName} ${rawParameter.name}`, '', true))) === null || _a === void 0 ? void 0 : _a.typeURI; parameter.usedIn = this.getParameterUsedInType(rawParameter.in); parameter.description = rawParameter.description; parameter.deprecated = rawParameter.deprecated; parameter.example = rawParameter.example; if (rawParameter.required) { parameter.isOptional = false; } return parameter; } parseSchema(schema, defaultName, mediaType = null) { if (!schema) { console.warn('WARNING: No schema defined! Any will be use instead'); console.warn('TIP: Don\'t fill "content" for responses if void'); const instance = new model_attributes_model_1.ModelAttributessModel(null); instance.typeURI = 'default'; return instance; } if (this.isRefObject(schema)) { const instance = new model_attributes_model_1.ModelAttributessModel(null); instance.typeURI = schema.$ref; return instance; } else if (this.isSchemaObject(schema)) { if (schema.type === 'array') { const instance = this.parseSchema(schema.items, defaultName, mediaType); instance.isArray = true; instance.arrayLevels++; return instance; } if (mediaType === 'text/html' || (schema === null || schema === void 0 ? void 0 : schema.type) !== 'object') { const instance = new model_attributes_model_1.ModelAttributessModel(null); instance.typeURI = schema.type; return instance; } const newModel = new model_model_1.ModelModel(defaultName); newModel.addAttributes(this.parseAttributes(schema, defaultName)); this.store.models.add(newModel); const instance = new model_attributes_model_1.ModelAttributessModel(null); instance.typeURI = newModel.uri; return instance; } else { throw `No schema available for ${defaultName}. Check that property contains the TYPE property.`; } } parseAttributes(rawModel, parentName) { const attributes = []; console.group('Parsing attributes'); for (const attrName in rawModel.properties) { const rawAttribute = rawModel.properties[attrName]; const attribute = new model_attributes_model_1.ModelAttributessModel(attrName); attribute.isOptional = !this.isAttributeRequired(attrName, rawModel.required); this.fillAttribute(attribute, rawAttribute, (0, case_1.capital)(`${parentName} ${attrName}`, '', true)); attributes.push(attribute); } console.groupEnd(); return attributes; } isAttributeRequired(attrName, requiredList) { return (requiredList === null || requiredList === void 0 ? void 0 : requiredList.indexOf(attrName)) > -1; } fillAttribute(attribute, rawAttribute, defaultName) { const attrName = attribute.name; if (this.isRefObject(rawAttribute)) { console.debug(`${attrName} is ref of ${rawAttribute.$ref}`); attribute.typeURI = rawAttribute.$ref; } else { attribute.usedIn = this.getParameterUsedInType(rawAttribute.in); attribute.description = rawAttribute.description; attribute.example = rawAttribute.example; attribute.deprecated = rawAttribute.deprecated; if (rawAttribute.nullable !== undefined) { attribute.isOptional = rawAttribute.nullable; } if (this.isSchemaObject(rawAttribute)) { if (rawAttribute.type === 'array') { console.group(`${attrName} is an array`); attribute.isArray = true; attribute.arrayLevels++; this.fillAttribute(attribute, rawAttribute.items, defaultName); console.groupEnd(); } else if (rawAttribute.enum) { // (this.isEnumObject(schema)) -> error TS2339:, so "pure if" is used instead console.debug(`${attrName} is ENUM of type ${rawAttribute.type}`); const newModel = new enum_model_1.EnumModel(`${defaultName}ENUM`); newModel.type = rawAttribute.type; newModel.values = rawAttribute.enum; this.store.models.add(newModel); attribute.typeURI = newModel.uri; } else if (rawAttribute.type === 'object' && rawAttribute.properties) { const newModel = new model_model_1.ModelModel(defaultName); newModel.addAttributes(this.parseAttributes(rawAttribute, defaultName)); this.store.models.add(newModel); attribute.typeURI = newModel.uri; } else { console.debug(`${attrName} of type ${rawAttribute.type}`); attribute.typeURI = rawAttribute.type; } } else { console.warn(`WARNING: ${attrName} not recognized OpenAPIV3 Schema type ${JSON.stringify(rawAttribute)}. DEFAULT will be used instead.`); attribute.typeURI = 'default'; } } } } exports.ParserBaseService = ParserBaseService; //# sourceMappingURL=parser-base.service.js.map