YAPI Helper

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

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

  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. // ==/UserScript==
  12.  
  13. (function() {
  14. if (!$('#yapi').html()) return
  15. var title = null
  16. var table = null
  17. var url = ''
  18. var clipboard = null
  19. init()
  20. function init () {
  21. setInterval(function () {
  22. if (url !== window.location.href) {
  23. url = window.location.href
  24. title = null
  25. table = null
  26. var t = setInterval(function () {
  27. try {
  28. // 查找"返回数据"节点
  29. if ($('.interface-title') && $('.interface-title').length > 0) {
  30. $.each($('.interface-title'), function () {
  31. if ($(this).text().search('返回数据') > -1) {
  32. $(this).html('返回数据')
  33. title = $(this)
  34. table = $(this).next().find('table')
  35. expandAll(copy)
  36. }
  37. })
  38. } else {
  39. throw(Error('未找到元素'))
  40. }
  41. clearInterval(t)
  42. } catch (e) {
  43. if ((e.message !== '未找到元素')) console.error(e)
  44. }
  45. }, 100)
  46. }
  47. }, 500)
  48. }
  49. function addCopyBtn (text) {
  50. var copy = document.createElement('a')
  51. copy.id = 'copy'
  52. $(copy).attr('id', 'copy')
  53. $(copy).attr('href', '#')
  54. $(copy).attr('data-clipboard-text', text)
  55. $(copy).text('复制数据结构')
  56. title.append('&nbsp;&nbsp;').append($(copy));
  57. if (clipboard) clipboard.destroy()
  58. clipboard = new ClipboardJS(copy)
  59. }
  60. function copy () {
  61. var obj = {}
  62. var parent = obj
  63. var parentLevel = 0
  64. try {
  65. $.each(table.find('.ant-table-row'), function () {
  66. var key = $(this).find('td').eq(0).text()
  67. var type = $(this).find('td').eq(1).text()
  68. var level = parseInt($(this).attr('class').replace('ant-table-row ant-table-row-level-', ''))
  69.  
  70. if (level < parentLevel) parent = obj
  71.  
  72. parent[key] = typeTranslate(type)
  73. if (typeof parent[key] !== 'string') {
  74. parent = parent[key]
  75. }
  76. parentLevel = level
  77. })
  78. } catch (e) {
  79. console.error(e)
  80. }
  81. addCopyBtn(JSON.stringify(obj, replacer).replace(/"/g, '').replace(/,/g, ';'))
  82. }
  83. function expandAll (cb) {
  84. if (table.find('.ant-table-row-collapsed') && table.find('.ant-table-row-collapsed').length > 0) {
  85. $.each(table.find('.ant-table-row-collapsed'), function () {$(this).trigger('click')})
  86. expandAll(cb)
  87. } else {
  88. cb()
  89. }
  90. }
  91. function typeTranslate (type) {
  92. switch (type) {
  93. case 'integer': return 'number'
  94. case 'object []': return {}
  95. default: return type
  96. }
  97. }
  98. function replacer (key, value) {
  99. if (key !== '' && typeof value === 'object') return 'Array<' + JSON.stringify(value, replacer).replace(/"/g, '').replace(/,/g, ';') + '>'
  100. else return value
  101. }
  102. })();