swagger-typed-express-docs
Version:
Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types
89 lines (88 loc) • 3.39 kB
JavaScript
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tSchemaToTypescript = void 0;
var tSchemaToTypescript = function (schema, indentLevel) {
if (indentLevel === void 0) { indentLevel = 0; }
var str = '';
var indent = ' '.repeat(indentLevel);
switch (schema.type) {
case 'enum':
str = schema.options.map(function (o) { return "'".concat(o, "'"); }).join(' | ');
break;
case 'object':
var properties = Object.entries(schema.properties);
if (properties.length === 0) {
str = '{}';
}
else {
var requiredKeys_1 = properties.filter(function (_a) {
var _b = __read(_a, 2), _ = _b[0], v = _b[1];
return v.required === true;
}).map(function (_a) {
var _b = __read(_a, 2), k = _b[0], _ = _b[1];
return k;
});
str = "{\n".concat(properties
.map(function (_a) {
var _b = __read(_a, 2), k = _b[0], v = _b[1];
var isRequired = requiredKeys_1.includes(k);
var key = "'".concat(k, "'").concat(isRequired ? '' : '?', ":");
var value = (0, exports.tSchemaToTypescript)(v, indentLevel + 1);
return "".concat(indent, " ").concat(key, " ").concat(value);
})
.join(';\n'), ";\n").concat(indent, "}");
}
break;
case 'hashMap':
str = "{ [key: string]: ".concat((0, exports.tSchemaToTypescript)(schema.property, indentLevel + 1), " }");
break;
case 'array': {
var itemType = schema.items.required
? "".concat((0, exports.tSchemaToTypescript)(schema.items, indentLevel))
: "(".concat((0, exports.tSchemaToTypescript)(schema.items, indentLevel), ")");
str = "".concat(itemType, "[]");
break;
}
case 'oneOf':
str = schema.options.map(function (o) { return (0, exports.tSchemaToTypescript)(o, indentLevel); }).join(' | ');
break;
case 'any':
str = "any";
break;
case 'string':
str = "string";
break;
case 'boolean':
str = "boolean";
break;
case 'number':
str = "number";
break;
case 'transformType':
str = (0, exports.tSchemaToTypescript)(schema.encodedTSchema, indentLevel);
break;
default:
throw new Error("Unsupported type: ".concat(JSON.stringify(schema)));
}
if (!schema.required) {
str = "".concat(str, " | null | undefined");
}
return str;
};
exports.tSchemaToTypescript = tSchemaToTypescript;
;