komga/next-ui/openapi-generator.mts
2025-11-28 16:05:18 +08:00

26 lines
838 B
TypeScript

import fs from "node:fs";
import openapiTS, { astToString } from "openapi-typescript";
import ts from "typescript";
// From https://openapi-ts.dev/node
// We use the Node.js API as the CLI does not support Date types
const mySchema = new URL("../komga/docs/openapi.json", import.meta.url)
const DATE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier("Date")); // `Date`
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull()); // `null`
const ast = await openapiTS(mySchema, {
transform(schemaObject) {
if (schemaObject.format === "date-time") {
return schemaObject.nullable
? ts.factory.createUnionTypeNode([DATE, NULL])
: DATE;
}
},
});
const contents = astToString(ast);
// (optional) write to file
fs.writeFileSync("./src/generated/openapi/komga.d.ts", contents);