SwaggerDoc.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\tdescription: ${this.requestBody.description}\n`;
  66. annotation += ' *\t\t\t\tcontent:\n';
  67. annotation += ` *\t\t\t\t\t${this.requestBody.content.mediaType}:\n`;
  68. annotation += ' *\t\t\t\t\t\tschema:\n';
  69. annotation += this.deserializeSchema(this.requestBody.content.schema, 6);
  70. }
  71. if (this.responses.length > 0) {
  72. annotation += ' *\t\t\tresponses:\n';
  73. for (const res of this.responses) {
  74. annotation += ` *\t\t\t\t${res.code}:\n`;
  75. annotation += ` *\t\t\t\t\tdescription: ${res.description}\n`;
  76. if (res.content) {
  77. annotation += ' *\t\t\t\t\tcontent:\n';
  78. annotation += ` *\t\t\t\t\t\t${res.content.mediaType}:\n`;
  79. annotation += ' *\t\t\t\t\t\t\tschema:\n';
  80. annotation += this.deserializeSchema(res.content.schema, 7);
  81. }
  82. }
  83. }
  84. annotation += ' */\n';
  85. return annotation.replace(/\t/g, ' ');
  86. }
  87. getMethod() {
  88. switch (this.method) {
  89. case http_1.HttpMethod.GET:
  90. return 'get';
  91. case http_1.HttpMethod.POST:
  92. return 'post';
  93. default:
  94. return 'undefined';
  95. }
  96. }
  97. deserializeSchema(schema, level) {
  98. let res = '';
  99. const indent = ' *' + this.repeatStr('\t', level + 1);
  100. res += indent + `type: ${schema.type}\n`;
  101. if (schema.items && schema.items.length > 0) {
  102. res += indent + 'items:\n';
  103. for (const item of schema.items) {
  104. res += this.deserializeSchema(item, level + 2);
  105. }
  106. }
  107. if (schema.properties && Object.keys(schema.properties).length > 0) {
  108. res += indent + 'properties:\n';
  109. for (const key of Object.keys(schema.properties)) {
  110. res += indent + '\t' + `${key}:\n`;
  111. res += this.deserializeSchema(schema.properties[key], level + 3);
  112. }
  113. }
  114. return res;
  115. }
  116. repeatStr(str, n) {
  117. let res = '';
  118. for (let i = 0; i < n; i++)
  119. res += str;
  120. return res;
  121. }
  122. }
  123. exports.SwaggerDoc = SwaggerDoc;
  124. //# sourceMappingURL=SwaggerDoc.js.map