Skip to content

Commit 9755877

Browse files
authored
fix: generate type file loss (#114)
* fix: type生成错误情况
1 parent 2c70f78 commit 9755877

File tree

8 files changed

+122
-119
lines changed

8 files changed

+122
-119
lines changed

.changeset/olive-ways-destroy.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+
fix: generate type file loss

src/generator/serviceGenarator.ts

Lines changed: 69 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
entries,
77
filter,
88
forEach,
9-
intersection,
109
isArray,
1110
isEmpty,
1211
isFunction,
@@ -107,19 +106,20 @@ export default class ServiceGenerator {
107106
};
108107
this.generateInfoLog();
109108

110-
const includePaths = this.config?.includePaths || [];
111109
const includeTags = this.config?.includeTags || [];
112-
const excludePaths = this.config?.excludePaths || [];
110+
const includePaths = this.config?.includePaths || [];
113111
const excludeTags = this.config?.excludeTags || [];
112+
const excludePaths = this.config?.excludePaths || [];
114113

115114
const priorityRule: PriorityRule =
116115
PriorityRule[config.priorityRule as keyof typeof PriorityRule];
117116

118117
if (
119-
priorityRule === PriorityRule.include &&
120-
isEmpty(this.config?.includeTags) &&
121-
isEmpty(this.config?.includePaths)
118+
priorityRule === PriorityRule.both &&
119+
isEmpty(includeTags) &&
120+
isEmpty(includePaths)
122121
) {
122+
// 没意义,可以直接跳过
123123
this.log('priorityRule include need includeTags or includePaths');
124124
}
125125
const hookCustomFileNames =
@@ -145,49 +145,30 @@ export default class ServiceGenerator {
145145

146146
if (
147147
!isEmpty(includePaths) &&
148-
!includePaths.some((pathRule) =>
149-
typeof pathRule === 'string'
150-
? minimatch(pathKey, pathRule)
151-
: pathRule.test(pathKey)
152-
)
148+
!this.validateRegexp(pathKey, includePaths)
153149
) {
154150
continue;
155151
}
156152
break;
157153
}
158154
case PriorityRule.exclude: {
159-
if (
160-
excludePaths.some((pathRule) =>
161-
typeof pathRule === 'string'
162-
? minimatch(pathKey, pathRule)
163-
: pathRule.test(pathKey)
164-
)
165-
) {
155+
if (this.validateRegexp(pathKey, excludePaths)) {
166156
continue;
167157
}
168158
break;
169159
}
170160
case PriorityRule.both: {
171161
// includePaths is empty 没有配置,直接跳过
172162
if (isEmpty(includeTags) && isEmpty(includePaths)) {
173-
this.log('priorityRule include need includeTags or includePaths');
174163
continue;
175164
}
176165

177166
const inIncludePaths =
178167
!isEmpty(includePaths) &&
179-
!includePaths.some((path) =>
180-
typeof path === 'string'
181-
? minimatch(pathKey, path)
182-
: path.test(pathKey)
183-
);
168+
!this.validateRegexp(pathKey, includePaths);
184169
const inExcludePaths =
185170
!isEmpty(excludePaths) &&
186-
excludePaths.some((path) =>
187-
typeof path === 'string'
188-
? minimatch(pathKey, path)
189-
: path.test(pathKey)
190-
);
171+
this.validateRegexp(pathKey, excludePaths);
191172

192173
if (inIncludePaths || inExcludePaths) {
193174
continue;
@@ -231,24 +212,14 @@ export default class ServiceGenerator {
231212

232213
if (
233214
!isEmpty(includeTags) &&
234-
!includeTags.some((tagRule) =>
235-
typeof tagRule === 'string'
236-
? minimatch(tagLowerCase, tagRule)
237-
: tagRule.test(tagLowerCase)
238-
)
215+
!this.validateRegexp(tagLowerCase, includeTags)
239216
) {
240217
return;
241218
}
242219
}
243220

244221
if (priorityRule === PriorityRule.exclude) {
245-
if (
246-
excludeTags.some((tagRule) =>
247-
typeof tagRule === 'string'
248-
? minimatch(tagLowerCase, tagRule)
249-
: tagRule.test(tagLowerCase)
250-
)
251-
) {
222+
if (this.validateRegexp(tagLowerCase, excludeTags)) {
252223
return;
253224
}
254225
}
@@ -260,21 +231,14 @@ export default class ServiceGenerator {
260231
return;
261232
}
262233

263-
const inincludeTags =
234+
const inIncludeTags =
264235
!isEmpty(includeTags) &&
265-
!includeTags.some((tagRule) =>
266-
typeof tagRule === 'string'
267-
? minimatch(tagLowerCase, tagRule)
268-
: tagRule.test(tagLowerCase)
269-
);
236+
!this.validateRegexp(tagLowerCase, includeTags);
270237
const inExcludeTags =
271238
!isEmpty(excludeTags) &&
272-
excludeTags.some((tagRule) =>
273-
typeof tagRule === 'string'
274-
? minimatch(tagLowerCase, tagRule)
275-
: tagRule.test(tagLowerCase)
276-
);
277-
if (inincludeTags || inExcludeTags) {
239+
this.validateRegexp(tagLowerCase, excludeTags);
240+
241+
if (inIncludeTags || inExcludeTags) {
278242
return;
279243
}
280244
}
@@ -439,33 +403,30 @@ export default class ServiceGenerator {
439403
const schemas = this.openAPIData.components?.schemas;
440404
const lastTypes: Array<ITypeItem> = [];
441405

406+
const includeTags = this.config?.includeTags || [];
407+
const includePaths = this.config?.includePaths || [];
408+
442409
// 强行替换掉请求参数params的类型,生成方法对应的 xxxxParams 类型
443410
keys(this.openAPIData.paths).forEach((pathKey) => {
444411
const pathItem = this.openAPIData.paths[pathKey] as PathItemObject;
445412
forEach(methods, (method) => {
446413
const operationObject = pathItem[method] as OperationObject;
447414

448-
if (!operationObject) {
415+
if (
416+
!operationObject ||
417+
isEmpty(includeTags) ||
418+
(isEmpty(includeTags) && isEmpty(includePaths)) ||
419+
isEmpty(operationObject.tags)
420+
) {
449421
return;
450422
}
451-
452423
// 筛选出 pathItem 包含的 $ref 对应的schema
453-
if (
454-
!isEmpty(this.config?.includeTags) &&
455-
!isEmpty(operationObject.tags)
456-
) {
457-
if (
458-
!isEmpty(
459-
intersection(
460-
this.config.includeTags,
461-
map(operationObject.tags, (tag) => tag.toLowerCase())
462-
)
463-
)
464-
) {
465-
markAllowedSchema(JSON.stringify(pathItem), schemas);
466-
} else {
467-
return;
468-
}
424+
const flag = this.validateRegexp(
425+
map(operationObject.tags, (tag) => tag.toLowerCase()),
426+
includeTags
427+
);
428+
if (flag) {
429+
markAllowedSchema(JSON.stringify(pathItem), schemas);
469430
}
470431

471432
operationObject.parameters = operationObject.parameters?.filter(
@@ -1256,4 +1217,41 @@ export default class ServiceGenerator {
12561217
this.log(`excludePaths: ${this.config?.excludePaths.join(', ')}`);
12571218
}
12581219
}
1220+
1221+
/**
1222+
* 校验规则
1223+
* @param regexp 正则数组
1224+
* @param data 数据
1225+
*/
1226+
private validateRegexp(
1227+
data: string | string[],
1228+
regexp: (string | RegExp)[]
1229+
): boolean {
1230+
// 确保 data 是数组
1231+
const dataArray = Array.isArray(data) ? data : [data];
1232+
this.log(`"Data Array:", ${dataArray.join(',')}`);
1233+
this.log(`"Regexp Array:", ${regexp.join(',')}`);
1234+
1235+
return dataArray.some((item) => {
1236+
const result = regexp.some((reg) => this.matches(item, reg));
1237+
this.log(`"Item:", ${item}, "Matches:", ${result}`);
1238+
return result;
1239+
});
1240+
}
1241+
1242+
/**
1243+
*
1244+
* @param item 数组数据
1245+
* @param reg 规则
1246+
*/
1247+
// 提取匹配逻辑到单独的函数
1248+
private matches(item: string, reg: string | RegExp): boolean {
1249+
if (typeof reg === 'string') {
1250+
return minimatch(item, reg);
1251+
} else if (reg instanceof RegExp) {
1252+
reg.lastIndex = 0; // 重置正则表达式的 lastIndex 属性
1253+
return reg.test(item);
1254+
}
1255+
return false; // 对于其他类型,返回 false
1256+
}
12591257
}

src/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ComponentsObject,
88
OpenAPIObject,
99
OperationObject,
10+
PriorityRule,
1011
ReferenceObject,
1112
SchemaObject,
1213
} from './type';
@@ -223,6 +224,16 @@ export async function generateService({
223224
}
224225

225226
const requestImportStatement = getImportStatement(requestLibPath);
227+
228+
const priorityRule: PriorityRule =
229+
rest.priorityRule === undefined
230+
? PriorityRule.include
231+
: PriorityRule[rest.priorityRule as keyof typeof PriorityRule];
232+
233+
if (priorityRule === PriorityRule.include && includeTags === undefined) {
234+
includeTags = [/.*/g];
235+
}
236+
226237
const serviceGenerator = new ServiceGenerator(
227238
{
228239
schemaPath,

test/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ if(include(includeList)){
5252
| --- | --- | --- | --- | --- | --- |
5353
| [] | [] | [] | [] | [] = includeTag | includetag为空,跳过所有步骤 |
5454
| [] | ['/sys-a/\**','/sys-b/\**','/sys-c/\**'] | [] | [] | [] = includeTag | includetag为空,跳过所有步骤 |
55-
| ['*'] | [] | [] | [] | ['*'] = includeTag | |
56-
| ['*'] | ['/sys-a/\**','/sys-b/\**','/sys-c/\**'] | [] | [] | ['/sys-a/\**','/sys-b/\**','/sys-c/\**'] = includePaths | |
55+
| [/.*/g] | [] | [] | [] | [/.*/g] = includeTag | |
56+
| [/.*/g] | ['/sys-a/\**','/sys-b/\**','/sys-c/\**'] | [] | [] | ['/sys-a/\**','/sys-b/\**','/sys-c/\**'] = includePaths | |
5757
| ['sys-a'] | ['/sys-a/a1/aa1/\**'] | [] | [] | ['/sys-a/a1/aa1/aaa1','/sys-a/a1/aa1/aaa1/aaaa1'] | 通配符规则 |
5858
| ['sys-a','user-z'] | ['/**'] | [] | [] | ['/sys-a/\**','/user-z/\**'] | |
5959
| | | | | | |
@@ -70,10 +70,10 @@ if(!include(excludeList)){
7070

7171
| includeTag | includePaths | excludeTag | excludePaths | 结果 | 备注 |
7272
| --- | --- | --- | --- | --- | --- |
73-
| [] | [] | [] | [] | ['*'] | 没有排除项,生成所有 |
73+
| [] | [] | [] | [] | [/.*/g] | 没有排除项,生成所有 |
7474
| [] | [] | ['sys-a,'user-z'] | [] | ['/sys-b/\**', '/sys-c/\**', '/user-x/\**', '/user-y/\**'] | 全集与excludeTag的差集 |
7575
| [] | [] | [] | ['/sys-a/\**','/user-z/\**'] | ['/sys-b/\**', '/sys-c/\**', '/user-x/\**', '/user-y/\**'] | 全集与excludeTag的差集 |
76-
| [] | [] | ['*'] | [] | [] | 排除了所有,是空集 |
76+
| [] | [] | [/.*/g] | [] | [] | 排除了所有,是空集 |
7777
| [] | [] | ['sys-a','user-z'] | ['/sys-b/\**','/user-y/\**'] | ['/sys-c/\**', '/user-x/\**'] | 先排除tag,然后排除path |
7878

7979
# Both模式
@@ -96,7 +96,7 @@ if(include(includeList) && !include(excludeList)){
9696
| includeTag(为空会生成空数组) | includePaths | excludeTag | excludePaths | 结果 | 备注 |
9797
| --- | --- | --- | --- | --- | --- |
9898
| [] | [] | [] | [] | [] | |
99-
| ['*'] | [] | [] | [] | ['*'] | |
100-
| ['*'] | ['/sys-a/**'] | [] | ['/sys-a/a1/**'] | ['/sys-a/a1'] | |
101-
| ['*'] | ['/sys-a/**','/user-z/\**'] | [] | [/sys-c/\*,/user-z/*] | ['/user-z/z1'] | |
102-
| ['*'] | [] | ['sys-b','sys-c','user-x','user-y'] | ['/sys-a/**', '/user-z/z1/zz1/\**'] | ['/user-z/z1', '/user-z/z1/zz1'] | |
99+
| [/.*/g] | [] | [] | [] | [/.*/g] | |
100+
| [/.*/g] | ['/sys-a/**'] | [] | ['/sys-a/a1/**'] | ['/sys-a/a1'] | |
101+
| [/.*/g] | ['/sys-a/**','/user-z/\**'] | [] | [/sys-c/\*,/user-z/*] | ['/user-z/z1'] | |
102+
| [/.*/g] | [] | ['sys-b','sys-c','user-x','user-y'] | ['/sys-a/**', '/user-z/z1/zz1/\**'] | ['/user-z/z1', '/user-z/z1/zz1'] | |

test/bothTest.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const bothTest2 = async () =>
4141
requestLibPath: '../request',
4242
enableLogging: true, // 开启日志
4343
priorityRule: 'both',
44-
includeTags: ['*'],
44+
includeTags: [/.*/g],
4545
includePaths: [],
4646
excludeTags: [],
4747
excludePaths: [],
@@ -57,7 +57,7 @@ const bothTest3 = async () =>
5757
requestLibPath: '../request',
5858
enableLogging: true, // 开启日志
5959
priorityRule: 'both',
60-
includeTags: ['*'],
60+
includeTags: [/.*/g],
6161
includePaths: ['/sys-a/**'],
6262
excludeTags: [],
6363
excludePaths: ['/sys-a/a1/**'],
@@ -73,7 +73,7 @@ const bothTest4 = async () =>
7373
requestLibPath: '../request',
7474
enableLogging: true, // 开启日志
7575
priorityRule: 'both',
76-
includeTags: ['*'],
76+
includeTags: [/.*/g],
7777
includePaths: ['/sys-a/**', '/user-z/**'],
7878
excludeTags: ['sys-a'],
7979
excludePaths: ['/user-z/z1/**'],
@@ -89,7 +89,7 @@ const bothTest5 = async () =>
8989
requestLibPath: '../request',
9090
enableLogging: true, // 开启日志
9191
priorityRule: 'both',
92-
includeTags: ['*'],
92+
includeTags: [/.*/g],
9393
includePaths: [],
9494
excludeTags: ['sys-b', 'sys-c', 'user-x', 'user-y'],
9595
excludePaths: ['/sys-a/**', '/user-z/z1/zz1/**'],

test/excludeTest.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const excludeTest4 = async () =>
7575
priorityRule: 'exclude',
7676
includeTags: [],
7777
includePaths: [],
78-
excludeTags: ['*'],
78+
excludeTags: [/.*/g],
7979
excludePaths: [],
8080
});
8181

0 commit comments

Comments
 (0)