|
| 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 | +} |
0 commit comments