Skip to content

Commit e048dc2

Browse files
authored
Merge pull request #248 from FrontEndDev-org/feat/custom-method-name
feat(parser): 新增nameFormatter支持自定义方法名 Implement #247
2 parents ea3efd7 + 9eab1e3 commit e048dc2

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,29 @@ 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格式化方法,函数签名 ```(props: NameFormatterProps) => string;``` | ```({name}) => name``` |
163+
| `requestPathTypeName` | `string` | `false` | 请求路径参数类型名称 | `ReqPath` |
164+
| `requestQueryTypeName` | `string` | `false` | 请求查询参数类型名称 | `ReqParams` |
165+
| `requestBodyTypeName` | `string` | `false` | 请求体参数类型名称 | `ReqData` |
166+
| `responseBodyTypeName` | `string` | `false` | 响应体参数类型名称 | `ResData` |
167+
168+
<details>
169+
<summary>【点击展开】查看 NameFormatterProps 签名</summary>
170+
171+
## `NameFormatterProps` 签名:
172+
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
173+
|-----------------------|----------|---------|--------------------|-----------------------------|
174+
| `name` | `string` | `true` | 原始名称(经过内部处理,能保证唯一) ||
175+
| `method` | `string` | `true` | 方法 ||
176+
| `path` | `string` | `true` | 路径 ||
177+
| `operationId` | `string` | `false` | operationId ||
178+
179+
</details>
166180

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

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
@@ -42,7 +42,13 @@ export class PathsParser extends ComponentsParser {
4242
parseOperation(operation: OpenAPIV3.OperationObject): TypeOperation {
4343
const { parameters, requestBody: requestBodySchema } = operation;
4444
const { pathTypes, queryTypes } = this.parseOperationParameters(parameters);
45-
const name = this.named.nextOperationId(this.parsingMethod, this.parsingUrl, operation.operationId);
45+
const initialName = this.named.nextOperationId(this.parsingMethod, this.parsingUrl, operation.operationId);
46+
const name = this.options.nameFormatter({
47+
name: initialName,
48+
method: this.parsingMethod,
49+
url: this.parsingUrl,
50+
operationId: operation.operationId,
51+
});
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export interface TypeAlias extends TypeComments {
3434
export type TypeItem = TypeOrigin | TypeAlias;
3535
export type TypeList = TypeItem[];
3636

37+
export interface NameFormatterProps {
38+
name: string;
39+
method: string;
40+
url: string;
41+
operationId?: string;
42+
}
43+
3744
export interface ParserOptions {
3845
cwd?: string;
3946

@@ -49,6 +56,7 @@ export interface ParserOptions {
4956
*/
5057
okMediaType?: string;
5158

59+
nameFormatter?: (props: NameFormatterProps) => string;
5260
requestPathTypeName?: string;
5361
requestQueryTypeName?: string;
5462
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)