Skip to content

Commit 94953da

Browse files
perf: 增加枚举类型注释 (#604)
* feat: 增加枚举类型注释 * feat: 完善枚举类型注释 * Enhance enum comments for performance tracking --------- Co-authored-by: 故城 <1055120207@qq.com>
1 parent 6c53f96 commit 94953da

21 files changed

Lines changed: 75 additions & 8 deletions

.changeset/odd-webs-cover.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openapi-ts-request': patch
3+
---
4+
5+
perf: 增加枚举注释

src/generator/serviceGenarator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,7 @@ export default class ServiceGenerator {
15071507
schemaObject,
15081508
getType: (schema: ISchemaObject, namespace?: string) =>
15091509
this.getType(schema, namespace),
1510+
openAPIData: this.openAPIData,
15101511
});
15111512
}
15121513

src/generator/serviceGeneratorHelper.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ export function resolveAllOfObject(params: {
215215
export function getProps(params: {
216216
schemaObject: SchemaObject;
217217
getType: (schema: SchemaObject) => string;
218+
openAPIData?: OpenAPIObject;
218219
}): IPropObject[] {
219-
const { schemaObject, getType } = params;
220+
const { schemaObject, getType, openAPIData } = params;
220221
const requiredPropKeys = schemaObject?.required ?? false;
221222
const properties = schemaObject.properties;
222223

@@ -226,15 +227,35 @@ export function getProps(params: {
226227
// eslint-disable-next-line no-useless-escape
227228
propKey = propKey.replace(/[\[|\]]/g, '');
228229

230+
// 获取描述信息,如果是 $ref 引用,尝试获取引用对象的描述
231+
let desc = [schema.title, schema.description]
232+
.filter((item) => item)
233+
.join(' ')
234+
.replace(lineBreakReg, '');
235+
236+
// 如果是 $ref 引用,尝试获取引用对象的描述
237+
if (isReferenceObject(schema) && openAPIData) {
238+
const refName = getLastRefName(schema.$ref);
239+
const refSchema = openAPIData.components?.schemas?.[
240+
refName
241+
] as SchemaObject;
242+
if (refSchema) {
243+
const refDesc = [refSchema.title, refSchema.description]
244+
.filter((item) => item)
245+
.join(' ')
246+
.replace(lineBreakReg, '');
247+
if (refDesc) {
248+
desc = desc + refDesc;
249+
}
250+
}
251+
}
252+
229253
// 复用 schema 部分字段
230254
return {
231255
...schema,
232256
name: propKey,
233257
type: getType(schema),
234-
desc: [schema.title, schema.description]
235-
.filter((item) => item)
236-
.join(' ')
237-
.replace(lineBreakReg, ''),
258+
desc: desc,
238259
// 如果没有 required 信息,默认全部是非必填
239260
required: requiredPropKeys
240261
? requiredPropKeys.some((key) => key === propKey)

templates/enum.njk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
{%- if list.length > 0 %}
55
{% for type in list -%}
66
{%- if type.isEnum %}
7+
{%- if type.description%}
8+
/** {{ type.description }} */
9+
{%- endif %}
710
export enum {{ type.typeName | safe }} {{ type.type }};
811

912
export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}

templates/interface.njk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
{%- endfor %}
4444
{%- else %}
4545
{%- if type.isEnum %}
46+
{%- if type.description%}
47+
/** {{ type.description }} */
48+
{%- endif %}
4649
export enum {{ type.typeName | safe }} {{ type.type }};
4750

4851
export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}

test/__snapshots__/common/小驼峰命名文件和请求函数.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export type PetUsingPutResponses = {
411411
405: unknown;
412412
};
413413

414+
/** Order Status */
414415
export enum StatusEnum {
415416
'placed' = 'placed',
416417
'approved' = 'approved',
@@ -419,6 +420,7 @@ export enum StatusEnum {
419420

420421
export type IStatusEnum = keyof typeof StatusEnum;
421422

423+
/** pet status in the store */
422424
export enum StatusEnum2 {
423425
'available' = 'available',
424426
'pending' = 'pending',

test/__snapshots__/common/支持 null 类型作为默认值.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export type PetUsingPutResponses = {
411411
405: unknown;
412412
};
413413

414+
/** Order Status */
414415
export enum StatusEnum {
415416
'placed' = 'placed',
416417
'approved' = 'approved',
@@ -419,6 +420,7 @@ export enum StatusEnum {
419420

420421
export type IStatusEnum = keyof typeof StatusEnum;
421422

423+
/** pet status in the store */
422424
export enum StatusEnum2 {
423425
'available' = 'available',
424426
'pending' = 'pending',

test/__snapshots__/common/正常命名文件和请求函数.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ export type PetUsingPutResponses = {
411411
405: unknown;
412412
};
413413

414+
/** Order Status */
414415
export enum StatusEnum {
415416
'placed' = 'placed',
416417
'approved' = 'approved',
@@ -419,6 +420,7 @@ export enum StatusEnum {
419420

420421
export type IStatusEnum = keyof typeof StatusEnum;
421422

423+
/** pet status in the store */
422424
export enum StatusEnum2 {
423425
'available' = 'available',
424426
'pending' = 'pending',

test/__snapshots__/common/测试 number类型 枚举.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export type CityGetRes = {
154154
};
155155

156156
export type CityPutRes = {
157+
/** Poi2CategoryEnum poi2分类 */
157158
city: Poi2CategoryEnum;
158159
};
159160

@@ -162,6 +163,7 @@ export type HTTPValidationError = {
162163
detail?: ValidationError[];
163164
};
164165

166+
/** poi2分类 */
165167
export enum Poi2CategoryEnum {
166168
'STRING_NUMBER_-1' = '-1',
167169
'STRING_NUMBER_-1000' = '-1000',

test/__snapshots__/common/测试 number类型 枚举,使用 desc 解析枚举.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export type ResultOutputUserInfoOutput = {
3535
data?: UserInfoOutput;
3636
};
3737

38+
/** 系统用户角色:User(普通用户)=0,Agent(经纪人)=1,Admin(管理员)=2 */
3839
export enum SysUserRole {
3940
User = 0,
4041
Agent = 1,
@@ -50,6 +51,7 @@ export type UserInfoOutput = {
5051
avatar?: string;
5152
/** 手机号 */
5253
phone?: string;
54+
/** 系统用户角色:User(普通用户)=0,Agent(经纪人)=1,Admin(管理员)=2 */
5355
role?: SysUserRole;
5456
};
5557
/* eslint-disable */

0 commit comments

Comments
 (0)