Skip to content

Commit 28e64d7

Browse files
committed
feat(parser): 新增nameFormatter支持自定义方法名 Implement #247
1 parent ea3efd7 commit 28e64d7

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ console.log(pet);
154154
| `openAPIs` | `OpenAPIOptions[]` | `true` | OpenAPIOptions 列表,至少需要配置一个 ||
155155

156156
## `ParserOptions` 签名:
157-
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
158-
|------------------------|----------|---------|------------|--------------------|
159-
| `cwd` | `string` | `false` | 当前工作路径 | `process.cwd()` |
160-
| `okCode` | `number` | `false` | ok 的响应码 | `200` |
161-
| `okMediaType` | `number` | `false` | ok 的响应类型 | `application/json` |
162-
| `requestPathTypeName` | `string` | `false` | 请求路径参数类型名称 | `ReqPath` |
163-
| `requestQueryTypeName` | `string` | `false` | 请求查询参数类型名称 | `ReqParams` |
164-
| `requestBodyTypeName` | `string` | `false` | 请求体参数类型名称 | `ReqData` |
165-
| `responseBodyTypeName` | `string` | `false` | 响应体参数类型名称 | `ResData` |
157+
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
158+
|------------------------|------------|---------|----------------------------------------------------------------------------------------------|----------------------|
159+
| `cwd` | `string` | `false` | 当前工作路径 | `process.cwd()` |
160+
| `okCode` | `number` | `false` | ok 的响应码 | `200` |
161+
| `okMediaType` | `number` | `false` | ok 的响应类型 | `application/json` |
162+
| `nameFormatter` | `function` | `false` | 自定义name解析格式化方法,函数签名 ```(name:string,method:string,url:string,operationId?:string)=>string``` | ```(name) => name``` |
163+
| `requestPathTypeName` | `string` | `false` | 请求路径参数类型名称 | `ReqPath` |
164+
| `requestQueryTypeName` | `string` | `false` | 请求查询参数类型名称 | `ReqParams` |
165+
| `requestBodyTypeName` | `string` | `false` | 请求体参数类型名称 | `ReqData` |
166+
| `responseBodyTypeName` | `string` | `false` | 响应体参数类型名称 | `ResData` |
166167

167168
## `PrinterOptions` 签名:
168169
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |

src/parsers/BaseParser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class BaseParser {
1111
cwd: process.cwd(),
1212
okCode: 200,
1313
okMediaType: JSON_MIME,
14+
nameFormatter: (name) => name,
1415
requestPathTypeName: 'ReqPath',
1516
requestQueryTypeName: 'ReqParams',
1617
requestBodyTypeName: 'ReqData',

src/parsers/PathsParser.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { OpenAPIV3 } from 'openapi-types';
22
import { ComponentsParser } from './ComponentsParser';
33
import { BLOB_MIME, JSON_MIME, HTTP_METHODS } from './const';
44
import type { TypeItem, TypeList, TypeOperation, TypeOperations, TypeOrigin } from './types';
5+
import type { Named } from './Named';
56

67
export class PathsParser extends ComponentsParser {
78
parsingUrl = '';
@@ -42,7 +43,12 @@ export class PathsParser extends ComponentsParser {
4243
parseOperation(operation: OpenAPIV3.OperationObject): TypeOperation {
4344
const { parameters, requestBody: requestBodySchema } = operation;
4445
const { pathTypes, queryTypes } = this.parseOperationParameters(parameters);
45-
const name = this.named.nextOperationId(this.parsingMethod, this.parsingUrl, operation.operationId);
46+
const nameParams: Parameters<Named['nextOperationId']> = [
47+
this.parsingMethod,
48+
this.parsingUrl,
49+
operation.operationId,
50+
];
51+
const name = this.options.nameFormatter(this.named.nextOperationId(...nameParams), ...nameParams);
4652
const requestPathTypeName = this.named.nextTypeName(name + this.options.requestPathTypeName);
4753
const requestQueryTypeName = this.named.nextTypeName(name + this.options.requestQueryTypeName);
4854
const requestBodyTypeName = this.named.nextTypeName(name + this.options.requestBodyTypeName);

src/parsers/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { OpenAPIV3Document } from '../types/openapi';
2+
import type { Named } from './Named';
23

34
export type TypeUnit = 'number' | 'string' | 'boolean' | 'never' | 'object' | 'array' | 'any';
45

@@ -49,6 +50,10 @@ export interface ParserOptions {
4950
*/
5051
okMediaType?: string;
5152

53+
nameFormatter?: (
54+
name: ReturnType<Named['nextOperationId']>,
55+
...params: Parameters<Named['nextOperationId']>
56+
) => string;
5257
requestPathTypeName?: string;
5358
requestQueryTypeName?: string;
5459
requestBodyTypeName?: string;

test/parsers/PathsParser.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,54 @@ test('empty path item method responses keys + specify operationId', async () =>
9090
]);
9191
});
9292

93+
test('custom name formatter', async () => {
94+
const parser = new PathsParser(
95+
{
96+
info: {
97+
title: 'test',
98+
version: '1.0.0',
99+
},
100+
openapi: '3.0.0',
101+
paths: {
102+
'/pet': {
103+
get: {
104+
operationId: 'findPet',
105+
responses: {},
106+
},
107+
},
108+
'/cat': {
109+
get: {
110+
responses: {},
111+
},
112+
},
113+
},
114+
},
115+
{
116+
nameFormatter: (name, method, url, operationId) => {
117+
return `custom_${name}`;
118+
},
119+
}
120+
);
121+
122+
const t = parser.parsePaths();
123+
expect(t).toEqual<TypeOperations>([
124+
{
125+
name: `custom_getCat`,
126+
method: OpenAPIV3.HttpMethods.GET,
127+
url: '/cat',
128+
request: {},
129+
response: {},
130+
},
131+
{
132+
name: `custom_findPet`,
133+
method: OpenAPIV3.HttpMethods.GET,
134+
url: '/pet',
135+
request: {},
136+
response: {},
137+
},
138+
]);
139+
});
140+
93141
test('operationId is reserved', async () => {
94142
const parser = new PathsParser({
95143
info: {

0 commit comments

Comments
 (0)