@bwyd/yapi-magic
TypeScript icon, indicating that this package has built-in type declarations

1.1.7 • Public • Published

yapiMagic

一个根据yapi文档自动生成前端api接口代码的工具,提升效率

解决痛点

  • 后端基于yapi写出的mock 前端几乎人肉copy,加大工作量,特别在ts这种强类型项目中,需要手写所有接口的入参出参类型,耗时长
  • 对接口规范要求高,因为代码是自动生成的,所以更要注意接口文档的严谨性

外网安装

npm i @bwyd/yapiMagic -g

安装完成后可以检查下环境中是否有 yapiMagic

配置本机 yapi账号用于自动登录

路径 ~/.yapiUser

{
  "email": "xxxx",
  "password": "xxxx"
}

或者

{
  "_yapi_token": "xxxxx",
  "_yapi_uid": "xxxxx"
}

使用方法

  • 2、 生成 yapiMagic.config.ts 文件配置到项目

yapiMagic init 到当前开发的项目根目录(与package.json平级)运行该命令,如果当前目录已存在yapiMagic.config.ts 则会提示是否覆盖,没有则会创建,具体配置说明:

export interface ServerConfig {
  /**
  * 构建ts 、js版本
  *
  * @example 'ts'
  */
  target: 'ts' | 'js',
  /**
  * YApi 服务地址。
  *
  * @example 'http://yapi.foo.bar'
  */
  serverUrl: string,
  /**
  * 项目id
  *
  * @example 'http://yapi.ywwl.org/project/24/interface/api' projectId 对应 24
  */
  projectId: string,
  /**
  * 1.1.0新增 是否生成 index 文件入口, 默认 false,不建议开启
  */
  generateIndexFile: boolean,
  /** api.d.ts 全局声明的命名空间 会包含所有的接口interface 默认值 YapiTypes */
  apiInterfaceNamespace?: string,
  /**
  * 输出文件路径。
  *
  * 可以是 `相对路径` 或 `绝对路径`。
  *
  * @example 'src/api'
  */
  outputFilePath: string,
  /**
  * 1.1.0 被废除, 请使用customizeFilter
  * include 只包含的 catid
  * exclude 忽略的 catid
  * include exclude 只配置其中之一 也可以都不配置(*)
  */
  catid?: {
    exclude?: string[],
    include?: string[],
  },
  /**
  * 过滤需要比对的文件方法,用于在多迭代并行的项目
  * currentGitBranch 当前git分支号
  */
  customizeFilter?: (api: IOutPut, opt: {
    currentGitBranch: string,
  }) => boolean,
  /**
  * 文件名称生成规则,默认使用path+method 的小驼峰命名,也可以采用id重写
  * @param  {string} path 接口路径 url
  * @param  {string} _id 接口id
  * @param  {string} method 请求方法
  */
  generateApiName?: (path: string, _id: string | number, method: string) => string,
  /**
  * 自定义代码片段函数
  * 不配置的话会有默认代码片段
  */
  generateApiFileCode?: (api: IOutPut) => string,
  /**
  * 更新文件方式
  * 1.1.0 废弃,请使用customizeFilter 自定义
  */
  updateMode: ('ADD' | 'DELETE' | 'CHANGE')[]
}
// generateApiFileCode 方法中 api字段说明
export interface IOutPut {
  /** 生成api 文件名称 */
  name: string,
  /** 接口url */
  path: string,
  method: Method,
  /** 接口名 */
  title: string,
  /** 接口备注 */
  markdown: string,
  /** 分类菜单id */
  catid: number,
  /** 接口ID */
  id: number,
  /** request interface 名称 */
  reqInterfaceName: string,
  /** response interface 名称 */
  resInterfaceName: string,
  requestInterface: string,
  responseInterface: string,
  /**
  * yapi 基础数据源,包含yapi该项接口所有源数据
  */
  yapiBaseInfo: Object,
}

1.示例

  • ts版本
const config = {
  target: 'ts',
  serverUrl: 'https://yapi.com',
  outputFilePath: 'src/api',
  projectId: '48',
  generateApiFileCode: (api) => {
    const arr = [
      `
      /**
      * ${api.title}
      * ${api.markdown || ''}
      **/
      `,
      "import request from '../utils/request'",
      'type Serve<T, G> = (data?: T) => Promise<G>',
      api.requestInterface,
      api.responseInterface,
      `const http: Serve<${api.reqInterfaceName}, ${api.resInterfaceName}['data'] > = (data?) =>  request({
        method: '${api.method}',
        url: '${api.path}',
        data: ${(() => {
          if (api.method.toLocaleLowerCase() === 'get') {
            return 'params: data'
          } else {
            return 'data'
          }
        })()}
      }) `,
      `export default http`,
    ]
    return arr.join(`
    `)
  }
}

module.exports = config
  • js版本
const config = {
  target: 'js',
  serverUrl: 'https://yapi.weierai.com',
  outputFilePath: 'src/api',
  projectId: '48',
  // 不生成 updateJson
  generateUpdateJson: false,
  // 不生成 index文件
  generateIndexFile: false,
  customizeFilter: (api, branchName) => {
    // 采用 git 分支号做多版本并行的标识
    // 你也可以自定义 你需要的规则来判断你需要生成的接口
    const { tag } = api.yapiBaseInfo
    if (tag.includes(branchName)) {
      console.log(api.id)
    }
    return tag.includes(branchName)
  },
  generateApiFileCode: (api) => {
    const arr = [
      `
      /**
      * ${api.title}
      * ${api.markdown || ''}
      **/
      `,
      "import request from '@/utils/request.js'",

      `export default (data = {}) => request({
        method: '${api.method}',
        url: '${api.path}',
        ${(() => {
          if (api.method.toLocaleLowerCase() === 'get') {
            return 'params: data,'
          } else {
            return 'data'
          }
        })()}
      })`,
    ]
    return arr.join(`
    `)
  }
}

module.exports = config

一般来说 generateApiFileCode 方法需要自己实现一下,组装拼接出符合自己期望的 接口代码格式

  • 3、生成代码 yapiMagic 运行该命令 会根据步骤2的配置文件,生产出api(outputFilePath)文件夹,该文件夹下index.ts作为所有接口的导出口,供项目中导入使用
  • config 可以是数组形式(1.0.7之后版本支持),生成多个project的yapi接口到你的项目,值得注意的是,每一个项目都应该是独有的outputPath,模板片段也不应该设置相同(访问不同域名)
  • 4、查看接口变动日志yapiMagic changelog

  • 5、查看版本号 yapiMagic version

Package Sidebar

Install

npm i @bwyd/yapi-magic

Weekly Downloads

1

Version

1.1.7

License

MIT

Unpacked Size

98.7 kB

Total Files

27

Last publish

Collaborators

  • venus_jin