Skip to content

Commit 9eab1e3

Browse files
committed
fix(parser): nameFormatter 入参改为对象
1 parent 28e64d7 commit 9eab1e3

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +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-
| `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` |
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>
167180

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

src/parsers/BaseParser.ts

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

src/parsers/PathsParser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ 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';
65

76
export class PathsParser extends ComponentsParser {
87
parsingUrl = '';
@@ -43,12 +42,13 @@ export class PathsParser extends ComponentsParser {
4342
parseOperation(operation: OpenAPIV3.OperationObject): TypeOperation {
4443
const { parameters, requestBody: requestBodySchema } = operation;
4544
const { pathTypes, queryTypes } = this.parseOperationParameters(parameters);
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);
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+
});
5252
const requestPathTypeName = this.named.nextTypeName(name + this.options.requestPathTypeName);
5353
const requestQueryTypeName = this.named.nextTypeName(name + this.options.requestQueryTypeName);
5454
const requestBodyTypeName = this.named.nextTypeName(name + this.options.requestBodyTypeName);

src/parsers/types.ts

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

43
export type TypeUnit = 'number' | 'string' | 'boolean' | 'never' | 'object' | 'array' | 'any';
54

@@ -35,6 +34,13 @@ export interface TypeAlias extends TypeComments {
3534
export type TypeItem = TypeOrigin | TypeAlias;
3635
export type TypeList = TypeItem[];
3736

37+
export interface NameFormatterProps {
38+
name: string;
39+
method: string;
40+
url: string;
41+
operationId?: string;
42+
}
43+
3844
export interface ParserOptions {
3945
cwd?: string;
4046

@@ -50,10 +56,7 @@ export interface ParserOptions {
5056
*/
5157
okMediaType?: string;
5258

53-
nameFormatter?: (
54-
name: ReturnType<Named['nextOperationId']>,
55-
...params: Parameters<Named['nextOperationId']>
56-
) => string;
59+
nameFormatter?: (props: NameFormatterProps) => string;
5760
requestPathTypeName?: string;
5861
requestQueryTypeName?: string;
5962
requestBodyTypeName?: string;

test/parsers/PathsParser.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ test('custom name formatter', async () => {
113113
},
114114
},
115115
{
116-
nameFormatter: (name, method, url, operationId) => {
116+
nameFormatter: ({ name /*, method, url, operationId*/ }) => {
117117
return `custom_${name}`;
118118
},
119119
}

0 commit comments

Comments
 (0)