Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/few-elephants-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-ts-request": minor
---

feat: 过滤条件默认大小写敏感,提供参数filterCaseInsensitive支持,关闭 #467
2 changes: 2 additions & 0 deletions README-en_US.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ $ openapi --help
-f, --full <boolean> full replacement (default: true)
--enableLogging <boolean> open the log (default: false)
--priorityRule <string> priority rule, include/exclude/both (default: "include")
--filterCaseInsensitive <boolean> whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false)
--includeTags <(string|RegExp)[]> generate code from include tags
--includePaths <(string|RegExp)[]> generate code from include paths
--excludeTags <(string|RegExp)[]> generate code from exclude tags
Expand Down Expand Up @@ -230,6 +231,7 @@ openapi -i ./spec.json -o ./apis
| full | no | boolean | true | full replacement |
| enableLogging | no | boolean | false | open the log |
| priorityRule | no | string | 'include' | priority rule, include/exclude/both |
| filterCaseInsensitive | no | boolean | false | whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false) | |
| includeTags | no | (string\|RegExp)[] | - | generate code from include tags, priorityRule=include required |
| includePaths | no | (string\|RegExp)[] | - | generate code from include paths |
| excludeTags | no | (string\|RegExp)[] | - | generate code from exclude tags |
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ $ openapi --help
-f, --full <boolean> full replacement (default: true)
--enableLogging <boolean> open the log (default: false)
--priorityRule <string> priority rule, include/exclude/both (default: "include")
--filterCaseInsensitive <boolean> whether to perform a case-insensitive match with includeTags, includePaths, excludeTags, excludePaths filters. (default: false)
--includeTags <(string|RegExp)[]> generate code from include tags
--includePaths <(string|RegExp)[]> generate code from include paths
--excludeTags <(string|RegExp)[]> generate code from exclude tags
Expand Down Expand Up @@ -232,6 +233,7 @@ openapi --i ./spec.json --o ./apis
| full | 否 | boolean | true | 是否全量替换 |
| enableLogging | 否 | boolean | false | 是否开启日志 |
| priorityRule | 否 | string | 'include' | 模式规则,可选include/exclude/both |
| filterCaseInsensitive | 否 | boolean | false | 执行 includeTags、includePaths、excludeTags、excludePaths 过滤时是否忽略大小写 | |
| includeTags | 否 | (string\|RegExp)[] | - | 根据指定的 tags 生成代码, priorityRule=include则必填 |
| includePaths | 否 | (string\|RegExp)[] | - | 根据指定的 paths 生成代码 |
| excludeTags | 否 | (string\|RegExp)[] | - | 根据指定的 tags 不生成代码 |
Expand Down
23 changes: 8 additions & 15 deletions src/generator/serviceGenarator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,20 @@ export default class ServiceGenerator {
return;
}

const tagLowerCase = tag.toLowerCase();

if (priorityRule === PriorityRule.include) {
// includeTags 为空,不会匹配任何path,故跳过
if (isEmpty(includeTags)) {
this.log('priorityRule include need includeTags or includePaths');
return;
}

if (!this.validateRegexp(tagLowerCase, includeTags)) {
if (!this.validateRegexp(tag, includeTags)) {
return;
}
}

if (priorityRule === PriorityRule.exclude) {
if (this.validateRegexp(tagLowerCase, excludeTags)) {
if (this.validateRegexp(tag, excludeTags)) {
return;
}
}
Expand All @@ -230,11 +228,9 @@ export default class ServiceGenerator {
}

const outIncludeTags =
!isEmpty(includeTags) &&
!this.validateRegexp(tagLowerCase, includeTags);
!isEmpty(includeTags) && !this.validateRegexp(tag, includeTags);
const inExcludeTags =
!isEmpty(excludeTags) &&
this.validateRegexp(tagLowerCase, excludeTags);
!isEmpty(excludeTags) && this.validateRegexp(tag, excludeTags);

if (outIncludeTags || inExcludeTags) {
return;
Expand Down Expand Up @@ -491,12 +487,7 @@ export default class ServiceGenerator {
}

const flag = this.validateRegexp(
filter(
map(tags, (tag) =>
tag?.toLowerCase ? tag.toLowerCase() : undefined
),
(tag) => !!tag
),
filter(tags, (tag) => !!tag),
includeTags
);

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

Expand Down
15 changes: 7 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export type GenerateServiceProps = {
* 优先规则, include(只允许include列表) | exclude(只排除exclude列表) | both(允许include列表,排除exclude列表)
*/
priorityRule?: IPriorityRule;

/**
* includeTags、includePaths、excludeTags、excludePaths 过滤器执行时是否忽略大小写
*/
filterCaseInsensitive?: boolean;
/**
* 只解析归属于 tags 集合的 api 和 schema
*/
Expand Down Expand Up @@ -319,9 +324,7 @@ export async function generateService({
enableLogging: false,
priorityRule,
includeTags: includeTags
? map(includeTags, (item) =>
isString(item) ? item.toLowerCase() : item
)
? includeTags
: priorityRule === PriorityRule.include ||
priorityRule === PriorityRule.both
? [/.*/g]
Expand All @@ -332,11 +335,7 @@ export async function generateService({
priorityRule === PriorityRule.both
? [/.*/g]
: null,
excludeTags: excludeTags
? map(excludeTags, (item) =>
isString(item) ? item.toLowerCase() : item
)
: null,
excludeTags: excludeTags ? excludeTags : null,
excludePaths: excludePaths ? excludePaths : null,
requestOptionsType: '{[key: string]: unknown}',
namespace: 'API',
Expand Down
Loading