@@ -3,6 +3,13 @@ import type { OpenApi } from '../interfaces/OpenApi';
3
3
import { getModel } from './getModel' ;
4
4
import { reservedWords } from './getOperationParameterName' ;
5
5
import { getType } from './getType' ;
6
+ import { getOperationParameters } from './getOperationParameters' ;
7
+ import { unique } from '../../../utils/unique' ;
8
+ import { getOperation } from './getOperation' ;
9
+ import { OperationParameter } from '../../../client/interfaces/OperationParameter' ;
10
+ import { OpenApiSchema } from '../interfaces/OpenApiSchema' ;
11
+ import { Dictionary } from '../../../utils/types' ;
12
+ import { OpenApiParameter } from '../interfaces/OpenApiParameter' ;
6
13
7
14
export const getModels = ( openApi : OpenApi ) : Model [ ] => {
8
15
const models : Model [ ] = [ ] ;
@@ -16,5 +23,65 @@ export const getModels = (openApi: OpenApi): Model[] => {
16
23
}
17
24
}
18
25
}
26
+
27
+ /**
28
+ * Generate query param type from x-codegen.
29
+ * Same discovery mechanism as getServices.ts
30
+ */
31
+ for ( const url in openApi . paths ) {
32
+ if ( openApi . paths . hasOwnProperty ( url ) ) {
33
+ // Grab path and parse any global path parameters
34
+ const path = openApi . paths [ url ] ;
35
+ const pathParams = getOperationParameters ( openApi , path . parameters || [ ] ) ;
36
+
37
+ // Parse all the methods for this path
38
+ for ( const method in path ) {
39
+ if ( path . hasOwnProperty ( method ) ) {
40
+ switch ( method ) {
41
+ case 'get' :
42
+ case 'put' :
43
+ case 'post' :
44
+ case 'delete' :
45
+ case 'options' :
46
+ case 'head' :
47
+ case 'patch' :
48
+ // Each method contains an OpenAPI operation, we parse the operation
49
+ const op = path [ method ] ! ;
50
+ const tags = op . tags ?. length ? op . tags . filter ( unique ) : [ 'Default' ] ;
51
+ tags . forEach ( tag => {
52
+ const operation = getOperation ( openApi , url , method , tag , op , pathParams ) ;
53
+ if ( operation . codegen . queryParams ) {
54
+ const definition = getDefinitionFromParametersQuery ( operation . parametersQuery ) ;
55
+ const model = getModel ( openApi , definition , true , operation . codegen . queryParams ) ;
56
+ models . push ( model ) ;
57
+ }
58
+ } ) ;
59
+ break ;
60
+ }
61
+ }
62
+ }
63
+ }
64
+ }
65
+
19
66
return models ;
20
67
} ;
68
+
69
+ const getDefinitionFromParametersQuery = ( parametersQuery : OperationParameter [ ] ) : OpenApiSchema => {
70
+ const required = parametersQuery . filter ( parameter => parameter . spec . required ) . map ( parameter => parameter . name ) ;
71
+ const properties : Dictionary < OpenApiSchema > = { } ;
72
+ for ( const parameter of parametersQuery ) {
73
+ const spec = parameter . spec as OpenApiParameter ;
74
+ properties [ parameter . name ] = Object . assign (
75
+ { ...spec . schema } ,
76
+ {
77
+ description : spec . description ,
78
+ deprecated : spec . deprecated ,
79
+ }
80
+ ) ;
81
+ }
82
+ return {
83
+ type : 'object' ,
84
+ required,
85
+ properties,
86
+ } ;
87
+ } ;
0 commit comments