UNPKG

swagger-typed-express-docs

Version:

Simple express runtime parser and documentation swagger generator with 100% support of Typescript static types

135 lines (134 loc) 5.17 kB
"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); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; 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.tSchemaToJsonSchemaWithDefinitions = exports.tSchemaToJsonSchema = void 0; var tSchemaToJsonSchema = function (schema, options) { var e_1, _a; var jsonSchema = {}; if (options === null || options === void 0 ? void 0 : options.title) { jsonSchema.title = options.title; } if (options === null || options === void 0 ? void 0 : options.description) { jsonSchema.description = options.description; } switch (schema.type) { case 'string': jsonSchema.type = 'string'; break; case 'number': jsonSchema.type = 'number'; break; case 'boolean': jsonSchema.type = 'boolean'; break; case 'any': break; case 'enum': jsonSchema.enum = schema.options; break; case 'oneOf': jsonSchema.oneOf = schema.options.map(function (option) { return (0, exports.tSchemaToJsonSchema)(option); }); break; case 'array': jsonSchema.type = 'array'; jsonSchema.items = (0, exports.tSchemaToJsonSchema)(schema.items); break; case 'object': jsonSchema.type = 'object'; jsonSchema.properties = {}; var requiredKeys = []; try { for (var _b = __values(Object.entries(schema.properties)), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), key = _d[0], value = _d[1]; jsonSchema.properties[key] = (0, exports.tSchemaToJsonSchema)(value); if (value.required) { requiredKeys.push(key); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } if (requiredKeys.length > 0) { jsonSchema.required = requiredKeys; } break; case 'hashMap': jsonSchema.type = 'object'; jsonSchema.additionalProperties = (0, exports.tSchemaToJsonSchema)(schema.property); break; case 'transformType': return (0, exports.tSchemaToJsonSchema)(schema.encodedTSchema, options); case 'lazy': var resolvedSchema = schema.getSchema(); return (0, exports.tSchemaToJsonSchema)(resolvedSchema, options); default: throw new Error("Unsupported TSchema type: ".concat(schema.type)); } return jsonSchema; }; exports.tSchemaToJsonSchema = tSchemaToJsonSchema; var tSchemaToJsonSchemaWithDefinitions = function (schema, definitions, options) { var e_2, _a; var jsonSchemaDefinitions = {}; try { for (var _b = __values(Object.entries(definitions)), _c = _b.next(); !_c.done; _c = _b.next()) { var _d = __read(_c.value, 2), name_1 = _d[0], defSchema = _d[1]; jsonSchemaDefinitions[name_1] = (0, exports.tSchemaToJsonSchema)(defSchema, options); } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_2) throw e_2.error; } } var result = (0, exports.tSchemaToJsonSchema)(schema, __assign(__assign({}, options), { definitions: jsonSchemaDefinitions })); if (Object.keys(jsonSchemaDefinitions).length > 0) { result.definitions = jsonSchemaDefinitions; } return result; }; exports.tSchemaToJsonSchemaWithDefinitions = tSchemaToJsonSchemaWithDefinitions;