File tree Expand file tree Collapse file tree 3 files changed +40
-16
lines changed Expand file tree Collapse file tree 3 files changed +40
-16
lines changed Original file line number Diff line number Diff line change 12
12
"lint" : " eslint \" lib/*.ts\" " ,
13
13
"lint:fix" : " eslint \" lib/*.ts\" --fix" ,
14
14
"format" : " prettier --write \" lib/*.ts\" \" examples/**/*.ts\" " ,
15
- "schema" : " ts-node scripts/schema .ts"
15
+ "schema" : " ts-node scripts/parseSchema .ts"
16
16
},
17
17
"repository" : {
18
18
"type" : " git" ,
45
45
"form-data" : " ^4.0.0" ,
46
46
"ws" : " ^8.17.1"
47
47
}
48
- }
48
+ }
Original file line number Diff line number Diff line change
1
+ import { promises as fs } from 'fs' ;
2
+ import openapiTS , { astToString } from 'openapi-typescript' ;
3
+ import * as ts from 'typescript' ;
4
+ import { pathToFileURL } from 'url' ;
5
+
6
+ const STRING = ts . factory . createKeywordTypeNode ( ts . SyntaxKind . StringKeyword ) ;
7
+ const ANY = ts . factory . createKeywordTypeNode ( ts . SyntaxKind . AnyKeyword ) ;
8
+
9
+ async function run ( ) {
10
+ const localPath = pathToFileURL ( '../firefly/doc-site/docs/swagger/swagger.yaml' ) ;
11
+ console . log ( `Generating types from ${ localPath } ` ) ;
12
+ const output = await openapiTS ( localPath . toString ( ) , {
13
+ transform : ( schemaObject , options ) => {
14
+ if ( schemaObject . type === 'object' && schemaObject . additionalProperties ) {
15
+ // For objects with arbitrary properties, use "any" instead of "unknown"
16
+ return ANY ;
17
+ }
18
+
19
+ if (
20
+ schemaObject . type === 'string' &&
21
+ schemaObject . format === 'uuid' &&
22
+ schemaObject . nullable &&
23
+ options . path &&
24
+ options . path . indexOf ( '/responses/' ) >= 0 &&
25
+ options . path . endsWith ( '/id' )
26
+ ) {
27
+ // For IDs in a response body, override to be non-nullable
28
+ schemaObject . nullable = false ;
29
+ return STRING ;
30
+ }
31
+ } ,
32
+ } ) ;
33
+ await fs . writeFile ( 'lib/schema.ts' , astToString ( output ) ) ;
34
+ }
35
+
36
+ run ( ) . catch ( ( err ) => {
37
+ console . error ( 'Error running script' , err ) ;
38
+ } ) ;
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments