Skip to content

Commit c920fc2

Browse files
committed
it's working whoooo
1 parent e79a7fb commit c920fc2

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

packages/openapi-typescript/src/index.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { GlobalContext, OpenAPI3, OpenAPITSOptions, Subschema } from "./types.js";
1+
import type { ComponentsObject, GlobalContext, OpenAPI3, OpenAPITSOptions, Subschema } from "./types.js";
22
import type { Readable } from "node:stream";
33
import { URL } from "node:url";
44
import load, { resolveSchema, VIRTUAL_JSON_URL } from "./load.js";
@@ -12,6 +12,7 @@ import transformResponseObject from "./transform/response-object.js";
1212
import transformSchemaObject from "./transform/schema-object.js";
1313
import { error, escObjKey, getDefaultFetch, getEntries, getSchemaObjectComment, indent } from "./utils.js";
1414
import transformPathItemObject, { Method } from "./transform/path-item-object.js";
15+
import transformComponentsObjectToTypes from "./transform/components-types.js";
1516
export * from "./types.js"; // expose all types to consumers
1617

1718
const EMPTY_OBJECT_RE = /^\s*\{?\s*\}?\s*$/;
@@ -101,6 +102,8 @@ async function openapiTS(schema: string | URL | OpenAPI3 | Readable, options: Op
101102
if (options.inject) output.push(options.inject);
102103

103104
// 2c. root schema
105+
const types = transformComponentsObjectToTypes((allSchemas["."].schema as OpenAPI3).components!, ctx);
106+
104107
const rootTypes = transformSchema(allSchemas["."].schema as OpenAPI3, ctx);
105108
for (const k of Object.keys(rootTypes)) {
106109
if (rootTypes[k] && !EMPTY_OBJECT_RE.test(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(types);
257260
}
258261

259262
export default openapiTS;
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import type { ComponentsObject, GlobalContext } from "../types.js";
22
import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js";
3-
import transformResponseObject from "./response-object.js";
43
import transformSchemaObject from "./schema-object.js";
54

65
export default function transformComponentsObjectToTypes(components: ComponentsObject, ctx: GlobalContext): string {
7-
let { indentLv } = ctx;
6+
const { indentLv } = ctx;
87
const output: string[] = [];
9-
indentLv++;
108

119
// schemas
1210
if (components.schemas) {
13-
output.push(indent("schemas: {", indentLv));
14-
indentLv++;
1511
for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) {
1612
const c = getSchemaObjectComment(schemaObject, indentLv);
1713
if (c) output.push(indent(c, indentLv));
@@ -22,15 +18,11 @@ export default function transformComponentsObjectToTypes(components: ComponentsO
2218
ctx: { ...ctx, indentLv: indentLv },
2319
});
2420

25-
output.push(indent(`${key}: ${schemaType};`, indentLv));
21+
output.push(indent(`export type ${key} = ${schemaType};\n`, indentLv));
2622
}
27-
indentLv--;
28-
output.push(indent("};", indentLv));
2923
} else {
3024
output.push(indent("schemas: never;", indentLv));
3125
}
3226

33-
indentLv--;
34-
output.push(indent("}", indentLv));
3527
return output.join("\n");
3628
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,5 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record<st
2222
if (schema.components) output.components = transformComponentsObject(schema.components, ctx);
2323
else output.components = "";
2424

25-
// types
26-
if (schema.components) output.types = transformComponentsObjectToTypes(schema.components, ctx);
27-
else output.types = "";
28-
29-
console.log(output.types);
30-
3125
return output;
3226
}

0 commit comments

Comments
 (0)