Skip to content

Commit e79a7fb

Browse files
committed
wip on a new transformer
1 parent efbc449 commit e79a7fb

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { ComponentsObject, GlobalContext } from "../types.js";
2+
import { escObjKey, getEntries, getSchemaObjectComment, indent, tsReadonly } from "../utils.js";
3+
import transformResponseObject from "./response-object.js";
4+
import transformSchemaObject from "./schema-object.js";
5+
6+
export default function transformComponentsObjectToTypes(components: ComponentsObject, ctx: GlobalContext): string {
7+
let { indentLv } = ctx;
8+
const output: string[] = [];
9+
indentLv++;
10+
11+
// schemas
12+
if (components.schemas) {
13+
output.push(indent("schemas: {", indentLv));
14+
indentLv++;
15+
for (const [name, schemaObject] of getEntries(components.schemas, ctx.alphabetize, ctx.excludeDeprecated)) {
16+
const c = getSchemaObjectComment(schemaObject, indentLv);
17+
if (c) output.push(indent(c, indentLv));
18+
let key = escObjKey(name);
19+
if (ctx.immutableTypes || schemaObject.readOnly) key = tsReadonly(key);
20+
const schemaType = transformSchemaObject(schemaObject, {
21+
path: `#/components/schemas/${name}`,
22+
ctx: { ...ctx, indentLv: indentLv },
23+
});
24+
25+
output.push(indent(`${key}: ${schemaType};`, indentLv));
26+
}
27+
indentLv--;
28+
output.push(indent("};", indentLv));
29+
} else {
30+
output.push(indent("schemas: never;", indentLv));
31+
}
32+
33+
indentLv--;
34+
output.push(indent("}", indentLv));
35+
return output.join("\n");
36+
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { GlobalContext, OpenAPI3 } from "../types.js";
22
import transformComponentsObject from "./components-object.js";
3+
import transformComponentsObjectToTypes from "./components-types.js";
34
import transformPathsObject from "./paths-object.js";
45
import transformWebhooksObject from "./webhooks-object.js";
56

@@ -21,5 +22,11 @@ export function transformSchema(schema: OpenAPI3, ctx: GlobalContext): Record<st
2122
if (schema.components) output.components = transformComponentsObject(schema.components, ctx);
2223
else output.components = "";
2324

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

0 commit comments

Comments
 (0)