|
1 | 1 | import fs from 'node:fs'; |
2 | 2 | import openapiTS from 'openapi-typescript'; |
3 | | -import { COLORS } from './colors.js'; |
4 | 3 |
|
5 | 4 | const OPENAPI_URL = 'http://127.0.0.1:9090/openapi.json'; |
6 | 5 | const OUTPUT_FILE = 'src/services/api/schema.d.ts'; |
7 | 6 |
|
8 | 7 | async function main() { |
9 | 8 | process.stdout.write( |
10 | | - `Generating types "${OPENAPI_URL}" --> "${OUTPUT_FILE}"...\n\n` |
| 9 | + `Generating types "${OPENAPI_URL}" --> "${OUTPUT_FILE}"...` |
11 | 10 | ); |
12 | 11 | const types = await openapiTS(OPENAPI_URL, { |
13 | 12 | exportType: true, |
14 | | - transform: (schemaObject, metadata) => { |
| 13 | + transform: (schemaObject) => { |
15 | 14 | if ('format' in schemaObject && schemaObject.format === 'binary') { |
16 | 15 | return schemaObject.nullable ? 'Blob | null' : 'Blob'; |
17 | 16 | } |
18 | | - |
19 | | - /** |
20 | | - * Because invocations may have required fields that accept connection input, the generated |
21 | | - * types may be incorrect. |
22 | | - * |
23 | | - * For example, the ImageResizeInvocation has a required `image` field, but because it accepts |
24 | | - * connection input, it should be optional on instantiation of the field. |
25 | | - * |
26 | | - * To handle this, the schema exposes an `input` property that can be used to determine if the |
27 | | - * field accepts connection input. If it does, we can make the field optional. |
28 | | - */ |
29 | | - |
30 | | - if ('class' in schemaObject && schemaObject.class === 'invocation') { |
31 | | - // We only want to make fields optional if they are required |
32 | | - if (!Array.isArray(schemaObject?.required)) { |
33 | | - schemaObject.required = []; |
34 | | - } |
35 | | - |
36 | | - schemaObject.required.forEach((prop) => { |
37 | | - const acceptsConnection = ['any', 'connection'].includes( |
38 | | - schemaObject.properties?.[prop]?.['input'] |
39 | | - ); |
40 | | - |
41 | | - if (acceptsConnection) { |
42 | | - // remove this prop from the required array |
43 | | - const invocationName = metadata.path.split('/').pop(); |
44 | | - console.log( |
45 | | - `Making connectable field optional: ${COLORS.fg.green}${invocationName}.${COLORS.fg.cyan}${prop}${COLORS.reset}` |
46 | | - ); |
47 | | - schemaObject.required = schemaObject.required.filter( |
48 | | - (r) => r !== prop |
49 | | - ); |
50 | | - } |
51 | | - }); |
52 | | - return; |
53 | | - } |
54 | | - |
55 | | - // Check if we are generating types for an invocation output |
56 | | - if ('class' in schemaObject && schemaObject.class === 'output') { |
57 | | - // modify output types |
58 | | - } |
59 | 17 | }, |
60 | 18 | }); |
61 | 19 | fs.writeFileSync(OUTPUT_FILE, types); |
|
0 commit comments