@@ -2,18 +2,22 @@ import fs from "fs/promises";
22import { OpenAPIV3_1 } from "openapi-types" ;
33import argv from "yargs-parser" ;
44
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+ }
610 const paramParts = ref . split ( "/" ) ;
711 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
913 while ( true ) {
1014 const part = paramParts . shift ( ) ;
1115 if ( ! part ) {
1216 break ;
1317 }
14- param = param [ part ] ;
18+ foundObj = foundObj [ part ] ;
1519 }
16- return param ;
20+ return foundObj as T ;
1721}
1822
1923async function main ( ) {
@@ -32,6 +36,7 @@ async function main() {
3236 operationId : string ;
3337 requiredParams : boolean ;
3438 tag : string ;
39+ hasResponseBody : boolean ;
3540 } [ ] = [ ] ;
3641
3742 const openapi = JSON . parse ( specFile ) as OpenAPIV3_1 . Document ;
@@ -44,13 +49,27 @@ async function main() {
4449 }
4550
4651 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+ }
4770
4871 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 ) ;
5473 if ( paramObject . in === "path" ) {
5574 requiredParams = true ;
5675 }
@@ -61,27 +80,41 @@ async function main() {
6180 method : method . toUpperCase ( ) ,
6281 operationId : operation . operationId || "" ,
6382 requiredParams,
83+ hasResponseBody,
6484 tag : operation . tags [ 0 ] ,
6585 } ) ;
6686 }
6787 }
6888
6989 const operationOutput = operations
7090 . map ( ( operation ) => {
71- const { operationId, method, path, requiredParams } = operation ;
91+ const { operationId, method, path, requiredParams, hasResponseBody } = operation ;
7292 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+ ` : `` } }
7696` ;
7797 } )
7898 . join ( "\n" ) ;
7999
80100 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" ) ;
85118
86119 await fs . writeFile ( file , output , "utf8" ) ;
87120}
0 commit comments