YAPI Helper

将YAPI的接口返回数据结构复制为typescript的interface类型

  1. // ==UserScript==
  2. // @name YAPI Helper
  3. // @namespace https://greasyfork.org/zh-CN/scripts/426512
  4. // @version 0.4
  5. // @description 将YAPI的接口返回数据结构复制为typescript的interface类型
  6. // @author zhenhappy<q505507538@gmail.com>
  7. // @include http://*
  8. // @include https://*
  9. // @require https://unpkg.com/jquery/dist/jquery.slim.min.js
  10. // @require https://unpkg.com/clipboard/dist/clipboard.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. if (!$('#yapi').html()) return
  16. var title = null
  17. var table = null
  18. var url = ''
  19. var clipboard = null
  20. init()
  21. function init () {
  22. setInterval(function () {
  23. if (url !== window.location.href) {
  24. url = window.location.href
  25. title = null
  26. table = null
  27. var t = setInterval(function () {
  28. try {
  29. // 查找"返回数据"节点
  30. if ($('.interface-title') && $('.interface-title').length > 0) {
  31. $.each($('.interface-title'), function () {
  32. if ($(this).text().search('返回数据') > -1) {
  33. $(this).html('返回数据')
  34. title = $(this)
  35. table = $(this).next().find('table')
  36. expandAll(copy)
  37. }
  38. })
  39. } else {
  40. throw(Error('未找到元素'))
  41. }
  42. clearInterval(t)
  43. } catch (e) {
  44. if ((e.message !== '未找到元素')) console.error(e)
  45. }
  46. }, 100)
  47. }
  48. }, 500)
  49. }
  50. function addCopyBtn (text) {
  51. var copy = document.createElement('a')
  52. copy.id = 'copy'
  53. $(copy).attr('id', 'copy')
  54. $(copy).attr('href', '#')
  55. $(copy).attr('data-clipboard-text', text)
  56. $(copy).text('复制数据结构')
  57. title.append('&nbsp;&nbsp;').append($(copy));
  58. if (clipboard) clipboard.destroy()
  59. clipboard = new ClipboardJS(copy)
  60. }
  61. function copy () {
  62. var obj = {}
  63. var parent = obj
  64. var parentLevel = 0
  65. try {
  66. $.each(table.find('.ant-table-row'), function () {
  67. var key = $(this).find('td').eq(0).text()
  68. var type = $(this).find('td').eq(1).text()
  69. var level = parseInt($(this).attr('class').replace('ant-table-row ant-table-row-level-', ''))
  70.  
  71. if (level < parentLevel) parent = obj
  72.  
  73. parent[key] = typeTranslate(type)
  74. if (typeof parent[key] !== 'string') {
  75. parent = parent[key]
  76. }
  77. parentLevel = level
  78. })
  79. } catch (e) {
  80. console.error(e)
  81. }
  82. addCopyBtn(JSON.stringify(obj, replacer).replace(/"/g, '').replace(/,/g, ';'))
  83. }
  84. function expandAll (cb) {
  85. if (table.find('.ant-table-row-collapsed') && table.find('.ant-table-row-collapsed').length > 0) {
  86. $.each(table.find('.ant-table-row-collapsed'), function () {$(this).trigger('click')})
  87. expandAll(cb)
  88. } else {
  89. cb()
  90. }
  91. }
  92. function typeTranslate (type) {
  93. switch (type) {
  94. case 'integer': return 'number'
  95. case 'object []': return {}
  96. default: return type
  97. }
  98. }
  99. function replacer (key, value) {
  100. if (key !== '' && typeof value === 'object') return 'Array<' + JSON.stringify(value, replacer).replace(/"/g, '').replace(/,/g, ';') + '>'
  101. else return value
  102. }
  103. })();