raml2swagger.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. const converter = require('oas-raml-converter');
  2. const fs = require('fs');
  3. const raml10ToOas20 = new converter.Converter(
  4. converter.Formats.RAML,
  5. converter.Formats.OAS20
  6. );
  7. const tagsMap = {
  8. '/auth': 'auth-权限',
  9. '/commit': 'commit-提交',
  10. '/course': 'course-课程',
  11. '/exam': 'exam-考试',
  12. '/problem': 'problem-题目',
  13. '/record': 'record-记录',
  14. '/result': 'result-结果',
  15. '/user': 'user-用户',
  16. '/statistic': 'statistic-分析'
  17. };
  18. raml10ToOas20
  19. .convertFile('./index.raml')
  20. .then(function(json) {
  21. // 接口名称
  22. json = json.replace(/operationId/g, 'summary');
  23. json = JSON.parse(json);
  24. // tags支持
  25. tags = Object.values(tagsMap).map(v => ({ name: v, description: v }));
  26. json['tags'] = tags;
  27. Object.keys(json.paths).forEach(uri => {
  28. uriTags = Object.keys(tagsMap)
  29. .filter(k => uri.startsWith(k))
  30. .map(k => tagsMap[k]);
  31. Object.keys(json.paths[uri]).forEach(method => {
  32. json.paths[uri][method]['tags'] = uriTags;
  33. });
  34. });
  35. fs.writeFileSync('./swagger.json', JSON.stringify(json, null, ' '));
  36. })
  37. .catch(function(err) {
  38. console.error(err);
  39. });