YAPI Helper

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YAPI Helper
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  将YAPI的接口返回数据结构复制为typescript的interface类型
// @author       zhenhappy<[email protected]>
// @match        http://gate.97kid.com:8004/*
// @require      https://unpkg.com/jquery/dist/jquery.slim.min.js
// @require      https://unpkg.com/clipboard/dist/clipboard.min.js
// ==/UserScript==

(function() {
    var title = null
    var table = null
    var t = setInterval(function () {
        try {
            // 查找"返回数据"节点
            if ($('.interface-title') && $('.interface-title').length > 0) {
                $.each($('.interface-title'), function () {
                    if ($(this).text() === '返回数据') {
                        console.log('找到: "' + $(this).text() + '"')
                        title = $(this)
                        table = $(this).next().find('table')
                        expandAll(copy)
                    }
                })
            } else {
                throw(Error('未找到元素'))
            }
            clearInterval(t)
        } catch (e) {
            if ((e.message !== '未找到元素')) console.error(e)
        }
    }, 500)

    function addCopyBtn (text) {
        title.append(' <a id="copy" href="#" data-clipboard-text=\''+ text +'\'>复制为interface</a>');
        new ClipboardJS('#copy')
    }
    function copy () {
        var obj = {}
        var parent = obj
        var parentLevel = 0
        try {
            $.each(table.find('.ant-table-row'), function () {
                var key = $(this).find('td').eq(0).text()
                var type = $(this).find('td').eq(1).text()
                var level = parseInt($(this).attr('class').replace('ant-table-row  ant-table-row-level-', ''))

                if (level < parentLevel) parent = obj

                parent[key] = typeTranslate(type)
                if (typeof parent[key] !== 'string') {
                    parent = parent[key]
                }
                parentLevel = level
            })
        } catch (e) {
            console.error(e)
        }
        addCopyBtn(JSON.stringify(obj, replacer).replace(/"/g, '').replace(/,/g, ';'))
    }
    function expandAll (cb) {
        if (table.find('.ant-table-row-collapsed') && table.find('.ant-table-row-collapsed').length > 0) {
            $.each(table.find('.ant-table-row-collapsed'), function () {$(this).trigger('click')})
            expandAll(cb)
        } else {
            cb()
        }
    }
    function typeTranslate (type) {
        switch (type) {
            case 'integer': return 'number'
            case 'object []': return {}
            default: return type
        }
    }
    function replacer (key, value) {
        if (key !== '' && typeof value === 'object') return 'Array<' + JSON.stringify(value, replacer).replace(/"/g, '').replace(/,/g, ';') + '>'
        else return value
    }
})();