Skip to content

Commit 560deff

Browse files
committed
maxi wip
1 parent 6d79df0 commit 560deff

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

packages/openapi-typescript/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
102102

103103
// 2c. root schema
104104
const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx);
105+
const allTypes = rootTypes.allTypes;
106+
delete rootTypes.allTypes;
107+
105108
for (const k of Object.keys(rootTypes)) {
106109
if (rootTypes[k] && !EMPTY_OBJECT_RE.test(rootTypes[k])) {
107110
output.push(options.exportType ? `export type ${k} = ${rootTypes[k]};` : `export interface ${k} ${rootTypes[k]}`, "");
@@ -253,7 +256,7 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
253256
output.splice(1, 0, "/** WithRequired type helpers */", "type WithRequired<T, K extends keyof T> = T & { [P in K]-?: T[P] };", "");
254257
}
255258

256-
return output.join("\n");
259+
return output.join("\n").concat(`;\n${allTypes}`);
257260
}
258261

259262
export default openapiTS;

packages/openapi-typescript/src/transform/components-object.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import fs from "node:fs";
12
import type { ComponentsObject, GlobalContext } from "../types.js";
23
import { escObjKey, getEntries, getSchemaObjectComment, indent, tsOptionalProperty, tsReadonly } from "../utils.js";
34
import transformHeaderObject from "./header-object.js";
@@ -7,7 +8,7 @@ import transformRequestBodyObject from "./request-body-object.js";
78
import transformResponseObject from "./response-object.js";
89
import transformSchemaObject from "./schema-object.js";
910

10-
export default function transformComponentsObject(components: ComponentsObject, ctx: GlobalContext): string {
11+
export default function transformComponentsObject(components: ComponentsObject, ctx: GlobalContext): { output: string; allTypes: string } {
1112
let { indentLv } = ctx;
1213
const output: string[] = ["{"];
1314
indentLv++;
@@ -181,7 +182,9 @@ export default function transformComponentsObject(components: ComponentsObject,
181182
output.push(indent("pathItems: never;", indentLv));
182183
}
183184

185+
fs.writeFileSync(`${process.cwd()}/types.ts`, allTypes);
186+
184187
indentLv--;
185188
output.push(indent("}", indentLv));
186-
return output.join("\n");
189+
return { output: output.join("\n"), allTypes };
187190
}

packages/openapi-typescript/src/transform/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record<st
1818
else output.webhooks = "";
1919

2020
// components
21-
if (schema.components) output.components = transformComponentsObject(schema.components, ctx);
22-
else output.components = "";
21+
if (schema.components) {
22+
const componentObjects = transformComponentsObject(schema.components, ctx);
23+
24+
output.components = componentObjects.output;
25+
output.allTypes = componentObjects.allTypes;
26+
} else {
27+
output.components = "";
28+
}
2329

2430
return output;
2531
}

0 commit comments

Comments
 (0)