SwaggerDoc.js 5.0 KB

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