swagger-umi-request-generate

根据swagger生成umi request请求模板代码

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         swagger-umi-request-generate
// @namespace
// @version      2.1
// @description  根据swagger生成umi request请求模板代码
// @author       leesher
// @match        http://*/swagger-ui.html*
// @grant        GM_addStyle
// @run-at       document-end
// @require     https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js
// @require     https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.js
// @require     https://cdn.bootcdn.net/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.js
// @resource    customCSS https://cdn.bootcdn.net/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.css
// @grant       GM_addStyle
// @grant       GM_getResourceText

// @namespace lee
// ==/UserScript==

(function() {
    'use strict'

    console.log('油猴启动')
    var newCSS = GM_getResourceText ("customCSS");
    GM_addStyle (newCSS);

    new ClipboardJS('.copy-btn')

    function createATag(text, content) {
        const temp = $(`<button style="margin-right:50px;margin-bottom:5px" class="btn copy-btn" data-clipboard-text="${content}">${text}</button>`)

        temp.on('click', function(event) {

            event.preventDefault();
            $.toast({
                heading: '复制成功',
                position: 'mid-center',
                stack: false,
                hideAfter:200,
                icon:"success"
            })

        })

        return temp
    }

    function toHump(name) {
        return name.replace(/\-(\w)/g, function(all, letter){
            return letter.toUpperCase();
        });
    }

    function createFunc(methoud, path, description) {
        const methoudName = toHump(path.match('[^/]+(?!.*/)')[0])
        switch (methoud.toUpperCase()) {
            case 'POST':
                return `// ${description}
              export function ${methoudName}(data:DataType){
                  return request<ResType>('${path}',{
                    method: 'post',
                    data
                  })
                }`

      case 'GET':
                return `// ${description}
              export function ${methoudName}(params:paramType){
                  return request<ResType>('${path}',{
                    method: 'get',
                    params
                  })
                }`

      default:
                return '暂不支持当前请求格式'
        }
    }

    $('body')
        .delegate('.opblock-tag', 'click', function(event) {
        console.log($(this).next())
        setTimeout(() => {
            $(this).next().children('span').each((index, $el) => {
                console.log($el)
                $el = $($el)

                const methoud = $($el).find('.opblock-summary-method').text()
                const path = $($el).find('.opblock-summary-path > a > span').text()
                const description = $($el).find('.opblock-summary-description').text()

                $el.before($('<div></div>')
                           .append(createATag('复制URL', path))
                           .append(createATag('复制函数', createFunc(methoud, path, description)))
                          )
            })
        })
    })
})()