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}
0 commit comments