SwaggerDoc.ts 4.7 KB

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