@@ -2,18 +2,22 @@ import fs from "fs/promises";
2
2
import { OpenAPIV3_1 } from "openapi-types" ;
3
3
import argv from "yargs-parser" ;
4
4
5
- function findParamFromRef ( ref : string , openapi : OpenAPIV3_1 . Document ) : OpenAPIV3_1 . ParameterObject {
5
+ function findObjectFromRef < T > ( obj : T | OpenAPIV3_1 . ReferenceObject , openapi : OpenAPIV3_1 . Document ) : T {
6
+ const ref = ( obj as OpenAPIV3_1 . ReferenceObject ) . $ref ;
7
+ if ( ref === undefined ) {
8
+ return obj as T ;
9
+ }
6
10
const paramParts = ref . split ( "/" ) ;
7
11
paramParts . shift ( ) ; // Remove the first part which is always '#'
8
- let param : any = openapi ; // eslint-disable-line @typescript-eslint/no-explicit-any
12
+ let foundObj : any = openapi ; // eslint-disable-line @typescript-eslint/no-explicit-any
9
13
while ( true ) {
10
14
const part = paramParts . shift ( ) ;
11
15
if ( ! part ) {
12
16
break ;
13
17
}
14
- param = param [ part ] ;
18
+ foundObj = foundObj [ part ] ;
15
19
}
16
- return param ;
20
+ return foundObj as T ;
17
21
}
18
22
19
23
async function main ( ) {
@@ -32,6 +36,7 @@ async function main() {
32
36
operationId : string ;
33
37
requiredParams : boolean ;
34
38
tag : string ;
39
+ hasResponseBody : boolean ;
35
40
} [ ] = [ ] ;
36
41
37
42
const openapi = JSON . parse ( specFile ) as OpenAPIV3_1 . Document ;
@@ -44,13 +49,27 @@ async function main() {
44
49
}
45
50
46
51
let requiredParams = ! ! operation . requestBody ;
52
+ let hasResponseBody = false ;
53
+ for ( const code in operation . responses ) {
54
+ try {
55
+ const httpCode = parseInt ( code , 10 ) ;
56
+ if ( httpCode >= 200 && httpCode < 300 ) {
57
+ const response = operation . responses [ code ] ;
58
+ const responseObject = findObjectFromRef ( response , openapi ) ;
59
+ if ( responseObject . content ) {
60
+ for ( const contentType in responseObject . content ) {
61
+ const content = responseObject . content [ contentType ] ;
62
+ hasResponseBody = ! ! content . schema ;
63
+ }
64
+ }
65
+ }
66
+ } catch {
67
+ continue ;
68
+ }
69
+ }
47
70
48
71
for ( const param of operation . parameters || [ ] ) {
49
- const ref = ( param as OpenAPIV3_1 . ReferenceObject ) . $ref as string | undefined ;
50
- let paramObject : OpenAPIV3_1 . ParameterObject = param as OpenAPIV3_1 . ParameterObject ;
51
- if ( ref ) {
52
- paramObject = findParamFromRef ( ref , openapi ) ;
53
- }
72
+ const paramObject = findObjectFromRef ( param , openapi ) ;
54
73
if ( paramObject . in === "path" ) {
55
74
requiredParams = true ;
56
75
}
@@ -61,27 +80,41 @@ async function main() {
61
80
method : method . toUpperCase ( ) ,
62
81
operationId : operation . operationId || "" ,
63
82
requiredParams,
83
+ hasResponseBody,
64
84
tag : operation . tags [ 0 ] ,
65
85
} ) ;
66
86
}
67
87
}
68
88
69
89
const operationOutput = operations
70
90
. map ( ( operation ) => {
71
- const { operationId, method, path, requiredParams } = operation ;
91
+ const { operationId, method, path, requiredParams, hasResponseBody } = operation ;
72
92
return `async ${ operationId } (options${ requiredParams ? "" : "?" } : FetchOptions<operations["${ operationId } "]>) {
73
- const { data } = await this.client.${ method } ("${ path } ", options);
74
- return data;
75
- }
93
+ ${ hasResponseBody ? ` const { data } = ` : `` } await this.client.${ method } ("${ path } ", options);
94
+ ${ hasResponseBody ? ` return data;
95
+ ` : `` } }
76
96
` ;
77
97
} )
78
98
. join ( "\n" ) ;
79
99
80
100
const templateFile = ( await fs . readFile ( file , "utf8" ) ) as string ;
81
- const output = templateFile . replace (
82
- / \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. \n .* \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. / g,
83
- operationOutput
84
- ) ;
101
+ const templateLines = templateFile . split ( "\n" ) ;
102
+ let outputLines : string [ ] = [ ] ;
103
+ let addLines = true ;
104
+ for ( const line of templateLines ) {
105
+ if ( line . includes ( "DO NOT EDIT. This is auto-generated code." ) ) {
106
+ addLines = ! addLines ;
107
+ outputLines . push ( line ) ;
108
+ if ( ! addLines ) {
109
+ outputLines . push ( operationOutput ) ;
110
+ }
111
+ continue ;
112
+ }
113
+ if ( addLines ) {
114
+ outputLines . push ( line ) ;
115
+ }
116
+ }
117
+ const output = outputLines . join ( "\n" ) ;
85
118
86
119
await fs . writeFile ( file , output , "utf8" ) ;
87
120
}
0 commit comments