SwaggerDoc.ts 4.2 KB

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