openapi-ts-generator
Version:
Based on swagger-ts-generator, this is a type script model generator specifically for services with OpenApi spec documentation.
374 lines (373 loc) • 22.7 kB
JavaScript
"use strict";
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OpenApiDocConverter = void 0;
var generator_options_1 = require("./models/generator-options");
var schema_info_1 = require("./models/schema-info");
var pluralize_1 = require("pluralize");
var lodash_1 = require("lodash");
var OpenApiDocConverter = /** @class */ (function () {
function OpenApiDocConverter(options, apiDocument) {
this.options = options;
this.apiDocument = apiDocument;
this.endAlphaNumRegex = /[A-z0-9]*$/s;
this.startNumberregex = /^\d*/;
}
OpenApiDocConverter.prototype.convertDocument = function () {
var entities = this.convertEntities();
var paths = this.convertPaths();
return { entities: entities, paths: paths };
};
OpenApiDocConverter.prototype.convertPaths = function () {
var paths = [];
for (var key in this.apiDocument.paths) {
var path = this.apiDocument.paths[key] || {};
var tagLookup = path.get || path.post || path.put;
tagLookup = tagLookup || path.delete || path.patch;
var tag = ((tagLookup === null || tagLookup === void 0 ? void 0 : tagLookup.tags) || ['unknown_endpoint'])[0];
paths.push({
tag: (0, lodash_1.snakeCase)(tag),
endpoint: this.options.pathUrlFormattingCallBack ? this.options.pathUrlFormattingCallBack(key) : key,
});
}
return paths;
};
OpenApiDocConverter.prototype.convertEntities = function () {
var _a, _b, _c;
var entities = [];
for (var schemaName in (_a = this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) {
if ((_b = this.apiDocument.components) === null || _b === void 0 ? void 0 : _b.schemas[schemaName]) {
var schemaWrapperInfo = new schema_info_1.SchemaWrapperInfo((_c = this.apiDocument.components) === null || _c === void 0 ? void 0 : _c.schemas[schemaName]);
if (schemaWrapperInfo.componentSchemaObject.enum) {
this.buildSchemaWrapperInfoForEnum(schemaWrapperInfo);
}
else {
this.buildSchemaWrapperInfo(schemaName, schemaWrapperInfo);
}
schemaWrapperInfo.updateReferenceProperties(this.options);
var entity = {
isEnum: schemaWrapperInfo.isEnum,
enumValues: schemaWrapperInfo.enumValues.map(function (t) {
var _a;
return typeof t === 'string' || t instanceof String
? t
: __assign(__assign({}, t), { key: (_a = t.key) !== null && _a !== void 0 ? _a : 0 });
}),
name: schemaName,
kebabCasedName: (0, lodash_1.kebabCase)(schemaName),
singularName: (0, pluralize_1.singular)(schemaName),
camelSingularName: (0, lodash_1.camelCase)((0, pluralize_1.singular)(schemaName)),
description: schemaWrapperInfo.description,
referenceProperties: schemaWrapperInfo.referenceProperties,
valueProperties: schemaWrapperInfo.valueProperties.filter(this.options.valuePropertyTypeFilterCallBack || generator_options_1.defaultFilter),
importTypes: this.getImportTypes(schemaName, schemaWrapperInfo),
};
entities.push(entity);
}
}
return entities.filter(this.options.typeFilterCallBack || generator_options_1.defaultFilter);
};
OpenApiDocConverter.prototype.buildSchemaWrapperInfoForEnum = function (schemaWrapperInfo) {
var _a;
var _this = this;
schemaWrapperInfo.isEnum = true;
(_a = schemaWrapperInfo.enumValues).push.apply(_a, (schemaWrapperInfo.componentSchemaObject.enum || []).map(function (x) {
var _a, _b, _c;
var key = (_a = _this.startNumberregex.exec(x)) === null || _a === void 0 ? void 0 : _a.at(0);
var name = (_c = (_b = _this.endAlphaNumRegex.exec(x)) === null || _b === void 0 ? void 0 : _b.at(0)) !== null && _c !== void 0 ? _c : '';
return {
key: key ? +key : 0,
name: name,
titleName: (0, lodash_1.startCase)(name),
snakeCaseName: (0, lodash_1.snakeCase)(name).toUpperCase(),
};
}));
};
OpenApiDocConverter.prototype.buildSchemaWrapperInfo = function (parentTypeName, schemaWrapperInfo) {
for (var propertyName in schemaWrapperInfo.componentSchemaObject.properties) {
if ((schemaWrapperInfo.propertySchemaObject = schemaWrapperInfo.componentSchemaObject.properties[propertyName]).type && // NOSONAR
schemaWrapperInfo.propertySchemaObject.type !== 'array') {
schemaWrapperInfo.valueProperties.push(this.convertSchemaObjectToPropertyType(parentTypeName, propertyName, schemaWrapperInfo));
}
else {
schemaWrapperInfo.propertyReferenceObject = schemaWrapperInfo.componentSchemaObject.properties[propertyName];
if (schemaWrapperInfo.propertyReferenceObject.$ref) {
schemaWrapperInfo.referenceProperties.push(this.convertReferenceObjectToPropertyType(parentTypeName, propertyName, schemaWrapperInfo));
}
else if (schemaWrapperInfo.propertySchemaObject.type === 'array' && schemaWrapperInfo.propertySchemaObject.items) {
this.convertArray(parentTypeName, propertyName, schemaWrapperInfo);
}
}
}
};
OpenApiDocConverter.prototype.convertArray = function (parentTypeName, propertyName, schemaWrapperInfo) {
var arraySchemaObject = schemaWrapperInfo.propertySchemaObject.items;
if (arraySchemaObject.type) {
schemaWrapperInfo.valueProperties.push(this.convertArrayObjectToValuePropertyType(parentTypeName, propertyName, schemaWrapperInfo));
}
else {
schemaWrapperInfo.propertyReferenceObject = schemaWrapperInfo.propertySchemaObject.items;
schemaWrapperInfo.referenceProperties.push(this.convertArrayObjectToReferencePropertyType(parentTypeName, propertyName, schemaWrapperInfo));
}
};
OpenApiDocConverter.prototype.convertSchemaObjectToPropertyType = function (parentTypeName, propertyName, schemaWrapperInfo) {
var _a, _b;
var required = this.getIsRequired(propertyName, schemaWrapperInfo);
var validatorCount = this.getValidatorCount(propertyName, schemaWrapperInfo);
var initialValue = this.options.genAngularFormGroupsWithDefaultValues
? this.getInitialValue(propertyName, schemaWrapperInfo)
: 'undefined';
var initialTestValue = this.getInitialTestValue(parentTypeName, propertyName, schemaWrapperInfo);
return {
required: required,
name: propertyName,
initialValue: initialValue,
initialTestValue: initialTestValue,
isArray: false,
snakeCaseName: (0, lodash_1.snakeCase)(propertyName).toUpperCase(),
typeScriptType: this.getPropertyTypeScriptType(schemaWrapperInfo),
maxLength: schemaWrapperInfo.propertySchemaObject.maxLength,
minLength: schemaWrapperInfo.propertySchemaObject.minLength,
maximum: schemaWrapperInfo.propertySchemaObject.maximum,
minimum: schemaWrapperInfo.propertySchemaObject.minimum,
email: ((_a = schemaWrapperInfo.propertySchemaObject.format) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'email',
uri: ((_b = schemaWrapperInfo.propertySchemaObject.format) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'uri',
minItems: schemaWrapperInfo.propertySchemaObject.minItems,
maxItems: schemaWrapperInfo.propertySchemaObject.maxItems,
description: schemaWrapperInfo.propertySchemaObject.description,
pattern: schemaWrapperInfo.propertySchemaObject.pattern,
hasMultipleValidators: validatorCount > 1,
hasValidators: validatorCount > 0,
};
};
OpenApiDocConverter.prototype.convertValidator = function (validationValue) {
var exists = validationValue !== null && validationValue !== undefined;
return +exists;
};
OpenApiDocConverter.prototype.convertArrayObjectToValuePropertyType = function (parentTypeName, propertyName, schemaWrapperInfo) {
var required = this.getIsRequired(propertyName, schemaWrapperInfo);
var validatorCount = this.getValidatorCount(propertyName, schemaWrapperInfo);
var initialValue = this.options.genAngularFormGroupsWithDefaultValues
? this.getInitialValue(propertyName, schemaWrapperInfo)
: 'undefined';
var initialTestValue = this.getInitialTestValue(parentTypeName, propertyName, schemaWrapperInfo);
return {
required: required,
typeScriptType: this.getPropertyTypeScriptType(schemaWrapperInfo),
initialValue: initialValue,
initialTestValue: initialTestValue,
name: propertyName,
email: false,
uri: false,
isArray: true,
snakeCaseName: (0, lodash_1.snakeCase)(propertyName).toUpperCase(),
hasMultipleValidators: false,
hasValidators: validatorCount > 0,
};
};
OpenApiDocConverter.prototype.getInitialValue = function (propertyName, schemaWrapperInfo) {
var _a, _b;
var typescriptType = this.getPropertyTypeScriptType(schemaWrapperInfo);
var isRequired = this.getIsRequired(propertyName, schemaWrapperInfo);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
var refName = (((_a = schemaWrapperInfo === null || schemaWrapperInfo === void 0 ? void 0 : schemaWrapperInfo.componentSchemaObject) === null || _a === void 0 ? void 0 : _a.properties) || {})[propertyName].$ref;
var refObject = (((_b = this.apiDocument.components) === null || _b === void 0 ? void 0 : _b.schemas) || {})[refName];
var defaultValue = (schemaWrapperInfo.componentSchemaObject.default || (refObject === null || refObject === void 0 ? void 0 : refObject.default) || ((refObject === null || refObject === void 0 ? void 0 : refObject.enum) || [])[0]);
if (!isRequired) {
return 'null';
}
else if (defaultValue && refObject.enum) {
return "".concat(schemaWrapperInfo.propertyReferenceObject['$ref'], ".").concat(defaultValue.split(' ').pop());
}
else if (defaultValue) {
return "'".concat(defaultValue.split(' ').pop(), "'");
}
else if (typescriptType === 'Date') {
return 'new Date()';
}
else if (typescriptType === 'boolean') {
return 'false';
}
else if (typescriptType === 'number') {
return '0';
}
else {
return "''";
}
};
OpenApiDocConverter.prototype.getInitialTestValue = function (parentTypeName, propertyName, schemaWrapperInfo) {
var _a, _b, _c, _d, _e, _f;
var typescriptType = this.getPropertyTypeScriptType(schemaWrapperInfo);
var schemaObject = ((_b = (_a = schemaWrapperInfo === null || schemaWrapperInfo === void 0 ? void 0 : schemaWrapperInfo.componentSchemaObject) === null || _a === void 0 ? void 0 : _a.properties) !== null && _b !== void 0 ? _b : {})[propertyName];
var maxLength = schemaWrapperInfo.propertySchemaObject.maxLength;
var minLength = schemaWrapperInfo.propertySchemaObject.minLength;
var minValue = schemaWrapperInfo.propertySchemaObject.minimum;
var email = ((_c = schemaWrapperInfo.propertySchemaObject.format) === null || _c === void 0 ? void 0 : _c.toLowerCase()) === 'email';
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
var refName = (schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.$ref) || ((_d = schemaObject.items) === null || _d === void 0 ? void 0 : _d.$ref);
var refObject = ((_f = (_e = this.apiDocument.components) === null || _e === void 0 ? void 0 : _e.schemas) !== null && _f !== void 0 ? _f : {})[refName];
var defaultValue = (schemaWrapperInfo.componentSchemaObject.default || (refObject === null || refObject === void 0 ? void 0 : refObject.default) || ((refObject === null || refObject === void 0 ? void 0 : refObject.enum) || [])[0]);
if (defaultValue && refObject.enum && schemaObject.type === 'array') {
return "[".concat(schemaWrapperInfo.propertyReferenceObject['$ref'], ".").concat(defaultValue.split(' ').pop(), "]");
}
else if (defaultValue && refObject.enum) {
return "".concat(schemaWrapperInfo.propertyReferenceObject['$ref'], ".").concat(defaultValue.split(' ').pop());
}
else if (refObject) {
return schemaObject.type === 'array' ? "[]" : "undefined";
}
else if (defaultValue) {
return "'".concat(defaultValue.split(' ').pop(), "'");
}
else if (email) {
return "'".concat((0, lodash_1.kebabCase)(parentTypeName), "@email.org'");
}
else if (typescriptType === 'Date') {
return 'new Date()';
}
else if (typescriptType === 'boolean') {
return 'false';
}
else if (typescriptType === 'number' && schemaObject.type === 'array') {
return minValue ? "[".concat(minValue, "]") : '[0]';
}
else if (schemaObject.type === 'array') {
return defaultValue ? "[".concat(defaultValue, "]") : '[]';
}
else if (typescriptType === 'number') {
return minValue ? "".concat(minValue) : '0';
}
else {
var retValue = (0, lodash_1.snakeCase)(propertyName).toUpperCase();
while (minLength && retValue.length < minLength) {
retValue = "".concat(retValue, "_").concat(retValue);
}
return "'".concat(maxLength ? retValue.substring(0, maxLength) : retValue, "'");
}
};
OpenApiDocConverter.prototype.convertArrayObjectToReferencePropertyType = function (parentTypeName, propertyName, schemaWrapperInfo) {
var refProperty = __assign(__assign({}, this.convertReferenceObjectToPropertyType(parentTypeName, propertyName, schemaWrapperInfo)), { isArray: true });
refProperty.isEnumAndArray = refProperty.isEnum && refProperty.isArray;
return refProperty;
};
OpenApiDocConverter.prototype.convertReferenceObjectToPropertyType = function (parentTypeName, propertyName, schemaWrapperInfo) {
var _a, _b, _c;
var propertySchema = (((_a = this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[this.parseRef(schemaWrapperInfo)];
var refSchema = (((_b = schemaWrapperInfo === null || schemaWrapperInfo === void 0 ? void 0 : schemaWrapperInfo.componentSchemaObject) === null || _b === void 0 ? void 0 : _b.properties) || {})[propertyName];
var required = this.getIsRequired(propertyName, schemaWrapperInfo);
var validatorCount = this.getValidatorCount(propertyName, schemaWrapperInfo);
var initialValue = this.options.genAngularFormGroupsWithDefaultValues
? this.getInitialValue(propertyName, schemaWrapperInfo)
: 'undefined';
var initialTestValue = this.getInitialTestValue(parentTypeName, propertyName, schemaWrapperInfo);
var typeName = this.parseRef(schemaWrapperInfo);
return {
required: required,
name: propertyName,
isSameAsParentTypescriptType: parentTypeName.toLowerCase() === typeName.toLowerCase(),
initialValue: initialValue,
initialTestValue: initialTestValue,
snakeCaseName: (0, lodash_1.snakeCase)(propertyName).toUpperCase(),
referenceTypeName: typeName,
typeScriptType: typeName,
isArray: false,
isEnum: ((_c = propertySchema === null || propertySchema === void 0 ? void 0 : propertySchema.enum) !== null && _c !== void 0 ? _c : []).length > 0,
isEnumAndArray: false,
hasValidators: validatorCount > 0,
hasMultipleValidators: validatorCount > 1,
maxLength: refSchema === null || refSchema === void 0 ? void 0 : refSchema.maxLength,
minLength: refSchema === null || refSchema === void 0 ? void 0 : refSchema.minLength,
maximum: refSchema === null || refSchema === void 0 ? void 0 : refSchema.maximum,
minimum: refSchema === null || refSchema === void 0 ? void 0 : refSchema.minimum,
minItems: refSchema === null || refSchema === void 0 ? void 0 : refSchema.minItems,
maxItems: refSchema === null || refSchema === void 0 ? void 0 : refSchema.maxItems,
};
};
OpenApiDocConverter.prototype.getValidatorCount = function (propertyName, schemaWrapperInfo) {
var _a, _b;
var required = this.getIsRequired(propertyName, schemaWrapperInfo);
var email = ((_a = schemaWrapperInfo.propertySchemaObject.format) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === 'email' || false;
var uri = ((_b = schemaWrapperInfo.propertySchemaObject.format) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === 'uri' || false;
return (+required +
+email +
+uri +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.maxLength) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.minLength) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.maximum) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.minimum) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.maxItems) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.minItems) +
+this.convertValidator(schemaWrapperInfo.propertySchemaObject.pattern));
};
OpenApiDocConverter.prototype.getPropertyTypeScriptType = function (schemaWrapperInfo) {
var _a;
if (schemaWrapperInfo.propertySchemaObject.type === 'array' && schemaWrapperInfo.propertySchemaObject.items) {
var type = schemaWrapperInfo.propertySchemaObject.items.type;
return type === 'integer' ? 'number' : type;
}
else if (schemaWrapperInfo.propertySchemaObject.type === 'integer' && schemaWrapperInfo.propertySchemaObject.enum) {
return 'string | number';
}
else if (schemaWrapperInfo.propertySchemaObject.type === 'integer') {
return 'number';
}
else if (schemaWrapperInfo.propertySchemaObject.format === 'date' || schemaWrapperInfo.propertySchemaObject.format === 'date-time') {
return 'Date';
}
return (_a = schemaWrapperInfo.propertySchemaObject.type) !== null && _a !== void 0 ? _a : 'string';
};
OpenApiDocConverter.prototype.parseRef = function (schemaWrapperInfo) {
var regexResult;
var result = null;
if (schemaWrapperInfo.propertyReferenceObject.$ref &&
// tslint:disable-next-line: no-conditional-assignment
(regexResult = this.endAlphaNumRegex.exec(schemaWrapperInfo.propertyReferenceObject.$ref)) // NOSONAR
) {
schemaWrapperInfo.propertyReferenceObject.$ref = regexResult[0];
result = schemaWrapperInfo.propertyReferenceObject.$ref;
}
return result || 'unknown';
};
OpenApiDocConverter.prototype.getImportTypes = function (entityName, schemaWrapperInfo) {
var _a;
var _this = this;
var _b, _c, _d;
var schemaProperties = (_d = ((_c = (_b = this.apiDocument.components) === null || _b === void 0 ? void 0 : _b.schemas) !== null && _c !== void 0 ? _c : (_a = {}, _a[entityName] = { properties: {} }, _a))[entityName].properties) !== null && _d !== void 0 ? _d : {};
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
var properties = Object.keys(schemaProperties).map(function (key) { return (__assign(__assign({ key: key }, schemaProperties[key]), { $ref: schemaProperties[key].$ref, items: schemaProperties[key].items || {}, type: schemaProperties[key].type })); });
return schemaWrapperInfo.referenceProperties
.map(function (t) { return t.referenceTypeName; })
.filter(function (value, index, array) { return array.indexOf(value) === index; })
.map(function (value) {
var _a, _b;
var refSchema = (((_a = _this.apiDocument.components) === null || _a === void 0 ? void 0 : _a.schemas) || {})[value];
var props = properties.filter(function (t) { return t.items.$ref === value || t.$ref === value; });
return {
name: value,
kebabCasedTypeName: (0, lodash_1.kebabCase)(value),
isEnum: ((_b = refSchema === null || refSchema === void 0 ? void 0 : refSchema.enum) !== null && _b !== void 0 ? _b : []).length > 0,
areAllArrays: props.every(function (val) { return val.type === 'array'; }),
hasArrays: props.some(function (val) { return val.type === 'array'; }),
isSelfReferencing: entityName === value,
};
});
};
OpenApiDocConverter.prototype.getIsRequired = function (propertyName, schemaWrapperInfo) {
var _a;
return ((((_a = schemaWrapperInfo.componentSchemaObject.required) !== null && _a !== void 0 ? _a : []).indexOf(propertyName) > -1 ||
(schemaWrapperInfo.propertySchemaObject.nullable === undefined ? false : !schemaWrapperInfo.propertySchemaObject.nullable)) &&
propertyName !== 'id');
};
return OpenApiDocConverter;
}());
exports.OpenApiDocConverter = OpenApiDocConverter;