YAPI Helper

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

当前为 2021-05-15 提交的版本,查看 最新版本

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