handle File in openapi generator

This commit is contained in:
Gauthier Roebroeck 2025-10-14 13:58:26 +08:00
parent 56ca0af6b3
commit 1ba5d7124a
2 changed files with 35 additions and 21 deletions

View file

@ -1,26 +1,36 @@
import fs from "node:fs";
import openapiTS, { astToString } from "openapi-typescript";
import ts from "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 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 DATE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Date')) // `Date`
const FILE = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('File')) // `File`
const BLOB = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Blob')) // `Blob`
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;
transform(schemaObject, metadata) {
if (schemaObject.format === 'date-time') {
return schemaObject.nullable ? ts.factory.createUnionTypeNode([DATE, NULL]) : DATE
}
if (schemaObject.format === 'binary') {
console.log(schemaObject)
console.log(metadata)
if (metadata.path.includes('multipart~1form-data')) {
return schemaObject.nullable ? ts.factory.createUnionTypeNode([FILE, NULL]) : FILE
}
if (metadata.path.includes('application~1octet-stream')) {
return schemaObject.nullable ? ts.factory.createUnionTypeNode([BLOB, NULL]) : BLOB
}
}
},
});
})
const contents = astToString(ast);
const contents = astToString(ast)
// (optional) write to file
fs.writeFileSync("./src/generated/openapi/komga.d.ts", contents);
fs.writeFileSync('./src/generated/openapi/komga.d.ts', contents)

View file

@ -3046,7 +3046,11 @@ export interface components {
* @example Version 1.9.0 contains the second feature drop for Ebooks support.
*/
summary?: string;
/** @description Tags describing the item */
/**
* @description Tags describing the item
* @example upgrade
* @example komga
*/
tags: string[];
/**
* @description Plain text title
@ -5469,7 +5473,7 @@ export interface operations {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
file: File;
};
};
};
@ -6155,7 +6159,7 @@ export interface operations {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
file: File;
};
};
};
@ -7242,9 +7246,9 @@ export interface operations {
};
requestBody?: {
content: {
"application/json": {
"multipart/form-data": {
/** Format: binary */
file: string;
file: File;
};
};
};
@ -7644,7 +7648,7 @@ export interface operations {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
file: File;
};
};
};
@ -8518,7 +8522,7 @@ export interface operations {
content: {
"multipart/form-data": {
/** Format: binary */
file: string;
file: File;
};
};
};