@@ -50,6 +50,8 @@ export default function generateTypesV3(
50
50
}
51
51
}
52
52
53
+ const operations : Record < string , OpenAPI3Operation > = { } ;
54
+
53
55
// propertyMapper
54
56
const propertyMapped = options
55
57
? propertyMapper ( components . schemas , options . propertyMapper )
@@ -211,6 +213,52 @@ export default function generateTypesV3(
211
213
return output ;
212
214
}
213
215
216
+ function transformOperation ( operation : OpenAPI3Operation ) : string {
217
+ let output = "" ;
218
+ output += `{\n` ;
219
+
220
+ // handle operation parameters
221
+ if ( operation . parameters ) {
222
+ output += transformParameters ( operation . parameters ) ;
223
+ }
224
+
225
+ // handle requestBody
226
+ if ( operation . requestBody ) {
227
+ output += `requestBody: {\n` ;
228
+ Object . entries ( operation . requestBody . content ) . forEach (
229
+ ( [ contentType , { schema } ] ) => {
230
+ output += `"${ contentType } ": ${ transform ( schema ) } ;\n` ;
231
+ }
232
+ ) ;
233
+ output += `}\n` ;
234
+ }
235
+
236
+ // handle responses
237
+ output += `responses: {\n` ;
238
+ Object . entries ( operation . responses ) . forEach ( ( [ statusCode , response ] ) => {
239
+ if ( response . description ) output += comment ( response . description ) ;
240
+ if ( ! response . content || ! Object . keys ( response . content ) . length ) {
241
+ const type =
242
+ statusCode === "204" || Math . floor ( + statusCode / 100 ) === 3
243
+ ? "never"
244
+ : "unknown" ;
245
+ output += `"${ statusCode } ": ${ type } ;\n` ;
246
+ return ;
247
+ }
248
+ output += `"${ statusCode } ": {\n` ;
249
+ Object . entries ( response . content ) . forEach (
250
+ ( [ contentType , encodedResponse ] ) => {
251
+ output += `"${ contentType } ": ${ transform ( encodedResponse . schema ) } ;\n` ;
252
+ }
253
+ ) ;
254
+ output += `}\n` ;
255
+ } ) ;
256
+ output += `}\n` ;
257
+ output += `}\n` ;
258
+
259
+ return output ;
260
+ }
261
+
214
262
function transformPaths ( paths : OpenAPI3Paths ) : string {
215
263
let output = "" ;
216
264
Object . entries ( paths ) . forEach ( ( [ path , methods ] ) => {
@@ -220,51 +268,16 @@ export default function generateTypesV3(
220
268
// skip the parameters "method" for shared parameters - we'll handle it later
221
269
if ( method !== "parameters" ) {
222
270
operation = operation as OpenAPI3Operation ;
223
- if ( operation . description ) output += comment ( operation . description ) ;
224
- output += `"${ method } ": {\n` ;
225
-
226
- // handle operation parameters
227
- if ( operation . parameters ) {
228
- output += transformParameters ( operation . parameters ) ;
229
- }
230
271
231
- // handle requestBody
232
- if ( operation . requestBody ) {
233
- output += `requestBody: {\n` ;
234
- Object . entries ( operation . requestBody . content ) . forEach (
235
- ( [ contentType , { schema } ] ) => {
236
- output += `"${ contentType } ": ${ transform ( schema ) } ;\n` ;
237
- }
238
- ) ;
239
- output += `}\n` ;
272
+ if ( operation . operationId ) {
273
+ output += `"${ method } ": operations["${ operation . operationId } "];\n` ;
274
+ operations [ operation . operationId ] = operation ;
275
+ } else {
276
+ if ( operation . description ) output += comment ( operation . description ) ;
277
+ output += `"${ method } ": ${ transformOperation (
278
+ operation as OpenAPI3Operation
279
+ ) } `;
240
280
}
241
-
242
- // handle responses
243
- output += `responses: {\n` ;
244
- Object . entries ( operation . responses ) . forEach (
245
- ( [ statusCode , response ] ) => {
246
- if ( response . description ) output += comment ( response . description ) ;
247
- if ( ! response . content || ! Object . keys ( response . content ) . length ) {
248
- const type =
249
- statusCode === "204" || Math . floor ( + statusCode / 100 ) === 3
250
- ? "never"
251
- : "unknown" ;
252
- output += `"${ statusCode } ": ${ type } ;\n` ;
253
- return ;
254
- }
255
- output += `"${ statusCode } ": {\n` ;
256
- Object . entries ( response . content ) . forEach (
257
- ( [ contentType , encodedResponse ] ) => {
258
- output += `"${ contentType } ": ${ transform (
259
- encodedResponse . schema
260
- ) } ;\n`;
261
- }
262
- ) ;
263
- output += `}\n` ;
264
- }
265
- ) ;
266
- output += `}\n` ;
267
- output += `}\n` ;
268
281
}
269
282
} ) ;
270
283
@@ -295,6 +308,16 @@ export default function generateTypesV3(
295
308
` ;
296
309
}
297
310
311
+ finalOutput += "export interface operations {\n" ;
312
+ for ( const [ operationId , operation ] of Object . entries ( operations ) ) {
313
+ if ( operation . description ) finalOutput += comment ( operation . description ) ;
314
+ finalOutput += `"${ operationId } ": ${ transformOperation (
315
+ operation as OpenAPI3Operation
316
+ ) } `;
317
+ }
318
+ // close operations wrapper
319
+ finalOutput += "\n}\n\n" ;
320
+
298
321
finalOutput += "export interface components {\n" ;
299
322
300
323
if ( components . parameters && Object . keys ( components . parameters ) . length ) {
0 commit comments