swagger-typed-express-docs
Version:
Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types
70 lines (69 loc) • 2.45 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.codegenSchemaToJSCode = void 0;
var codegenSchemaToJSCode = function (schema) {
var str = '';
switch (schema.type) {
case 'enum':
str = "T.enum([".concat(schema.options.map(function (o) { return "'".concat(o, "'"); }).join(', '), "])");
break;
case 'object':
str = "T.object({\n ".concat(Object.entries(schema.properties)
.map(function (_a) {
var _b = __read(_a, 2), k = _b[0], v = _b[1];
return "'".concat(k, "': ").concat((0, exports.codegenSchemaToJSCode)(v));
})
.join(', \n'), "\n })");
break;
case 'hashMap':
str = "T.hashMap(".concat((0, exports.codegenSchemaToJSCode)(schema.property), ")");
break;
case 'array':
str = "T.list(".concat((0, exports.codegenSchemaToJSCode)(schema.items), ")");
break;
case 'oneOf':
str = "T.oneOf([".concat(schema.options.map(function (o) { return (0, exports.codegenSchemaToJSCode)(o); }).join(', '), "])");
break;
case 'any':
str = "T.any";
break;
case 'string':
str = "T.string";
break;
case 'boolean':
str = "T.boolean";
break;
case 'number':
str = "T.number";
break;
case 'transformType':
str = "T.any";
break;
default:
throw new Error("unsupported to string formatter for: ".concat(JSON.stringify(schema)));
}
if (!schema.required) {
if (!str.startsWith('T.')) {
throw new Error("invalid js stringified codegen code for required");
}
str = str.replace('T.', 'T.null_');
}
return str;
};
exports.codegenSchemaToJSCode = codegenSchemaToJSCode;
;