Skip to content

Commit 4ea7857

Browse files
perf: perf priorityRule feature code
1 parent 9755877 commit 4ea7857

File tree

17 files changed

+180
-187
lines changed

17 files changed

+180
-187
lines changed

.changeset/rich-trees-applaud.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
'openapi-ts-request': minor
2+
'openapi-ts-request': patch
33
---
44

5-
rename allowedPath and allowedTags
5+
chore: rename allowedTags => includeTags, allowedPaths => includePaths

.changeset/twelve-mails-shout.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
'openapi-ts-request': patch
2+
'openapi-ts-request': minor
33
---
44

5-
support priorityRule
5+
feat: support priorityRule

.changeset/wild-ears-tell.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: perf priorityRule feature code

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
### Patch Changes
5454

55-
- [`430c164`](https://github.com/openapi-ui/openapi-ts-request/commit/430c1648fa0cb1737ed83f762bff434678488c50) Thanks [@rookie-luochao](https://github.com/rookie-luochao)! - fix: fix $ref includes encode character causing includeTags function error
55+
- [`430c164`](https://github.com/openapi-ui/openapi-ts-request/commit/430c1648fa0cb1737ed83f762bff434678488c50) Thanks [@rookie-luochao](https://github.com/rookie-luochao)! - fix: fix $ref includes encode character causing allowedTags function error
5656

5757
## 0.8.2
5858

README-en_US.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
English | <a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README.md">简体中文</a>
66

7-
Generate TS interfaces, request client, request mock service, enum, type field label, JSON Schemas based on [Swagger2/OpenAPI3](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) specification
7+
Generate TS interfaces, request client, request mock service, enum, type field label, JSON Schemas based on [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) specification
88

99
文档:[use docs](https://github.com/openapi-ui/openapi-ts-request/issues/100)
1010

1111
## Features
1212

13-
- support Swagger2.0/OpenAPI 3.0,3.1 specification
13+
- support Swagger2.0/OpenAPI/Apifox 3.0,3.1 specification
1414
- generate TypeScript interface, reuquest client, request mock service, enum, type field label, JSON Schemas
1515
- support work with npx, CLI, Nodejs
1616
- support custom request function, Fetch、Axios、[UniApp-request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR client available
@@ -136,13 +136,13 @@ $ openapi --help
136136
-V, --version output the version number
137137
-i, --input <string> OpenAPI specification, can be a path, url (required)
138138
-o, --output <string> output directory (required)
139-
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
140-
--enableLogging <boolean> open the log
141-
--priorityRule <string> priority rule, include/exclude/both
142-
--includeTags <string|RegExp>[] generate code from include tags
143-
--includePaths <string|RegExp>[] generate code from include paths
144-
--excludeTags <string|RegExp>[] generate code from exclude tags
145-
--excludePaths <string|RegExp>[] generate code from exclude paths
139+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
140+
--enableLogging <boolean> open the log (default: false)
141+
--priorityRule <string> priority rule, include/exclude/both (default: "include")
142+
--includeTags <<string|RegExp>[]> generate code from include tags
143+
--includePaths <<string|RegExp>[]> generate code from include paths
144+
--excludeTags <<string|RegExp>[]> generate code from exclude tags
145+
--excludePaths <<string|RegExp>[]> generate code from exclude paths
146146
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
147147
string]: unknown }")
148148
--requestImportStatement <string> custom request import statement, for example: "const request =
@@ -151,7 +151,7 @@ $ openapi --help
151151
"'api'"(string)
152152
--isDisplayTypeLabel <boolean> generate label matching type field (default: false)
153153
--isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
154-
--mockFolder <string> mock file path
154+
--mockFolder <string> mock file path, for example: './mocks'
155155
--authorization <string> docs authorization
156156
--nullable <boolean> null instead of optional (default: false)
157157
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
@@ -184,7 +184,7 @@ openapi -i ./spec.json -o ./apis
184184
| apiPrefix | no | string | - | custom the prefix of the api path, for example: 'api'(variable), "'api'"(string) |
185185
| isDisplayTypeLabel | no | boolean | false | generate label matching type field |
186186
| isGenJsonSchemas | no | boolean | false | generate JSON Schemas |
187-
| mockFolder | no | string | './mocks' | mock file path |
187+
| mockFolder | no | string | - | mock file path, for example: './mocks' |
188188
| authorization || string | - | docs authorization |
189189
| nullable | no | boolean | false | null instead of optional |
190190
| isTranslateToEnglishTag | no | boolean | false | translate chinese tag name to english tag name |

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
<a href="https://github.com/openapi-ui/openapi-ts-request/blob/master/README-en_US.md">English</a> | 简体中文
66

7-
根据 [Swagger2/OpenAPI3](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 TS 类型, 客户端请求函数, 模拟请求响应服务, 枚举, 类型字段翻译, JSON Schemas
7+
根据 [Swagger2/OpenAPI3/Apifox](https://swagger.io/blog/news/whats-new-in-openapi-3-0/) 文档生成 TS 类型, 客户端请求函数, 模拟请求响应服务, 枚举, 类型字段翻译, JSON Schemas
88

99
文档:[使用手册](https://github.com/openapi-ui/openapi-ts-request/issues/100)
1010

1111
## 功能
1212

13-
- 支持 Swagger2.0/OpenAPI 3.0,3.1 定义
13+
- 支持 Swagger2.0/OpenAPI/Apifox 3.0,3.1 定义
1414
- 生成 TS 类型, 请求客户端, 请求模拟服务, 枚举, 类型字段翻译, JSON Schemas
1515
- 支持通过 npx、CLI、Nodejs 的方式使用
1616
- 支持自定义请求工具函数, 支持 Fetch、Axios、[UniApp-Request](https://github.com/openapi-ui/openapi-ts-request/issues/46)、Node.js、XHR 客户端
@@ -136,13 +136,13 @@ $ openapi --help
136136
-V, --version output the version number
137137
-i, --input <string> OpenAPI specification, can be a path, url (required)
138138
-o, --output <string> output directory (required)
139-
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch", default is "axios"
140-
--enableLogging <boolean> open the log
141-
--priorityRule <string> priority rule, include/exclude/both
142-
--includeTags <string|RegExp>[] generate code from include tags
143-
--includePaths <string|RegExp>[] generate code from include paths
144-
--excludeTags <string|RegExp>[] generate code from exclude tags
145-
--excludePaths <string|RegExp>[] generate code from exclude paths
139+
--requestLibPath <string> custom request lib path, for example: "@/request", "node-fetch" (default: "axios")
140+
--enableLogging <boolean> open the log (default: false)
141+
--priorityRule <string> priority rule, include/exclude/both (default: "include")
142+
--includeTags <(string|RegExp)[]> generate code from include tags
143+
--includePaths <(string|RegExp)[]> generate code from include paths
144+
--excludeTags <(string|RegExp)[]> generate code from exclude tags
145+
--excludePaths <(string|RegExp)[]> generate code from exclude paths
146146
--requestOptionsType <string> custom request method options parameter type (default: "{ [key:
147147
string]: unknown }")
148148
--requestImportStatement <string> custom request import statement, for example: "const request =
@@ -151,7 +151,7 @@ $ openapi --help
151151
"'api'"(string)
152152
--isDisplayTypeLabel <boolean> generate label matching type field (default: false)
153153
--isGenJsonSchemas <boolean> generate JSON Schemas (default: false)
154-
--mockFolder <string> mock file path
154+
--mockFolder <string> mock file path, for example: './mocks'
155155
--authorization <string> docs authorization
156156
--nullable <boolean> null instead of optional (default: false)
157157
--isTranslateToEnglishTag <boolean>translate chinese tag name to english tag name (default: false)
@@ -184,7 +184,7 @@ openapi --i ./spec.json --o ./apis
184184
| apiPrefix || string | - | api path的前缀,例如:'api'(动态变量), "'api'"(字符串) |
185185
| isDisplayTypeLabel || boolean | false | 是否生成 type 对应的label |
186186
| isGenJsonSchemas || boolean | false | 是否生成 JSON Schemas |
187-
| mockFolder || string | './mocks' | mock文件路径 |
187+
| mockFolder || string | - | mock文件路径,例如:'./mocks' |
188188
| authorization || string | - | 文档权限凭证 |
189189
| nullable || boolean | false | 使用 null 代替可选 |
190190
| isTranslateToEnglishTag || boolean | false | 将中文 tag 名称翻译成英文 tag 名称 |

src/bin/openapi.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,29 @@ const params = program
1616
.requiredOption('-o, --output <string>', 'output directory (required)')
1717
.option(
1818
'--requestLibPath <string>',
19-
'custom request lib path, for example: "@/request", "node-fetch", default is "axios"'
19+
'custom request lib path, for example: "@/request", "node-fetch" (default: "axios")'
20+
)
21+
.option('--enableLogging <boolean>', 'open the log', false)
22+
.option(
23+
'--priorityRule <string>',
24+
'priority rule, include/exclude/both (default: "include")'
25+
)
26+
.option(
27+
'--includeTags <(string|RegExp)[]>',
28+
'generate code from include tags'
29+
)
30+
.option(
31+
'--includePaths <(string|RegExp)[]>',
32+
'generate code from include paths'
33+
)
34+
.option(
35+
'--excludeTags <(string|RegExp)[]>',
36+
'generate code from exclude tags'
37+
)
38+
.option(
39+
'--excludePaths <(string|RegExp)[]>',
40+
'generate code from exclude paths'
2041
)
21-
.option('--includeTags <string[]>', 'generate results from include tags')
2242
.option(
2343
'--requestOptionsType <string>',
2444
'custom request method options parameter type (default: "{ [key: string]: unknown }")'
@@ -77,7 +97,12 @@ async function run() {
7797
schemaPath: input,
7898
serversPath: output,
7999
requestLibPath: params.requestLibPath as string,
100+
enableLogging: JSON.parse(params.enableLogging as string) === true,
101+
priorityRule: params.priorityRule as string,
80102
includeTags: params.includeTags as string[],
103+
includePaths: params.includePaths as string[],
104+
excludeTags: params.excludeTags as string[],
105+
excludePaths: params.excludePaths as string[],
81106
requestOptionsType: params.requestOptionsType as string,
82107
apiPrefix: params.apiPrefix as string,
83108
isDisplayTypeLabel:

src/generator/serviceGenarator.ts

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ export default class ServiceGenerator {
9898
protected schemaList: ISchemaItem[] = [];
9999

100100
constructor(config: GenerateServiceProps, openAPIData: OpenAPIObject) {
101-
// 默认值
102101
this.finalPath = '';
103102
this.config = {
104103
templatesFolder: join(__dirname, '../../', 'templates'),
@@ -113,15 +112,6 @@ export default class ServiceGenerator {
113112

114113
const priorityRule: PriorityRule =
115114
PriorityRule[config.priorityRule as keyof typeof PriorityRule];
116-
117-
if (
118-
priorityRule === PriorityRule.both &&
119-
isEmpty(includeTags) &&
120-
isEmpty(includePaths)
121-
) {
122-
// 没意义,可以直接跳过
123-
this.log('priorityRule include need includeTags or includePaths');
124-
}
125115
const hookCustomFileNames =
126116
this.config.hook?.customFileNames || getDefaultFileTag;
127117

@@ -133,14 +123,14 @@ export default class ServiceGenerator {
133123
}
134124

135125
// 用 tag 分组 paths, { [tag]: [pathMap, pathMap] }
136-
for (const pathKey in this.openAPIData.paths) {
126+
outerLoop: for (const pathKey in this.openAPIData.paths) {
137127
// 这里判断paths
138128
switch (priorityRule) {
139129
case PriorityRule.include: {
140-
// includePaths and includeTags is empty 没有任何include配置,直接跳过这个数组的所有元素
130+
// includePaths and includeTags is empty,直接跳过
141131
if (isEmpty(includeTags) && isEmpty(includePaths)) {
142132
this.log('priorityRule include need includeTags or includePaths');
143-
continue;
133+
break outerLoop;
144134
}
145135

146136
if (
@@ -158,19 +148,20 @@ export default class ServiceGenerator {
158148
break;
159149
}
160150
case PriorityRule.both: {
161-
// includePaths is empty 没有配置,直接跳过
151+
// includePaths and includeTags is empty直接跳过
162152
if (isEmpty(includeTags) && isEmpty(includePaths)) {
163-
continue;
153+
this.log('priorityRule both need includeTags or includePaths');
154+
break outerLoop;
164155
}
165156

166-
const inIncludePaths =
157+
const outIncludePaths =
167158
!isEmpty(includePaths) &&
168159
!this.validateRegexp(pathKey, includePaths);
169160
const inExcludePaths =
170161
!isEmpty(excludePaths) &&
171162
this.validateRegexp(pathKey, excludePaths);
172163

173-
if (inIncludePaths || inExcludePaths) {
164+
if (outIncludePaths || inExcludePaths) {
174165
continue;
175166
}
176167
break;
@@ -190,30 +181,20 @@ export default class ServiceGenerator {
190181
return;
191182
}
192183

193-
let tags = hookCustomFileNames(operationObject, pathKey, method);
194-
195-
if (!tags) {
196-
tags = getDefaultFileTag(operationObject, pathKey);
197-
}
184+
const tags = hookCustomFileNames(operationObject, pathKey, method);
198185

199186
// 这里判断tags
200187
tags.forEach((tag) => {
201188
const tagLowerCase = tag.toLowerCase();
202189

203190
if (priorityRule === PriorityRule.include) {
204-
// includeTags 为空, 不会匹配任何path,故跳过; includeTags 和 includePaths 同时为空则没意义,故跳过;
205-
if (
206-
isEmpty(includeTags) ||
207-
(isEmpty(includeTags) && isEmpty(includePaths))
208-
) {
191+
// includeTags 为空,不会匹配任何path,故跳过;
192+
if (isEmpty(includeTags)) {
209193
this.log('priorityRule include need includeTags or includePaths');
210194
return;
211195
}
212196

213-
if (
214-
!isEmpty(includeTags) &&
215-
!this.validateRegexp(tagLowerCase, includeTags)
216-
) {
197+
if (!this.validateRegexp(tagLowerCase, includeTags)) {
217198
return;
218199
}
219200
}
@@ -227,18 +208,18 @@ export default class ServiceGenerator {
227208
if (priorityRule === PriorityRule.both) {
228209
// includeTags is empty 没有配置,直接跳过
229210
if (isEmpty(includeTags)) {
230-
this.log('priorityRule include need includeTags or includePaths');
211+
this.log('priorityRule both need includeTags or includePaths');
231212
return;
232213
}
233214

234-
const inIncludeTags =
215+
const outIncludeTags =
235216
!isEmpty(includeTags) &&
236217
!this.validateRegexp(tagLowerCase, includeTags);
237218
const inExcludeTags =
238219
!isEmpty(excludeTags) &&
239220
this.validateRegexp(tagLowerCase, excludeTags);
240221

241-
if (inIncludeTags || inExcludeTags) {
222+
if (outIncludeTags || inExcludeTags) {
242223
return;
243224
}
244225
}
@@ -261,12 +242,6 @@ export default class ServiceGenerator {
261242
}
262243
}
263244

264-
public log(message: any) {
265-
if (this.config.enableLogging) {
266-
log(message);
267-
}
268-
}
269-
270245
public genFile() {
271246
try {
272247
globSync(`${this.config.serversPath}/**/*`)
@@ -402,33 +377,39 @@ export default class ServiceGenerator {
402377
private getInterfaceTPConfigs() {
403378
const schemas = this.openAPIData.components?.schemas;
404379
const lastTypes: Array<ITypeItem> = [];
405-
406380
const includeTags = this.config?.includeTags || [];
407-
const includePaths = this.config?.includePaths || [];
408381

409382
// 强行替换掉请求参数params的类型,生成方法对应的 xxxxParams 类型
410383
keys(this.openAPIData.paths).forEach((pathKey) => {
411384
const pathItem = this.openAPIData.paths[pathKey] as PathItemObject;
385+
412386
forEach(methods, (method) => {
413387
const operationObject = pathItem[method] as OperationObject;
388+
const hookCustomFileNames =
389+
this.config.hook?.customFileNames || getDefaultFileTag;
414390

415-
if (
416-
!operationObject ||
417-
isEmpty(includeTags) ||
418-
(isEmpty(includeTags) && isEmpty(includePaths)) ||
419-
isEmpty(operationObject.tags)
420-
) {
391+
if (!operationObject) {
421392
return;
422393
}
423-
// 筛选出 pathItem 包含的 $ref 对应的schema
394+
395+
const tags = hookCustomFileNames(operationObject, pathKey, method);
396+
397+
if (isEmpty(includeTags) || isEmpty(tags)) {
398+
return;
399+
}
400+
424401
const flag = this.validateRegexp(
425-
map(operationObject.tags, (tag) => tag.toLowerCase()),
402+
map(tags, (tag) => tag.toLowerCase()),
426403
includeTags
427404
);
428-
if (flag) {
429-
markAllowedSchema(JSON.stringify(pathItem), schemas);
405+
406+
if (!flag) {
407+
return;
430408
}
431409

410+
// 筛选出 pathItem 包含的 $ref 对应的schema
411+
markAllowedSchema(JSON.stringify(pathItem), this.openAPIData);
412+
432413
operationObject.parameters = operationObject.parameters?.filter(
433414
(item: ParameterObject) => item?.in !== `${parametersInsEnum.header}`
434415
);
@@ -1198,6 +1179,12 @@ export default class ServiceGenerator {
11981179
return refObject as T;
11991180
}
12001181

1182+
public log(message: string) {
1183+
if (this.config.enableLogging) {
1184+
log(message);
1185+
}
1186+
}
1187+
12011188
private generateInfoLog(): void {
12021189
this.log(`priorityRule: ${this.config?.priorityRule}`);
12031190

0 commit comments

Comments
 (0)