Skip to content

Commit

Permalink
fix: swagger命令不传action不解析ts、范型的类型名称优化
Browse files Browse the repository at this point in the history
  • Loading branch information
kongjing@dian.so authored and kongjing@dian.so committed Nov 4, 2022
1 parent 06da6db commit 8f0c40f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
16 changes: 9 additions & 7 deletions packages/api/src/swagger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ swagger data +
`),
)
await transform(swaggerData, path_, modules_)

setTimeout(() => {
file({
path: path_,
action: action,
forceUpdate: true,
// 选择创建请求方法时,才执行file生成请求方法和ts的ast描述
if (action) {
setTimeout(() => {
file({
path: path_,
action: action,
forceUpdate: true,
})
})
})
}
}
99 changes: 54 additions & 45 deletions packages/api/src/swagger/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import pat from 'path'
import fs from 'fs'
import prettier from 'prettier'
import * as ora from 'ora'
import { pinyin } from 'pinyin-pro'
import log from '../log.js'
import { getPrettierConfig } from '../config/getPrettierConfig.js'
import { createTypeFileName } from './createTypeFileName.js'
Expand Down Expand Up @@ -336,59 +337,55 @@ const typeCache: any[] = []

function formatBaseTypeKey(key: string) {
let res = key
const invalidMark = [
'(',
')',
',',
'=',
'(',
')',
',',
'#/components/schemas/',
'#/definitions/',
'`',
' ',
'[',
']',
]

invalidMark.forEach((it) => {
res = replaceAll(it, '', res)
})
res = res
.replace('#/components/schemas/', '')
.replace('#/definitions/', '')
.replace('`', '')
const origin = res

/** 服务端范型类转换成统一的名称,后续作重复处理 */
if (res.includes('«')) {
res = res.split('«')[0] || ''
}

res = res.replace(/«/g, '').replace(/»/g, '').replace(/\./g, 'a')
if (res.includes('[[')) {
res = res.split('[[')[0] || ''
}

if (res.includes('<')) {
res = res.split('<<')[0] || ''
}

if (typeMap[res]) return typeMap[res]
if (res.includes('(')) {
res = res.split('(')[0] || ''
}

let re = ''
if (res.includes('(')) {
res = res.split('(')[0] || ''
}

if (res.length > 20) {
re = res.slice(res.length - 20)
if (re && !typeCache.includes(re)) {
typeCache.push(re)
} else {
re = re + '1'
while (typeCache.includes(re)) {
re = re + '1'
}
typeCache.push(re)
}
typeMap[res] = re
} else {
re = res
while (typeCache.includes(re)) {
re = re + '1'
/** 引用过长的时候只取后续两位 */
if (res.includes('.')) {
const arr: string[] = res.split('.')
const arrLen = arr.length
if (arr && arrLen >= 2 && arr[arrLen - 1] && arr[arrLen - 2]) {
// @ts-ignore
res = arr[arrLen - 1] + wordFirstBig(arr[arrLen - 2])
}
typeCache.push(re)
typeMap[res] = re
}
res = res.replace(/\s/g, '')
res = pinyin(res, { toneType: 'none' })
res = res.replace(/\s/g, '')

return re
}
if (typeMap[origin]) return typeMap[origin]

while (typeCache.includes(res)) {
res = resetRepeatName(res)
}
typeCache.push(res)
typeMap[origin] = res

function replaceAll(find, replace, str) {
const find_ = find.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')
return str.replace(new RegExp(find_, 'g'), replace)
return res
}

function createComments(params?: Record<string, any>) {
Expand All @@ -406,3 +403,15 @@ function createComments(params?: Record<string, any>) {

return res
}

function resetRepeatName(name) {
if (/[0-9]{1,4}$/g.test(name)) {
let words = name.replace(/[0-9]{1,4}$/g, '')
let num = name.match(/[0-9]{1,4}$/g)[0] || 0

num = Number(num) + 1
return `${words}${num}`
} else {
return `${name}0`
}
}

0 comments on commit 8f0c40f

Please # to comment.