Skip to content

Commit 760b61c

Browse files
committed
feat: export operations interface
1 parent a44e986 commit 760b61c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/types/OpenAPI3.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface OpenAPI3Paths {
1515
}
1616

1717
export interface OpenAPI3Operation {
18+
operationId?: string;
1819
description?: string;
1920
parameters?: Parameter[];
2021
requestBody?: OpenAPI3RequestBody;
@@ -57,6 +58,9 @@ export interface OpenAPI3 {
5758
openapi: string;
5859
paths?: OpenAPI3Paths; // technically required by spec, but this library tries to be lenient
5960
components?: OpenAPI3Components;
61+
operations?: {
62+
[key: string]: OpenAPI3Operation;
63+
};
6064
[key: string]: any; // handle other properties beyond this library’s concern
6165
}
6266

src/v3.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ export default function generateTypesV3(
5050
}
5151
}
5252

53+
const operations: Record<string, OpenAPI3Operation> = {};
54+
5355
// propertyMapper
5456
const propertyMapped = options
5557
? propertyMapper(components.schemas, options.propertyMapper)
@@ -211,13 +213,9 @@ export default function generateTypesV3(
211213
return output;
212214
}
213215

214-
function transformOperation(
215-
method: string,
216-
operation: OpenAPI3Operation
217-
): string {
216+
function transformOperation(operation: OpenAPI3Operation): string {
218217
let output = "";
219-
if (operation.description) output += comment(operation.description);
220-
output += `"${method}": {\n`;
218+
output += `{\n`;
221219

222220
// handle operation parameters
223221
if (operation.parameters) {
@@ -269,7 +267,17 @@ export default function generateTypesV3(
269267
Object.entries(methods).forEach(([method, operation]) => {
270268
// skip the parameters "method" for shared parameters - we'll handle it later
271269
if (method !== "parameters") {
272-
output += transformOperation(method, operation as OpenAPI3Operation);
270+
operation = operation as OpenAPI3Operation;
271+
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+
)}`;
280+
}
273281
}
274282
});
275283

@@ -300,6 +308,16 @@ export default function generateTypesV3(
300308
`;
301309
}
302310

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+
303321
finalOutput += "export interface components {\n";
304322

305323
if (components.parameters && Object.keys(components.parameters).length) {

0 commit comments

Comments
 (0)