1.0.0-alpha.2 • Published 2 years ago

@yzfe/openapi-generator v1.0.0-alpha.2

Weekly downloads
1
License
MIT
Repository
-
Last release
2 years ago

openapi-generator

根据 openapi 规范的文档,生成 typescript 代码

安装

yarn add @yzfe/openapi-generator --dev

使用

yz-oapi-gen -c ./config-file.js -o ./output-path

api config

api 配置文件是 js 文件,export 配置数组

module.exports = [
    {
        // 模块名称
        name: 'custom-forms',
        // 接口文档地址
        api: 'https://yzt-dev.yzone.co/custom-forms/v2/api-docs',
        // 接口地址前缀
        apiPrefix: '/custom-forms',
        // 生成的 typings 的名字空间
        namespace: 'ApiCustomForms',
        // 自定义生成的接口方法名,对应接口地址的,不包含前缀部分
        customFunctionName: {
            '/form/data/{id}': 'getDataById',
            '/form/data/list': 'getDataList'
        }
    }
]

使用生成后的代码

默认生成的代码的请求库是使用 @yzfe/request 的,请先阅读其文档,一般需要配置拦截器才能使用的。

import { moduleName } from '生成的目标目录'

moduleName.functionName(datas)

实际例子:

yz-oapi-gen -c ./config-file.js -o ./src/api
import { customForms } from '@/api'

async function() {
    let res = await customForms.form.getDataList({
        memberId: this.memberId,
        page: this.pageInfo.currentPage,
        size: this.pageInfo.pageSize,
        from: this.beginDate,
        to: this.endDate
    })
    console.log(res)
}