SwaggerDoc.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SwaggerDoc = void 0;
  4. const http_1 = require("../http");
  5. class SwaggerDoc {
  6. static get(route) {
  7. return new SwaggerDoc(http_1.HttpMethod.GET).setRoute(route);
  8. }
  9. static post(route) {
  10. return new SwaggerDoc(http_1.HttpMethod.POST).setRoute(route);
  11. }
  12. constructor(method) {
  13. this.parameters = [];
  14. this.responses = [];
  15. this.method = method;
  16. }
  17. setRoute(route) {
  18. this.route = route;
  19. return this;
  20. }
  21. setSummary(summary) {
  22. this.summary = summary;
  23. return this;
  24. }
  25. setDescription(description) {
  26. this.description = description;
  27. return this;
  28. }
  29. setRequestBody(requestBody) {
  30. this.requestBody = requestBody;
  31. return this;
  32. }
  33. addParameter(param) {
  34. this.parameters.push(param);
  35. return this;
  36. }
  37. addResponse(res) {
  38. this.responses.push(res);
  39. return this;
  40. }
  41. toAnnotation() {
  42. let annotation = '/**\n';
  43. annotation += ' *\t@swagger\n';
  44. annotation += ` *\t${this.route}:\n`;
  45. annotation += ` *\t\t${this.getMethod()}:\n`;
  46. if (this.summary)
  47. annotation += ` *\t\t\tsummary: ${this.summary}\n`;
  48. if (this.description)
  49. annotation += ` *\t\t\tdescription: ${this.description}\n`;
  50. if (this.parameters.length > 0) {
  51. annotation += ' *\t\t\tparameters:\n';
  52. for (const param of this.parameters) {
  53. annotation += ` *\t\t\t- in: ${param.in}\n`;
  54. annotation += ` *\t\t\t name: ${param.name}\n`;
  55. annotation += ` *\t\t\t required: ${param.required}\n`;
  56. annotation += ` *\t\t\t description: ${param.description}\n`;
  57. if (param.schema) {
  58. annotation += ' *\t\t\t schema:\n';
  59. annotation += this.deserializeSchema(param.schema, 4);
  60. }
  61. }
  62. }
  63. if (this.requestBody) {
  64. annotation += ' *\t\t\trequestBody:\n';
  65. annotation += ` *\t\t\t\trequired: ${this.requestBody.required}\n`;
  66. annotation += ` *\t\t\t\tdescription: ${this.requestBody.description}\n`;
  67. annotation += ' *\t\t\t\tcontent:\n';
  68. annotation += ` *\t\t\t\t\t${this.requestBody.content.mediaType}:\n`;
  69. annotation += ' *\t\t\t\t\t\tschema:\n';
  70. annotation += this.deserializeSchema(this.requestBody.content.schema, 6);
  71. }
  72. if (this.responses.length > 0) {
  73. annotation += ' *\t\t\tresponses:\n';
  74. for (const res of this.responses) {
  75. annotation += ` *\t\t\t\t${res.code}:\n`;
  76. annotation += ` *\t\t\t\t\tdescription: ${res.description}\n`;
  77. if (res.content) {
  78. annotation += ' *\t\t\t\t\tcontent:\n';
  79. annotation += ` *\t\t\t\t\t\t${res.content.mediaType}:\n`;
  80. annotation += ' *\t\t\t\t\t\t\tschema:\n';
  81. annotation += this.deserializeSchema(res.content.schema, 7);
  82. }
  83. }
  84. }
  85. annotation += ' */\n';
  86. return annotation.replace(/\t/g, ' ');
  87. }
  88. getMethod() {
  89. switch (this.method) {
  90. case http_1.HttpMethod.GET:
  91. return 'get';
  92. case http_1.HttpMethod.POST:
  93. return 'post';
  94. default:
  95. return 'undefined';
  96. }
  97. }
  98. deserializeSchema(schema, level) {
  99. let res = '';
  100. const indent = ' *' + this.repeatStr('\t', level + 1);
  101. res += indent + `type: ${schema.type}\n`;
  102. if (schema.description)
  103. res += indent + `description: ${schema.description}\n`;
  104. if (schema.example)
  105. res += indent + `example: ${schema.example}\n`;
  106. if (schema.items && schema.items.length > 0) {
  107. res += indent + 'items:\n';
  108. for (const item of schema.items) {
  109. res += this.deserializeSchema(item, level + 2);
  110. }
  111. }
  112. if (schema.properties && Object.keys(schema.properties).length > 0) {
  113. res += indent + 'properties:\n';
  114. for (const key of Object.keys(schema.properties)) {
  115. res += indent + '\t' + `${key}:\n`;
  116. res += this.deserializeSchema(schema.properties[key], level + 3);
  117. }
  118. }
  119. return res;
  120. }
  121. repeatStr(str, n) {
  122. let res = '';
  123. for (let i = 0; i < n; i++)
  124. res += str;
  125. return res;
  126. }
  127. }
  128. exports.SwaggerDoc = SwaggerDoc;
  129. //# sourceMappingURL=SwaggerDoc.js.map