123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- "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.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;
- }
- 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.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
|