Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
S
swagger-to-ts
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
王绍森
swagger-to-ts
Commits
10c38e9d
提交
10c38e9d
authored
7月 23, 2020
作者:
王绍森
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改
上级
a4020e3c
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
68 行增加
和
71 行删除
+68
-71
.gitignore
.gitignore
+3
-2
generat.js
src/generat.js
+64
-68
index.js
src/index.js
+1
-1
没有找到文件。
.gitignore
浏览文件 @
10c38e9d
...
...
@@ -2,4 +2,5 @@
/models
/node_modules
.DS_Store
api-doc.json
\ No newline at end of file
api-doc.json
/dist
\ No newline at end of file
src/generat.js
浏览文件 @
10c38e9d
...
...
@@ -2,32 +2,46 @@ const fs = require('fs');
const
path
=
require
(
'path'
);
const
{
deleteFolderRecursive
}
=
require
(
'./utils'
);
const
{
EROFS
}
=
require
(
'constants'
);
const
{
format
}
=
require
(
'path'
);
function
formatModelName
(
str
)
{
if
(
!
str
)
{
throw
new
Error
(
'非法的model name: '
+
str
)
};
return
str
.
replace
(
/
[
-
\(\)\{\}]
/g
,
''
);
}
const
directory
=
path
.
join
(
process
.
cwd
(),
'dist'
);
function
getSubType
(
subty
e
)
{
function
getSubType
(
subty
pe
,
importedModels
)
{
// List«组织机构模型(OrgModel})»
// 组织机构模型(OrgModel})
// Page«组织机构模型(OrgModel})»
// boolean
if
(
!
subtye
)
{
console
.
error
(
"subtype为空: "
,
subtye
);
if
(
!
subty
p
e
)
{
console
.
error
(
"subtype为空: "
,
subty
p
e
);
return
;
}
if
(
!
subtye
.
includes
(
'«'
))
{
let
hasparent
=
subtye
.
match
(
/.*
\((
.+
)\)
.*/
)
if
(
hasparent
)
{
return
hasparent
[
1
]
if
(
!
subtype
.
includes
(
'«'
))
{
subtype
=
formatModelName
(
subtype
)
if
(
!
[
'boolean'
,
'number'
,
'boolean'
].
includes
(
subtype
))
{
if
(
!
importedModels
.
includes
(
subtype
))
{
importedModels
.
push
(
subtype
);
}
}
return
subtye
return
subtype
;
}
if
(
subtype
.
startsWith
(
'Page«'
))
{
return
`{rows:
${
getSubType
(
subtype
.
slice
(
5
,
-
1
),
importedModels
)}
[], total: number | string}`
;
}
if
(
subty
e
.
startsWith
(
'Page
«'
))
{
return
`
page<
${
getSubType
(
subtye
.
slice
(
5
,
-
1
))}
>
`
;
if
(
subty
pe
.
startsWith
(
'List
«'
))
{
return
`
${
getSubType
(
subtype
.
slice
(
5
,
-
1
),
importedModels
)}
[]
`
;
}
if
(
subty
e
.
startsWith
(
'Lis
t«'
))
{
return
`List<
${
getSubType
(
subtye
.
slice
(
5
,
-
1
))}
>`
;
if
(
subty
pe
.
startsWith
(
'Resul
t«'
))
{
return
getSubType
(
subtype
.
slice
(
7
,
-
1
),
importedModels
)
;
}
if
(
subtye
.
startsWith
(
'Result«'
))
{
return
`Result<
${
getSubType
(
subtye
.
slice
(
7
,
-
1
))}
>`
;
if
(
subtype
.
startsWith
(
"Map«"
))
{
const
[
keyType
,
valueType
]
=
subtype
.
slice
(
4
,
-
1
).
split
(
','
);
return
`{[
${
keyType
}
]:
${
valueType
}
}`
;
}
throw
new
Error
(
"为识别的subtype:"
+
subtye
)
}
...
...
@@ -36,11 +50,11 @@ function getSubType(subtye) {
* 获取ref的类型
*
*/
function
getTypeofDefinitions
(
ref
)
{
function
getTypeofDefinitions
(
ref
,
importedModels
)
{
// '#/definitions/Result«boolean»'
// '#/definitions/Result«List«资源管理模型(ModuleModel})»»'
// "#/definitions/资源管理新增参数模型"
return
getSubType
(
ref
.
replace
(
'#/definitions/'
,
''
));
return
getSubType
(
ref
.
replace
(
'#/definitions/'
,
''
)
,
importedModels
);
}
function
convertType
(
item
,
importedModels
)
{
...
...
@@ -52,7 +66,7 @@ function convertType(item, importedModels) {
return
convertType
(
item
.
items
,
importedModels
)
+
'[]'
;
}
if
(
$ref
)
{
const
typeofRef
=
getTypeofDefinitions
(
$ref
)
const
typeofRef
=
getTypeofDefinitions
(
$ref
,
importedModels
)
return
typeofRef
;
}
return
type
;
...
...
@@ -83,6 +97,7 @@ function getFields (properties, importedModels) {
function
getModelInterface
(
title
,
name
,
properties
)
{
const
imprtedModels
=
[];
name
=
formatModelName
(
name
);
const
titleComment
=
title
?
`
/**
...
...
@@ -101,38 +116,18 @@ function getModelInterface(title, name, properties) {
}
function
generateModels
(
definitions
)
{
const
modelDirectory
=
path
.
resolve
(
__dirname
,
'../models'
);
if
(
!
fs
.
existsSync
(
modelDirectory
))
{
fs
.
mkdirSync
(
modelDirectory
);
}
else
{
deleteFolderRecursive
(
modelDirectory
);
fs
.
mkdirSync
(
modelDirectory
);
}
const
modelNames
=
Object
.
keys
(
definitions
);
for
(
let
index
=
0
;
index
<
modelNames
.
length
;
index
++
)
{
const
modelName
=
modelNames
[
index
];
// Page, Map, Result不用生成Model文件,用泛型处理
if
([
'Page«'
,
'Map«'
,
'Result«'
].
some
(
i
=>
modelName
.
startsWith
(
i
)))
continue
;
let
title
,
name
;
const
{
properties
}
=
definitions
[
modelName
];
// 模型中文名称加模型英文名称的类型, 比如:投票候选人(VoteCandidatesModel)
const
match
=
modelName
.
match
(
/^
(
.+
)\((
.+
)\)
$/
);
if
(
match
)
{
title
=
match
[
1
];
name
=
match
[
2
];
}
else
if
(
modelName
)
{
// modelName只是一个单词
name
=
modelName
;
}
if
(
name
)
{
const
modelCotent
=
getModelInterface
(
title
,
name
,
properties
);
fs
.
writeFileSync
(
path
.
resolve
(
__dirname
,
`../models/
${
name
}
.ts`
),
modelCotent
,
{
encoding
:
"utf-8"
});
console
.
log
(
'write model: '
,
name
);
let
modelName
=
modelNames
[
index
];
if
([
"Page«"
,
"Result«"
,
'Map«'
].
some
(
prefix
=>
modelName
.
startsWith
(
prefix
)))
{
continue
;
}
const
{
title
,
properties
,
}
=
definitions
[
modelName
]
modelName
=
formatModelName
(
modelName
)
const
modelCotent
=
getModelInterface
(
title
,
title
,
properties
);
fs
.
writeFileSync
(
path
.
join
(
directory
,
`
${
modelName
}
.ts`
),
modelCotent
,
{
encoding
:
"utf-8"
});
console
.
log
(
'write model: '
,
modelName
);
}
}
...
...
@@ -142,17 +137,13 @@ function generateModels(definitions) {
* @param {*} parameters
*/
function
getParamsString
(
parameters
,
importedModels
)
{
const
parametersUsed
=
parameters
?
parameters
.
filter
(
i
=>
i
.
in
!==
'header'
)
:
null
;
const
paramsString
=
parametersUsed
?
parametersUsed
.
map
(
param
=>
{
let
fieldType
=
convertType
(
param
,
importedModels
);
if
(
fieldType
)
{
console
.
error
(
"函数参数类型获取失败:"
,
param
);
}
return
`
${
param
.
name
}${
param
.
required
?
''
:
'?'
}
:
${
fieldType
}
`
}).
join
(
', '
)
:
null
;
const
parameter
=
parameters
?
parameters
.
find
(
p
=>
p
.
in
===
'body'
&&
p
.
name
===
'param'
)
:
false
;
let
paramsString
;
if
(
parameter
)
{
paramsString
=
convertType
(
parameter
.
schema
,
importedModels
);
}
if
(
paramsString
)
{
return
'params:
{ '
+
paramsString
+
' }
'
return
'params:
'
+
paramsString
+
'
'
}
return
''
;
}
...
...
@@ -172,10 +163,10 @@ function getReturnType(schema, importedModels) {
function
getApiMethods
(
apiMethodMap
,
apiName
)
{
const
importedModels
=
[];
let
content
=
`
import request from './request'
`
;
let
content
=
`
\nimport request from './request'\n
`
;
const
methodNames
=
Object
.
keys
(
apiMethodMap
);
for
(
let
index
=
0
;
index
<
methodNames
.
length
;
index
++
)
{
cons
t
methodName
=
methodNames
[
index
];
le
t
methodName
=
methodNames
[
index
];
const
{
post
}
=
apiMethodMap
[
methodName
];
if
(
!
post
)
continue
;
const
{
parameters
,
responses
}
=
post
;
...
...
@@ -185,24 +176,20 @@ function getApiMethods(apiMethodMap, apiName) {
console
.
log
(
'response undefined: '
,
apiMethodMap
[
methodName
]);
}
const
returnType
=
getReturnType
(
responses
[
200
].
schema
,
importedModels
);
methodName
=
methodName
===
'delete'
?
'deleteApi'
:
methodName
;
content
+=
`
\t export function
${
methodName
}
(
${
paramsString
}
): Promise<
${
returnType
}
> {
return request(
'/
${
apiName
}
/
${
methodName
}
',
${
paramsString
?
'params'
:
''
}
);
return request(
${
paramsString
?
'params'
:
'{}'
}
, '/
${
apiName
}
/
${
methodName
}
'
);
}
`
;
}
return
importedModels
.
join
(
"
\
n"
)
+
content
;
return
importedModels
.
reduce
((
acc
,
model
)
=>
acc
+
`import
${
model
}
from './
${
model
}
';\n`
,
''
)
+
content
;
}
function
generateFunctions
(
paths
)
{
const
apisDirectory
=
path
.
resolve
(
__dirname
,
'../apis'
);
if
(
!
fs
.
existsSync
(
apisDirectory
))
{
fs
.
mkdirSync
(
apisDirectory
);
}
else
{
deleteFolderRecursive
(
apisDirectory
);
fs
.
mkdirSync
(
apisDirectory
);
}
const
pathnameList
=
Object
.
keys
(
paths
);
// {
...
...
@@ -220,7 +207,7 @@ function generateFunctions(paths) {
let
apiNames
=
Object
.
keys
(
apiMap
);
for
(
let
index
=
0
;
index
<
apiNames
.
length
;
index
++
)
{
const
apiName
=
apiNames
[
index
];
fs
.
writeFileSync
(
path
.
resolve
(
__dirname
,
`../apis/
${
apiName
}
.ts`
),
getApiMethods
(
apiMap
[
apiName
],
apiName
),
{
encoding
:
'utf-8'
});
fs
.
writeFileSync
(
path
.
join
(
directory
,
`
${
apiName
}
.ts`
),
getApiMethods
(
apiMap
[
apiName
],
apiName
),
{
encoding
:
'utf-8'
});
console
.
log
(
'write api functions: '
,
apiName
);
}
}
...
...
@@ -236,6 +223,15 @@ module.exports = function() {
// 网关
const
gateWay
=
jsonObject
.
basePath
;
if
(
!
fs
.
existsSync
(
directory
))
{
console
.
log
(
'create directory: '
,
directory
);
fs
.
mkdirSync
(
directory
);
}
else
{
console
.
log
(
'clean directory: '
,
directory
);
deleteFolderRecursive
(
directory
);
fs
.
mkdirSync
(
directory
);
}
generateModels
(
jsonObject
.
definitions
);
generateFunctions
(
jsonObject
.
paths
)
...
...
src/index.js
浏览文件 @
10c38e9d
const
getSwaggerJSON
=
require
(
'./getSwaggerJSON'
);
const
generat
=
require
(
'./generat'
)
const
url
=
'http://192.168.1.122
/produce/v1/api/uia
/v2/api-docs'
const
url
=
'http://192.168.1.122
:8101/zysoft-app-data-standard-web-service
/v2/api-docs'
getSwaggerJSON
(
url
,
(
err
)
=>
{
if
(
!
err
)
{
generat
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论