# ywapi2ts

> yapi 输出到前端项目中api ts & js

Latest version **1.0.1** (published 2019-04-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install ywapi2ts
pnpm add ywapi2ts
yarn add ywapi2ts
bun add ywapi2ts
```

Provides the command `ywapi2ts`.

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2019-04-30 |
| First published | 2019-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 24 |
| Unpacked size | 83.4 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | venus_jin |
| Maintainers | venus_jin |
| Keywords | ywapi2ts, yapi, typescript, typing |

## Links

- npm: https://www.npmjs.com/package/ywapi2ts
- npm.io page: https://npm.io/package/ywapi2ts

## Dependencies (24)

- [opn](https://npm.io/package/opn.md) ^6.0.0
- [ora](https://npm.io/package/ora.md) ^3.4.0
- [open](https://npm.io/package/open.md) ^6.2.0
- [json5](https://npm.io/package/json5.md) ^2.1.0
- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [vtils](https://npm.io/package/vtils.md) ^0.76.1
- [mockjs](https://npm.io/package/mockjs.md) ^1.0.1-beta3
- [consola](https://npm.io/package/consola.md) ^2.3.0
- [express](https://npm.io/package/express.md) ^4.16.4
- [json-ts](https://npm.io/package/json-ts.md) ^1.6.4
- [prompts](https://npm.io/package/prompts.md) ^2.0.4
- [request](https://npm.io/package/request.md) ^2.88.0
- [ts-node](https://npm.io/package/ts-node.md) ^8.0.2
- [anywhere](https://npm.io/package/anywhere.md) ^1.5.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^7.0.1
- [commander](https://npm.io/package/commander.md) ^2.19.0
- [@types/opn](https://npm.io/package/@types/opn.md) ^5.5.0
- [@types/open](https://npm.io/package/@types/open.md) ^6.1.0
- [change-case](https://npm.io/package/change-case.md) ^3.0.2
- [@types/express](https://npm.io/package/@types/express.md) ^4.16.1
- [yapi-to-typescript](https://npm.io/package/yapi-to-typescript.md) ^1.4.0
- [json-schema-generator](https://npm.io/package/json-schema-generator.md) ^2.0.6
- [request-promise-native](https://npm.io/package/request-promise-native.md) ^1.0.5
- [json-schema-to-typescript](https://npm.io/package/json-schema-to-typescript.md) ^6.1.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2019-04-30
- 1.0.0 — 2019-04-29

## README

# ywapi2ts

##### 一个根据yapi文档自动生成前端api接口代码的工具，提升效率

### 解决痛点
  * 后端基于yapi写出的mock 前端几乎人肉copy，加大工作量，特别在ts这种强类型项目中，需要手写所有接口的入参出参类型，耗时长
  * 对接口规范要求高，因为代码是自动生成的，所以更要注意接口文档的严谨性

## 使用方法
 - 1、 全局安装 ywapi2ts 到本机

  `npm i ywapi2ts -g`

 - 2、 生成 ywapi2ts.config.ts 文件配置到项目

  `ywapi2ts init`
  到当前开发的项目根目录（与package.json平级）运行该命令，如果当前目录已存在`ywapi2ts.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,
  /** cookie _yapi_token */
  _yapi_token: string,
  /** cookie _yapi_uid */
  _yapi_uid: string,
  /**
   * 输出文件路径。
   *
   * 可以是 `相对路径` 或 `绝对路径`。
   *
   * @example 'src/ywapi/'
   */
  outputFilePath: string,
  /**
   * 自定义代码片段函数
   * 不配置的话会有默认代码片段
   */
  generateApiFileCode?: (api: IOutPut) => string,
}

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,
}

  ```

  示例
  ```
export default {
  serverUrl: 'http://yapi.ywwl.org',
  outputFilePath: 'api',
  projectId: '24',
  _yapi_token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjE4LCJpYXQiOjE1NTY1MDYyMTUsImV4cCI6MTU1NzExMTAxNX0.ADmz2HEE6hKoe1DP_U2QtyKSSEURLf5soGKRNyJkX_o',
  _yapi_uid: '18',
  generateApiFileCode: (api) => {
    const arr = [
      `
      /**
      * ${api.title}
      * ${api.markdown || ''}
      **/
      `,
      api.requestInterface,
      api.responseInterface,
      `
      export default (data: ${api.reqInterfaceName}): ${api.resInterfaceName} => request({
        method: '${api.method}',
        url: '${api.path}',
        data: data
      })
      `,
    ]
    return arr.join(`
    `)
  }
}

  ```

  一般来说 generateApiFileCode 方法需要自己实现一下，组装拼接出符合自己期望的 接口代码格式

 - 3、生成代码
  `ywapi2ts`
  运行该命令 会根据步骤2的配置文件，生产出api（outputFilePath）文件夹，该文件夹下`index.ts`作为所有接口的导出口，供项目中导入使用

 - 4、查看接口变动日志`ywapi2ts changelog`

### feature

---
_Source: https://npm.io/package/ywapi2ts · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
