Skip to content

Commit 2c6aba8

Browse files
WangMingHua111Manweill
authored andcommitted
feat: classNameMode 支持 回调函数的形式
1 parent e9e57c4 commit 2c6aba8

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export interface ISwaggerOptions {
4141
serviceNameSuffix?: string
4242
enumNamePrefix?: string
4343
methodNameMode?: 'operationId' | 'path' | 'shortOperationId' | ((reqProps: IRequestMethod) => string)
44-
classNameMode?: 'parentPath' | 'normal'
44+
classNameMode?: 'parentPath' | 'normal' | ((path: string, tag: string[]) => string)
4545
/** only effect classNameMode='parentPath' */
4646
pathClassNameDefaultName?: string
4747
outputDir?: string

src/baseInterfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface ISwaggerOptions {
44
serviceNameSuffix?: string
55
enumNamePrefix?: string
66
methodNameMode?: 'operationId' | 'path' | 'shortOperationId' | ((reqProps: IRequestMethod) => string)
7-
classNameMode?: 'parentPath' | 'normal'
7+
classNameMode?: 'parentPath' | 'normal' | ((path: string, tag: string[]) => string)
88
/** only effect classNameMode='parentPath' */
99
pathClassNameDefaultName?: string
1010
outputDir?: string

src/requestCodegen/index.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { getClassNameByPath, getMethodNameByPath, RemoveSpecialCharacters } from '../utils'
2-
import { IPaths } from '../swaggerInterfaces'
3-
import { getRequestParameters } from './getRequestParameters'
4-
import { getResponseType } from './getResponseType'
51
import camelcase from 'camelcase'
6-
import { getRequestBody } from './getRequestBody'
72
import { ISwaggerOptions } from '../baseInterfaces'
3+
import { IPaths } from '../swaggerInterfaces'
4+
import { getClassNameByPath, getMethodNameByPath, RemoveSpecialCharacters } from '../utils'
85
import { getContentType } from './getContentType'
6+
import { getRequestBody } from './getRequestBody'
7+
import { getRequestParameters } from './getRequestParameters'
8+
import { getResponseType } from './getResponseType'
99
import { mapFormDataToV2 } from './mapFormDataToV2'
1010

1111
export interface IRequestClass {
@@ -29,10 +29,10 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
2929
options.methodNameMode === 'operationId'
3030
? reqProps.operationId
3131
: options.methodNameMode === 'shortOperationId'
32-
? trimSuffix(reqProps.operationId, reqProps.tags?.[0])
33-
: typeof options.methodNameMode === 'function'
34-
? options.methodNameMode(reqProps)
35-
: methodName
32+
? trimSuffix(reqProps.operationId, reqProps.tags?.[0])
33+
: typeof options.methodNameMode === 'function'
34+
? options.methodNameMode(reqProps)
35+
: methodName
3636
if (!methodName) {
3737
// console.warn('method Name is null:', path);
3838
continue
@@ -50,6 +50,8 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
5050
} else {
5151
className = camelcase(className, { pascalCase: true })
5252
}
53+
} else if (typeof options.classNameMode === 'function') {
54+
className = options.classNameMode(path, reqProps.tags || [])
5355
} else {
5456
if (!reqProps.tags) continue
5557
className = camelcase(RemoveSpecialCharacters(reqProps.tags[0]), { pascalCase: true })
@@ -59,8 +61,6 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
5961
// 如果是数字开头,则加上下划线
6062
if (!Number.isNaN(Number(className[0]))) className = '_' + className
6163

62-
63-
6464
// 是否存在
6565
if (!requestClasses[className]) {
6666
requestClasses[className] = []
@@ -138,7 +138,7 @@ export function requestCodegen(paths: IPaths, isV3: boolean, options: ISwaggerOp
138138
}
139139
} catch (error) {
140140
// 获取方法名字失败,跳过该请求
141-
console.log('error request, url: ', path);
141+
console.log('error request, url: ', path)
142142
continue
143143
}
144144

0 commit comments

Comments
 (0)