peer.ts

convert your apib to typescript

Usage no npm install needed!

<script type="module">
  import peerTs from 'https://cdn.skypack.dev/peer.ts';
</script>

README

Peer.ts 1.2

1.2 版主要优化:

  1. 优化了配置文件, 提供了更灵活的编译方式.
  2. 优化了输出文件的格式, 更加的通用.

原理

apib文档 => swagger.json => 生成描述 ts 代码的json => 合成 Typescript 的代码及文件.

安装

1. 全局安装:

yarn global add peer.ts

cd 到有 peer.config.json 文件的目录, 运行 peer 即可.

2. 局部安装:

yarn add peer.ts --dev

在package.json 中 添加 npm script : "peer": "peer";

cd 到有 peer.config.json 文件的目录, 运行 yarn peer 即可.

Peer.ts 编译配置文件: peer.config.json

例如:

{
  "rootDir": ".",
  "loggerDir": "log",
  "nameConfig": {
    "input": "name.json",
    "output": "name.json"
  },
  "apibPath": "todo.apib",
  "output": {
    "apiFileName": "TestApi",
    "dataFileName": "TestData",
    "urlFileName": "Url"
  },
  "exceptHeaders": {
    "Content-Type": "annotation"
  }
}

各个字段的介绍:

  1. rootDir , 相对于当前运行路径的编译路径, 其他配置项的路径都相对于这个路径.
  2. loggerDir, 日志的路径.
  3. apibPath, 接口文档的输入路径.
  4. output, 输出的 TypeScript文件 的名字配置.
  5. exceptHeaders , 需要忽略的http请求头. 忽略模式目前只可以选择 "annotation" --- 注释的方式.

nameConfig. 名字的配置文件, 即接口的英文名配置.

第一次运行 peer 进行解析时, 会自动生成初始的命名配置表.

命名配置表的输入输出为同一个文件时可以直接修改这个文件, 输出入和输出会智能合并, 不会发生简单的覆盖

apib 文档编写的注意事项:

1. 数组的类型描述:

✅: RoomOrderList (array, fixed-type),

❌: RoomOrderList (array, fixed);

2. 不可以有中文的标点符号.

Peer.ts 的待办事项:

  • 生成 数据的 mock 函数
  • 使用json schema 测试 api 接口.