SwaggerDoc.ts 4.4 KB

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