代码拉取完成,页面将自动刷新
通过 swagger 文档生成 TypeScript 代码,支持 swagger 2.0
生成代码结构如下:
国内可访问 gitee 镜像
httpClient
,外部可通过 httpClient.handleRequest
处理请求jsdoc
npm install @upace/swagger-to-ts --global
# 在线swagger
swagger-to-ts -s http://yourhost/api-docs -o output
# 本地swagger
swagger-to-ts -s ./swagger.json -o output
生成的代码使用自定义 httpClient,不会发送请求。在调用接口前你需要拦截处理,以下是示例
import httpClient from "./output/httpClient";
httpClient.handleRequest(async ({ method, url, body }) => {
const res = await fetch(url, {
method,
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
return res.json();
});
Options:
-s, --swagger [swagger] swagger路径或url
-o, --output [output] 输入目录
--exclude-tags [excludeTags] 排除指定tag,逗号分割
--include-tags [includeTags] 只生成特定tag的接口,逗号分割
--exclude-path [excludePath] 排除特定路径,支持正则
--include-path [includePath] 只生存特定路径接口,支持正则
--http-client-output [path] httpClient输出路径,默认存于输出目录
--config [config] 使用配置文件
-h, --help display help for command
除了使用命令行外,swagger-to-ts
还支持 swagger.config.js
进行更多配置
swagger-to-ts --config swagger.config.js
swagger.config.js 示例
module.exports = {
httpClientOutput: "output",
swaggers: [
{
swagger: "swagger1.json",
output: "output/swagger1",
// type Filter = string | RegExp | FunctionFilter;
excludePath: /internal/,
includePath: /api/,
excludeTags: "tag1,tag2",
includeTags: () => true,
// 重命名接口,注意需要拼接basePath
rename: {
'/api/order': {
get: 'queryOrder'
}
},
// 重写路径
rewritePath: {
'^/order': '/api/order'
}
},
{
swagger: "swagger2.json",
output: "output/swagger2",
// type Filter = string | RegExp | FunctionFilter;
excludePath: (pathName, operation) => pathName.includes('internal'),
includePath: /api/,
excludeTags: "tag1,tag2",
},
],
};
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。