本篇教程主要介绍如何自动更新 Swagger
数据到 YApi 接口管理平台
,咱们假设你已经可以熟练使用 YApi接口管理平台。node
yapi-cli 依赖 Node.js , 请安装不低于 7.6 版本的 Node.js,若是你的机器已经安装了 yapi-cli 工具,且版本号 >= 2.7,可忽略该章节。git
方案一: 从 Node.js 官网下载安装包github
方案二: 使用 Node Version Manager(NVM) 安装 Node.js,你能够经过如下命令安装 NVM :npm
cURL:json
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
复制代码
Wget:api
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash
复制代码
NVM 安装好之后,重启终端并运行如下命令来安装 Node.js :bash
nvm install stable
复制代码
请确保 yapi-cli 工具版本 >= 1.2.7。服务器
npm install yapi-cli [-g]
复制代码
该功能可在任意一台机器使用,只需确保该机器可以链接到部署的 YApi 网站。curl
在任意一个目录下新建配置文件 yapi-import.json
,内容以下:工具
{
"type": "swagger",
"token": "17fba0027f300248b804",
"file": "swagger.json",
"merge": "good",
"server": "http://yapi.local.qunar.com:3000"
}
复制代码
type
是数据数据方式,目前官方只支持 swagger
token
是项目token,在 项目设置 -> token
设置获取
file
是 swagger 接口文档文件,可以使用绝对路径或 url
merge
导入旧的接口策略,默认使用智能模式,一共有 "normal"(普通模式) , "good"(智能合并), "merge"(彻底覆盖) 三种模式
server
是yapi服务器地址
在新建配置文件的当前目录
,执行下面指令就能把数据导入到 yapi 接口管理平台。
yapi import
复制代码
若是提示找不到
yapi
命令,可尝试执行yapi-cli
,由于部分系统环境不兼容。
配合 crontab 这类定时任务执行工具,就能够实现自动更新接口数据,相信聪明的你必定能搞定。