Skip to content

Commit 0a30ddd

Browse files
feat: 过滤条件默认大小写敏感,提供参数filterCaseInsensitive支持. 关闭 #467 (#471)
* feat: 过滤条件默认大小写敏感,提供参数filterCaseInsensitive支持. 关闭 #467 * docs: create changelog * chore: change set * chore: delete .changeset/open-llamas-brake.md --------- Co-authored-by: 故城 <[email protected]>
1 parent 50642a8 commit 0a30ddd

15 files changed

+2340
-23
lines changed

.changeset/few-elephants-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-ts-request": minor
3+
---
4+
5+
feat: 过滤条件默认大小写敏感,提供参数filterCaseInsensitive支持,关闭 #467

README-en_US.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ $ openapi --help
192192
-f, --full <boolean> full replacement (default: true)
193193
--enableLogging <boolean> open the log (default: false)
194194
--priorityRule <string> priority rule, include/exclude/both (default: "include")
195+
--filterCaseInsensitive <boolean> whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false)
195196
--includeTags <(string|RegExp)[]> generate code from include tags
196197
--includePaths <(string|RegExp)[]> generate code from include paths
197198
--excludeTags <(string|RegExp)[]> generate code from exclude tags
@@ -230,6 +231,7 @@ openapi -i ./spec.json -o ./apis
230231
| full | no | boolean | true | full replacement |
231232
| enableLogging | no | boolean | false | open the log |
232233
| priorityRule | no | string | 'include' | priority rule, include/exclude/both |
234+
| filterCaseInsensitive | no | boolean | false | whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false) | |
233235
| includeTags | no | (string\|RegExp)[] | - | generate code from include tags, priorityRule=include required |
234236
| includePaths | no | (string\|RegExp)[] | - | generate code from include paths |
235237
| excludeTags | no | (string\|RegExp)[] | - | generate code from exclude tags |

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ $ openapi --help
194194
-f, --full <boolean> full replacement (default: true)
195195
--enableLogging <boolean> open the log (default: false)
196196
--priorityRule <string> priority rule, include/exclude/both (default: "include")
197+
--filterCaseInsensitive <boolean> whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false)
197198
--includeTags <(string|RegExp)[]> generate code from include tags
198199
--includePaths <(string|RegExp)[]> generate code from include paths
199200
--excludeTags <(string|RegExp)[]> generate code from exclude tags
@@ -232,6 +233,7 @@ openapi --i ./spec.json --o ./apis
232233
| full || boolean | true | 是否全量替换 |
233234
| enableLogging || boolean | false | 是否开启日志 |
234235
| priorityRule || string | 'include' | 模式规则,可选include/exclude/both |
236+
| filterCaseInsensitive || boolean | false | 执行 includeTags、includePaths、excludeTags、excludePaths 过滤时是否忽略大小写 | |
235237
| includeTags || (string\|RegExp)[] | - | 根据指定的 tags 生成代码, priorityRule=include则必填 |
236238
| includePaths || (string\|RegExp)[] | - | 根据指定的 paths 生成代码 |
237239
| excludeTags || (string\|RegExp)[] | - | 根据指定的 tags 不生成代码 |

src/generator/serviceGenarator.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,20 @@ export default class ServiceGenerator {
202202
return;
203203
}
204204

205-
const tagLowerCase = tag.toLowerCase();
206-
207205
if (priorityRule === PriorityRule.include) {
208206
// includeTags 为空,不会匹配任何path,故跳过
209207
if (isEmpty(includeTags)) {
210208
this.log('priorityRule include need includeTags or includePaths');
211209
return;
212210
}
213211

214-
if (!this.validateRegexp(tagLowerCase, includeTags)) {
212+
if (!this.validateRegexp(tag, includeTags)) {
215213
return;
216214
}
217215
}
218216

219217
if (priorityRule === PriorityRule.exclude) {
220-
if (this.validateRegexp(tagLowerCase, excludeTags)) {
218+
if (this.validateRegexp(tag, excludeTags)) {
221219
return;
222220
}
223221
}
@@ -230,11 +228,9 @@ export default class ServiceGenerator {
230228
}
231229

232230
const outIncludeTags =
233-
!isEmpty(includeTags) &&
234-
!this.validateRegexp(tagLowerCase, includeTags);
231+
!isEmpty(includeTags) && !this.validateRegexp(tag, includeTags);
235232
const inExcludeTags =
236-
!isEmpty(excludeTags) &&
237-
this.validateRegexp(tagLowerCase, excludeTags);
233+
!isEmpty(excludeTags) && this.validateRegexp(tag, excludeTags);
238234

239235
if (outIncludeTags || inExcludeTags) {
240236
return;
@@ -491,12 +487,7 @@ export default class ServiceGenerator {
491487
}
492488

493489
const flag = this.validateRegexp(
494-
filter(
495-
map(tags, (tag) =>
496-
tag?.toLowerCase ? tag.toLowerCase() : undefined
497-
),
498-
(tag) => !!tag
499-
),
490+
filter(tags, (tag) => !!tag),
500491
includeTags
501492
);
502493

@@ -1561,7 +1552,9 @@ export default class ServiceGenerator {
15611552
// 提取匹配逻辑到单独的函数
15621553
private matches(item: string, reg: string | RegExp): boolean {
15631554
if (typeof reg === 'string') {
1564-
return minimatch(item, reg);
1555+
return minimatch(item, reg, {
1556+
nocase: this.config.filterCaseInsensitive,
1557+
});
15651558
} else if (reg instanceof RegExp) {
15661559
reg.lastIndex = 0; // 重置正则表达式的 lastIndex 属性
15671560

src/index.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ export type GenerateServiceProps = {
5353
* 优先规则, include(只允许include列表) | exclude(只排除exclude列表) | both(允许include列表,排除exclude列表)
5454
*/
5555
priorityRule?: IPriorityRule;
56+
57+
/**
58+
* includeTags、includePaths、excludeTags、excludePaths 过滤器执行时是否忽略大小写
59+
*/
60+
filterCaseInsensitive?: boolean;
5661
/**
5762
* 只解析归属于 tags 集合的 api 和 schema
5863
*/
@@ -319,9 +324,7 @@ export async function generateService({
319324
enableLogging: false,
320325
priorityRule,
321326
includeTags: includeTags
322-
? map(includeTags, (item) =>
323-
isString(item) ? item.toLowerCase() : item
324-
)
327+
? includeTags
325328
: priorityRule === PriorityRule.include ||
326329
priorityRule === PriorityRule.both
327330
? [/.*/g]
@@ -332,11 +335,7 @@ export async function generateService({
332335
priorityRule === PriorityRule.both
333336
? [/.*/g]
334337
: null,
335-
excludeTags: excludeTags
336-
? map(excludeTags, (item) =>
337-
isString(item) ? item.toLowerCase() : item
338-
)
339-
: null,
338+
excludeTags: excludeTags ? excludeTags : null,
340339
excludePaths: excludePaths ? excludePaths : null,
341340
requestOptionsType: '{[key: string]: unknown}',
342341
namespace: 'API',

0 commit comments

Comments
 (0)