@@ -15,7 +15,7 @@ import {
15
15
} from 'oazapfts/lib/codegen/tscodegen' ;
16
16
import type { OpenAPIV3 } from 'openapi-types' ;
17
17
import { generateReactHooks } from './generators/react-hooks' ;
18
- import type { EndpointOverrides , GenerationOptions , OperationDefinition } from './types' ;
18
+ import type { EndpointMatcher , EndpointOverrides , GenerationOptions , OperationDefinition , TextMatcher } from './types' ;
19
19
import { capitalize , getOperationDefinitions , getV3Doc , isQuery as testIsQuery , removeUndefined } from './utils' ;
20
20
import type { ObjectPropertyDefinitions } from './codegen' ;
21
21
import { generateCreateApiCall , generateEndpointDefinition , generateImportNode } from './codegen' ;
@@ -33,22 +33,30 @@ function getOperationName({ verb, path, operation }: Pick<OperationDefinition, '
33
33
return _getOperationName ( verb , path , operation . operationId ) ;
34
34
}
35
35
36
- function patternMatches ( pattern ?: string | RegExp | ( string | RegExp ) [ ] ) {
36
+ function patternMatches ( pattern ?: TextMatcher ) {
37
37
const filters = Array . isArray ( pattern ) ? pattern : [ pattern ] ;
38
- return function matcher ( operationDefinition : OperationDefinition ) {
38
+ return function matcher ( operationName : string ) {
39
39
if ( ! pattern ) return true ;
40
- const operationName = getOperationName ( operationDefinition ) ;
41
40
return filters . some ( ( filter ) =>
42
41
typeof filter === 'string' ? filter === operationName : filter ?. test ( operationName )
43
42
) ;
44
43
} ;
45
44
}
46
45
46
+ function operationMatches ( pattern ?: EndpointMatcher ) {
47
+ const checkMatch = typeof pattern === 'function' ? pattern : patternMatches ( pattern ) ;
48
+ return function matcher ( operationDefinition : OperationDefinition ) {
49
+ if ( ! pattern ) return true ;
50
+ const operationName = getOperationName ( operationDefinition ) ;
51
+ return checkMatch ( operationName , operationDefinition ) ;
52
+ } ;
53
+ }
54
+
47
55
export function getOverrides (
48
56
operation : OperationDefinition ,
49
57
endpointOverrides ?: EndpointOverrides [ ]
50
58
) : EndpointOverrides | undefined {
51
- return endpointOverrides ?. find ( ( override ) => patternMatches ( override . pattern ) ( operation ) ) ;
59
+ return endpointOverrides ?. find ( ( override ) => operationMatches ( override . pattern ) ( operation ) ) ;
52
60
}
53
61
54
62
export async function generateApi (
@@ -70,7 +78,7 @@ export async function generateApi(
70
78
71
79
const apiGen = new ApiGenerator ( v3Doc , { } ) ;
72
80
73
- const operationDefinitions = getOperationDefinitions ( v3Doc ) . filter ( patternMatches ( filterEndpoints ) ) ;
81
+ const operationDefinitions = getOperationDefinitions ( v3Doc ) . filter ( operationMatches ( filterEndpoints ) ) ;
74
82
75
83
const resultFile = ts . createSourceFile (
76
84
'someFileName.ts' ,
0 commit comments