@yzfe/gen-api

1.6.0 • Public • Published

yz-gen-api 根据 swagger 文档,生成 api 代码

根据 swagger API v2 生成接口代码

安装

yarn add @yzfe/gen-api -D
yarn add axios 

使用

npx yz-gen-api build https://petstore.swagger.io/v2/swagger.json -o src/api

命令

npx yz-gen-api build -h
yz-gen-api build <swaggerUrl>

根据 swagger 文档,生成 api 代码

选项:
  --version, -v  显示版本号                                               [布尔]
  --help, -h                                                              [布尔]
  --outPath, -o  生成目录                                                 [必需]

Vue 最佳实现

修改 api 入口文件 index.ts

import * as http from './http'
import client from './client'
import Vue from 'vue'

const api = {
    http,
    defs: client.defs,
    modules: client.modules
}


declare module 'vue/types/vue' {
    interface Vue {
        $api: typeof api
    }
}

export function install(vue: typeof Vue) {
    Object.defineProperty(vue.prototype, '$api', {
        get() {
            return api
        }
    })
}

export default api

Api request / response 值初始化

export default class Login extends Vue {
    protected loginReq: defs.AuthenticationAuthbasicReq = new this.$api.defs.AuthenticationAuthbasicReq()
}

Readme

Keywords

none

Package Sidebar

Install

npm i @yzfe/gen-api

Weekly Downloads

0

Version

1.6.0

License

MIT

Unpacked Size

22.6 kB

Total Files

8

Last publish

Collaborators

  • allenice
  • allenmo
  • vfasky