npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ywfe/yfeapi2ts

v1.1.1

Published

根据yApi接口文档,生成接口请求代码。支持生成TS & JavaScript语法

Downloads

2

Readme

YFEApi2TS

简介

根据YApi接口文档,自动生成前端API接口代码的工具,降低前端开发工作量,减少重复工作,提升开发效率。

name node npm download

解决问题

  • 后端在yApi上创建接口文档,前端几乎人肉复制,工作量巨大,特别在 TS 这种强类型项目中,需要手写所有接口的入参出参类型,工作量大,耗时长,收益又很低。
  • 严格控制接口规范。因为代码是自动生成的,所以,更要注意接口文档的规范、严谨。

安装

npm i -g @ywfe/yfeapi2ts

安装完成后,检查是否安装成功。

yfeapi2ts version

使用

生成配置文件

使用 yfeapi2ts init [ts|js] 生成 yfeapi2ts.config.ts 配置文件

yfeapi2ts init [ts|js]

参数说明: ts or js 指定生成的代码是 TypeScript 还是 JavaScript。不指定默认生成 TypeScript。

在项目根目录中(与 package.json 同级)运行该命令,如果当前目录已存在 yfeapi2ts.config.ts 则会提示是否覆盖,没有则会创建.

配置项说明:

  export interface ServerConfig {
   /**
   * 构建目标:ts or js
   * @example 'ts'
   */
  target: 'ts' | 'js',
  /**
   * YApi 服务地址。
   * @example 'http://yapi.ywfe.com'
   */
  serverUrl: string,
  /**
   * 项目id
   * @example 'http://yapi.ywfe.com/project/24/interface/api' projectId 对应 24
   */
  projectId: string,
  /** cookie _yapi_token */
  _yapi_token: string,
  /** cookie _yapi_uid */
  _yapi_uid: string,
  /**是否自动开启changelog视图, 默认false */
  changelog?: boolean,
  /**
   * 输出文件路径。可以是 `相对路径` 或 `绝对路径`。
   * @example 'src/ywapi/'
   */
  outputFilePath: string,
  /**
   * 菜单配置
   * include 只包含的 catid
   * exclude 忽略的 catid
   * include exclude 是互斥的 只配置其中之一 也可以都不配置(*)
   */
  catid?: {
    exclude?: string[],
    include?: string[],
  },
  /**
   * @param  {string} path url
   * @param  {string} id 接口唯一id
   */
  generateApiName: (path: string, id: string) => string,
  /**
   * 自定义代码片段函数
   * 不配置的话会有默认代码片段
   */
  generateApiFileCode?: (api: IOutPut) => string,
}
// 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,
}

示例

  • TS版本*
const config = {
  target: 'ts',
  serverUrl: 'http://yapi.ywfe.com',
  outputFilePath: 'src/api',
  projectId: '48',
  _yapi_uid: '18',
  _yapi_token: 'userToken',
  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(`
    `)
  }
}

export default config
  • Js版本
const config = {
  target: 'js',
  serverUrl: 'http://yapi.ywfe.com',
  outputFilePath: 'src/api',
  projectId: '48',
  _yapi_uid: '18',
  _yapi_token: 'userToken',
  catid: {
    exclude: ['37']
  },
  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(`
    `)
  }
}

export default config

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

如果 接口地址为 RESTFul 风格的需要解析地址。可使用如下方法:

// 转换RESTful路径
const converRESTfullPath = (path) => {
  let t = path.split('/')
  let tLen = t.length

  t.forEach((val, index) => {
    t[index] = val.replace(new RegExp('{', 'g'), '"+').replace(new RegExp('}', 'g'), '+"')
  })
  if (t[tLen - 1].lastIndexOf('"') === t[tLen - 1].length - 1) {
    t[tLen - 1] = t[tLen - 1].replace('+"', '')
  }
  if ((t[tLen - 2].indexOf('+') !== -1) && t[tLen - 1].indexOf('+') === -1) {
    t[tLen - 1] = t[tLen - 1] + '"'
  }
  return t.join('/').replace(new RegExp('"', 'g'), "'")
}

将请求函数中 url 配置项值 改为:

  url: '${converRESTfullPath(api.path)},

生成代码

yfeapi2ts

运行该命令 会根据步骤2的配置文件,生产出api(outputFilePath)文件夹,该文件夹下index.ts作为所有接口的导出口,供项目中导入使用

查看接口变动日志

yfeapi2ts changelog

查看版本号

yfeapi2ts version