0.3.5 • Published 4 years ago

ys-codegen-trans-param v0.3.5

Weekly downloads
44
License
MIT
Repository
-
Last release
4 years ago

ys-codegen-trans-param

代码生成之将yapi文档接口转代码生成的参数结构的工具

安装

使用 npm:

$ npm install -D ys-codegen-trans-param

使用 yarn:

$ yarn add -D ys-codegen-trans-param

更新:

$ yarn upgrade ys-codegen-trans-param --latest

使用

import { IndexParams,YApi } from 'ys-codegen-trans-param'
const engine = new YApi({ token: '9a444ebafbea360eab663677865aacc949c156a67b1d9f7152b42541b3840a33' })
const params = await engine.getInterface(18047)
if (false !== params) {
    const sdk = new IndexParams()
    let res = sdk.trans(params)
    sdk.outFile(__dirname + '/json/test.json')
    console.log('res', res)
} else {
    console.log('error', engine.getError())
}

// 修改默认json文件 模板 字符串方式
const sdk = new IndexParams({
  tpl:`{
            /* 列表表格配置 */
            "indexTable": {
                /* 列表表格数据 */
                "data": {
                    "columns": [<%- tableColumns %>]
                },
                /* 列表查询接口地址 */
                "apiQuery": "<%- apiQuery %>",
                /* 列表表格删除按钮接口地址 */
                "apiDelete": "<%- apiDelete %>",
                /* 新增的参数 */
                "apiDelete2": "<%- apiDelete %>",
                /* 列表html配置 */
                "_config": {
                    /* 列表表格标题 */
                    "title": "<%- indexTableTitle %>"
                }
            },
            /* 搜索区配置 */
            "searchForm": {
                /* 搜索区html配置 */
                "_config": {
                    "query": [<%- searchQuery %>]
                }
            }
        }`
})

// UTF8文件方式
const sdk = new IndexParams({
  tplPath:"./tpl.json"
})

转换

  • IndexParams 转换yapi query接口为列表页模板参数
    // 默认tpl
    tpl = `{
            /* 表单配置 */
            "block": {
                /* 搜索区配置 */
                "searchForm": {
                    type: "search",
                    render: "",
                    ref: "searchForm",
                    defaultValue: {},
                    jsonData: {
                        "list": [
                            <%- searchQuery %>
                        ],
                        "config": {
                            "layout": "inline",
                            "labelCol": {
                                "xs": 4,
                                "sm": 4,
                                "md": 4,
                                "lg": 4,
                                "xl": 4,
                                "xxl": 4
                            },
                            "wrapperCol": {
                                "xs": 20,
                                "sm": 20,
                                "md": 20,
                                "lg": 20,
                                "xl": 20,
                                "xxl": 20
                            },
                            "hideRequiredMark": false,
                            "customStyle": ""
                        }
                    }
                },
                /* 列表表格配置 */
                "indexTable": {
                    type: "list",
                    /* 列表表格数据 */
                    "data": {
                        "columns": [<%- tableColumns %>]
                    },
                    "apiQueryUrl": "<%- apiQuery %>", /* 列表查询接口地址 */
                    "apiDeleteUrl": "<%- apiDelete %>", /* 列表表格删除按钮接口地址 */
                    /* 列表html配置 */
                    "title": "<%- indexTableTitle %>" /* 列表表格标题 */
                },
            },
            title: "<%- indexTableTitle %>" /* 页面标题 */
        }`
  • FormParams 转换yapi read、update、save接口为表单页模板参数

    // 默认tpl
    tpl = `{
            /* 表单配置 */
            "block": {
                "editForm": {
                    apiSaveUrl: "<%- apiSave %>" /* 表单保存按钮接口地址 */,
                    apiUpdateUrl: "<%- apiUpdate %>" /* 表单更新按钮接口地址 */,
                    apiReadUrl: "<%- apiRead %>" /* 表单数据读取接口地址 */,
                    type: "form",
                    render: "k-form-design",
                    ref: "editForm",
                    defaultValue: {},
                    jsonData: {
                        "list": [
                            <%- list %>
                        ],
                        "config": {
                            "layout": "horizontal",
                            "labelCol": {
                                "xs": 4,
                                "sm": 4,
                                "md": 4,
                                "lg": 4,
                                "xl": 4,
                                "xxl": 4
                            },
                            "wrapperCol": {
                                "xs": 20,
                                "sm": 20,
                                "md": 20,
                                "lg": 20,
                                "xl": 20,
                                "xxl": 20
                            },
                            "hideRequiredMark": false,
                            "customStyle": ""
                        }
                    },
                }
            },
            /* 表单HTML配置 */
            "title": "<%- title %>" /* 表单区域标题显示内容 */
        }`

结构

引擎转化出来的结构

{
	"path": "/admin/SystemConfig/save",
	"post": [{
		"index": "title",
		"title": "配置标题",
		"type": "string",
		"rules": [{
			"max": 100,
			"message": "最大长度: 100"
		}],
		"items": []
	}, {
		"index": "type",
		"title": "配置类型",
		"type": "integer",
		"rules": [],
		"items": []
	}, {
		"index": "status",
		"title": "状态",
		"type": "integer",
		"rules": [],
		"items": [{
			"key": "1",
			"value": "有效"
		}, {
			"key": "0",
			"value": "无效"
		}]
	...
	}],
	"data": [
		// 结构同post
	],
	"query": [{
		"required": false,
		"name": "pageNo",
		"title": "页码",
		"items": []
	}, {
		"required": false,
		"name": "pageSize",
		"title": "每页条数",
		"items": []
	}, {
		"required": false,
		"name": "text",
		"title": "字典名称",
		"items": []
	}, {
		"required": false,
		"name": "code",
		"title": "字典代码",
		"items": []
	}],
	"method": "POST",
	"title": "系统配置新增"
}

模板参数

IndexParams 列表模板参数结构

{
  /* 表单配置 */
  "block": {
    /* 搜索区配置 */
    "searchForm": {
      "type": "search",
      "render": "",
      "ref": "searchForm",
      "defaultValue": {},
      "jsonData": {
        "list": [
          {
            "type": "input" /* 表单控件类型 */,
            "label": "配置标题" /* 表单控件显示名称 */,
            "icon": "icon-write",
            "options": {
              "type": "text",
              "width": "100%",
              "defaultValue": "",
              "placeholder": "请选择配置标题",
              "clearable": false,
              "maxLength": null,
              "hidden": false,
              "disabled": false
            },
            "model": "title" /* 表单控件name值 */,
            "key": "input_16117146443281",
            /* 表单控件数据校验规则 https://www.antdv.com/components/form-cn/ 更多高级用法可研究 https://github.com/yiminghe/async-validator */
            "rules": [
              //{
              // "required": false, /* boolean,默认false, 是否必选 */
              //"message": "最大长度: 100", /* string, 校验文案 */
              //"max": 100, /* number, 最大长度 */
              // "pattern": "", /* RegExp,正则表达式校验 */
              // "whitespace": boolean, /* boolean,默认false, 必选时,空格是否会被视为错误 */
              // "len": 120, /* number, 字段长度 */
              // "min": 0, /* number, 最小长度 */
              // "type": "string", /* string, 内建校验类型(string、number、integer、boolean、float、email、any、hex、array、url、object、date、enum、method、regexp,参考 https://github.com/yiminghe/async-validator#type) */
              // "transform": "", /* function(value) => transformedValue:any, 校验前转换字段值	这里填写函数名 */
              // "validator": "", /* function(rule, value, callback), 自定义校验(注意,callback 必须被调用)这里填写函数名 */
              // "enum": "", /* string, 枚举类型 */
              //},
            ]
          },
        ],
        "config": {
          "layout": "inline",
          "labelCol": {
            "xs": 4,
            "sm": 4,
            "md": 4,
            "lg": 4,
            "xl": 4,
            "xxl": 4
          },
          "wrapperCol": {
            "xs": 20,
            "sm": 20,
            "md": 20,
            "lg": 20,
            "xl": 20,
            "xxl": 20
          },
          "hideRequiredMark": false,
          "customStyle": ""
        }
      }
    },
    /* 列表表格配置 */
    "indexTable": {
      "type": "list",
      /* 列表表格数据 */
      "data": {
        "columns": [
          {
            "title": "id" /* 列表列头名称 */,
            "index": "id" /* 列表字段名称 */,
            "type": "integer" /* 列表字段类型 */
          },
          {
            "title": "配置标题" /* 列表列头名称 */,
            "index": "title" /* 列表字段名称 */,
            "type": "string" /* 列表字段类型 */
          },
          {
            "title": "配置名称" /* 列表列头名称 */,
            "index": "name" /* 列表字段名称 */,
            "type": "string" /* 列表字段类型 */
          },
        ]
      },
      "apiQueryUrl": "/admin/SystemConfig/query" /* 列表查询接口地址 */,
      "apiDeleteUrl": "/admin/SystemConfig/delete" /* 列表表格删除按钮接口地址 */,
      /* 列表html配置 */
      "title": "系统配置列表" /* 列表表格标题 */
    }
  },
  "title": "系统配置列表" /* 页面标题 */
}

FormParams 表单模板参数结构

{
  /* 表单配置 */
  "block": {
    "editForm": {
      "apiSaveUrl": "/admin/SystemConfig/save" /* 表单保存按钮接口地址 */,
      "apiUpdateUrl": "/admin/SystemConfig/update" /* 表单更新按钮接口地址 */,
      "apiReadUrl": "/admin/SystemConfig/read" /* 表单数据读取接口地址 */,
      "type": "form",
      "render": "k-form-design",
      "ref": "editForm",
      "defaultValue": {},
      "jsonData": {
        "list": [
          {
            "type": "number" /* 表单控件类型 */,
            "label": "id" /* 表单控件显示名称 */,
            "icon": "icon-number",
            "options": {
              "width": "100%",
              "defaultValue": 0,
              "min": null,
              "max": null,
              "precision": null,
              "step": 1,
              "hidden": false,
              "disabled": false,
              "placeholder": "请选择id"
            },
            "model": "id" /* 表单控件name值 */,
            "key": "number_161171464460015",
            /* 表单控件数据校验规则 https://www.antdv.com/components/form-cn/ 更多高级用法可研究 https://github.com/yiminghe/async-validator */
            "rules": [
              //{
              // "required": false, /* boolean,默认false, 是否必选 */
              //"message": "最大长度: 100", /* string, 校验文案 */
              //"max": 100, /* number, 最大长度 */
              // "pattern": "", /* RegExp,正则表达式校验 */
              // "whitespace": boolean, /* boolean,默认false, 必选时,空格是否会被视为错误 */
              // "len": 120, /* number, 字段长度 */
              // "min": 0, /* number, 最小长度 */
              // "type": "string", /* string, 内建校验类型(string、number、integer、boolean、float、email、any、hex、array、url、object、date、enum、method、regexp,参考 https://github.com/yiminghe/async-validator#type) */
              // "transform": "", /* function(value) => transformedValue:any, 校验前转换字段值	这里填写函数名 */
              // "validator": "", /* function(rule, value, callback), 自定义校验(注意,callback 必须被调用)这里填写函数名 */
              // "enum": "", /* string, 枚举类型 */
              //},
            ]
          },
        ],
        "config": {
          "layout": "horizontal",
          "labelCol": {
            "xs": 4,
            "sm": 4,
            "md": 4,
            "lg": 4,
            "xl": 4,
            "xxl": 4
          },
          "wrapperCol": {
            "xs": 20,
            "sm": 20,
            "md": 20,
            "lg": 20,
            "xl": 20,
            "xxl": 20
          },
          "hideRequiredMark": false,
          "customStyle": ""
        }
      }
    }
  },
  /* 表单HTML配置 */
  "title": "系统配置获取详情" /* 表单区域标题显示内容 */
}
0.3.5

4 years ago

0.3.4

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.2.0

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

5 years ago