"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SwaggerDoc = void 0; const http_1 = require("../http"); class SwaggerDoc { static get(route) { return new SwaggerDoc(http_1.HttpMethod.GET).setRoute(route); } static post(route) { return new SwaggerDoc(http_1.HttpMethod.POST).setRoute(route); } constructor(method) { this.parameters = []; this.responses = []; this.security = []; this.method = method; } setRoute(route) { this.route = route; return this; } setSummary(summary) { this.summary = summary; return this; } setDescription(description) { this.description = description; return this; } setRequestBody(requestBody) { this.requestBody = requestBody; return this; } addParameter(param) { this.parameters.push(param); return this; } addResponse(res) { this.responses.push(res); return this; } addSecurityScheme(scheme) { this.security.push(scheme); return this; } toAnnotation() { let annotation = '/**\n'; annotation += ' *\t@swagger\n'; annotation += ` *\t${this.route}:\n`; annotation += ` *\t\t${this.getMethod()}:\n`; if (this.summary) annotation += ` *\t\t\tsummary: ${this.summary}\n`; if (this.description) annotation += ` *\t\t\tdescription: ${this.description}\n`; if (this.security.length > 0) { annotation += ' *\t\t\tsecurity:\n'; for (const scheme of this.security) { annotation += ` *\t\t\t\t- ${scheme}: []\n`; } } if (this.parameters.length > 0) { annotation += ' *\t\t\tparameters:\n'; for (const param of this.parameters) { annotation += ` *\t\t\t- in: ${param.in}\n`; annotation += ` *\t\t\t name: ${param.name}\n`; annotation += ` *\t\t\t required: ${param.required}\n`; annotation += ` *\t\t\t description: ${param.description}\n`; if (param.schema) { annotation += ' *\t\t\t schema:\n'; annotation += this.deserializeSchema(param.schema, 4); } } } if (this.requestBody) { annotation += ' *\t\t\trequestBody:\n'; annotation += ` *\t\t\t\trequired: ${this.requestBody.required}\n`; annotation += ` *\t\t\t\tdescription: ${this.requestBody.description}\n`; annotation += ' *\t\t\t\tcontent:\n'; annotation += ` *\t\t\t\t\t${this.requestBody.content.mediaType}:\n`; annotation += ' *\t\t\t\t\t\tschema:\n'; annotation += this.deserializeSchema(this.requestBody.content.schema, 6); } if (this.responses.length > 0) { annotation += ' *\t\t\tresponses:\n'; for (const res of this.responses) { annotation += ` *\t\t\t\t${res.code}:\n`; annotation += ` *\t\t\t\t\tdescription: ${res.description}\n`; if (res.content) { annotation += ' *\t\t\t\t\tcontent:\n'; annotation += ` *\t\t\t\t\t\t${res.content.mediaType}:\n`; annotation += ' *\t\t\t\t\t\t\tschema:\n'; annotation += this.deserializeSchema(res.content.schema, 7); } } } annotation += ' */\n'; return annotation.replace(/\t/g, ' '); } getMethod() { switch (this.method) { case http_1.HttpMethod.GET: return 'get'; case http_1.HttpMethod.POST: return 'post'; default: return 'undefined'; } } deserializeSchema(schema, level) { let res = ''; const indent = ' *' + this.repeatStr('\t', level + 1); res += indent + `type: ${schema.type}\n`; if (schema.description) res += indent + `description: ${schema.description}\n`; if (schema.example) res += indent + `example: ${schema.example}\n`; if (schema.items && schema.items.length > 0) { res += indent + 'items:\n'; for (const item of schema.items) { res += this.deserializeSchema(item, level + 2); } } if (schema.properties && Object.keys(schema.properties).length > 0) { res += indent + 'properties:\n'; for (const key of Object.keys(schema.properties)) { res += indent + '\t' + `${key}:\n`; res += this.deserializeSchema(schema.properties[key], level + 3); } } return res; } repeatStr(str, n) { let res = ''; for (let i = 0; i < n; i++) res += str; return res; } } exports.SwaggerDoc = SwaggerDoc; //# sourceMappingURL=SwaggerDoc.js.map