Skip to content

Commit

Permalink
fix: guardconfig.prefixReg can work with yapi, also downward compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
gogoyqj committed Jul 25, 2023
1 parent d59ddc6 commit b4cc721
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function operationIdGuard(swagger: Autos.SwaggerJson, config: Autos.Guard
url
.replace(prefixReg, '')
.replace(/(^\/|})/g, '')
.replace(/[/{_-]{1,}([^/])/g, (mat, u: string) => u.toUpperCase()) +
.replace(/[/{_-]{1,}([^/])/g, (_mat, u: string) => u.toUpperCase()) +
methodPrefix +
method[0].toUpperCase() +
method.substring(1);
Expand Down
5 changes: 5 additions & 0 deletions src/types/swagger.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ export declare global {
beforeTransform?: (yapiJSON: YApiCategory[]) => YApiCategory[];
/** yapi json 转换成 swagger json 后的钩子 */
afterTransform?: (swaggerJSON: SwaggerJson) => SwaggerJson;
/**
* 配置为 true,向下兼容,YAPI 转 Swagger 时不生成 operationId,并在生成方法名,采用旧格式,即:url + Method 驼峰格式
* 否则生成 operationId,并在生成方法名,采用新格式,即: url + Using + Method 驼峰格式,且会通过 guardconfig.prefixReg 等配置对方法名进行进一步处理
* */
_capatibleYAPI?: boolean;
}

/** CLI配置 */
Expand Down
13 changes: 10 additions & 3 deletions src/yapi/yapi2swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function yapiJSON2swagger(
yapiList: Autos.YApiCategory[],
yapiConfig: Autos.JSON2Service['yapiConfig'] = {}
) {
const { beforeTransform, afterTransform } = yapiConfig;
const { beforeTransform, afterTransform, _capatibleYAPI = false } = yapiConfig;

// modify before transform yapi document to swagger document
const list = beforeTransform ? beforeTransform(yapiList) : yapiList;
Expand All @@ -28,14 +28,21 @@ export default function yapiJSON2swagger(
...api,
url
};
paths[url][method] = {
const swaggerItem: Autos.PathJson = (paths[url][method] = {
tags: [category.name],
summary: api.title,
description: api.markdown,
consumes: convertCosumes(syntheticAPI, yapiConfig),
parameters: convertParams(syntheticAPI, yapiConfig),
responses: convertResponse(syntheticAPI, yapiConfig)
};
});
if (_capatibleYAPI !== true) {
swaggerItem.operationId = `${url
.replace(/(^\/|})/g, '')
.replace(/[/{_-]{1,}([^/])/g, (_mat, u: string) =>
u.toUpperCase()
)}${method.replace(/^[a-z]/g, c => c.toUpperCase())}`;
}
}
}

Expand Down

0 comments on commit b4cc721

Please sign in to comment.