Skip to content

Commit d3d8055

Browse files
psychedelicioushipsterusername
authored andcommitted
feat(ui): update typegen script
1 parent 476b0a0 commit d3d8055

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

invokeai/frontend/web/scripts/typegen.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
/* eslint-disable no-console */
22
import fs from 'node:fs';
33

4-
import openapiTS from 'openapi-typescript';
4+
import openapiTS, { astToString } from 'openapi-typescript';
5+
import ts from 'typescript';
56

67
const OPENAPI_URL = 'http://127.0.0.1:9090/openapi.json';
78
const OUTPUT_FILE = 'src/services/api/schema.ts';
89

910
async function generateTypes(schema) {
1011
process.stdout.write(`Generating types ${OUTPUT_FILE}...`);
12+
13+
// Use https://ts-ast-viewer.com to figure out how to create these AST nodes - define a type and use the bottom-left pane's output
14+
// `Blob` type
15+
const BLOB = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Blob'));
16+
// `null` type
17+
const NULL = ts.factory.createLiteralTypeNode(ts.factory.createNull());
18+
// `Record<string, unknown>` type
19+
const RECORD_STRING_UNKNOWN = ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('Record'), [
20+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
21+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.UnknownKeyword),
22+
]);
23+
1124
const types = await openapiTS(schema, {
1225
exportType: true,
1326
transform: (schemaObject) => {
1427
if ('format' in schemaObject && schemaObject.format === 'binary') {
15-
return schemaObject.nullable ? 'Blob | null' : 'Blob';
28+
return schemaObject.nullable ? ts.factory.createUnionTypeNode([BLOB, NULL]) : BLOB;
1629
}
1730
if (schemaObject.title === 'MetadataField') {
1831
// This is `Record<string, never>` by default, but it actually accepts any a dict of any valid JSON value.
19-
return 'Record<string, unknown>';
32+
return RECORD_STRING_UNKNOWN;
2033
}
2134
},
35+
defaultNonNullable: false,
2236
});
23-
fs.writeFileSync(OUTPUT_FILE, types);
37+
fs.writeFileSync(OUTPUT_FILE, astToString(types));
2438
process.stdout.write(`\nOK!\r\n`);
2539
}
2640

0 commit comments

Comments
 (0)