@@ -8,10 +8,28 @@ import * as TJS from 'typescript-json-schema';
8
8
import type { PartialConfig } from './getConfig' ;
9
9
import { getConfig } from './getConfig' ;
10
10
11
- export const toOpenAPI = ( params : {
12
- input : string ;
13
- template ?: OpenAPIV3_1 . Document | string ;
14
- } ) : string => {
11
+ export const generate = ( configs ?: PartialConfig ) =>
12
+ getConfig ( configs ) . forEach ( ( config ) => {
13
+ const existingDoc : OpenAPIV3_1 . Document | undefined = existsSync ( config . output )
14
+ ? JSON . parse ( readFileSync ( config . output , 'utf8' ) )
15
+ : undefined ;
16
+ const template : OpenAPIV3_1 . Document = {
17
+ openapi : '3.1.0' ,
18
+ info : {
19
+ title : `${ config . output . split ( '/' ) . at ( - 1 ) ?. replace ( '.json' , '' ) } api` ,
20
+ version : 'v0.0' ,
21
+ } ,
22
+ servers : config . baseURL ? [ { url : config . baseURL } ] : undefined ,
23
+ ...existingDoc ,
24
+ paths : { } ,
25
+ components : { } ,
26
+ } ;
27
+
28
+ writeFileSync ( config . output , toOpenAPI ( { input : config . input , template } ) , 'utf8' ) ;
29
+ console . log ( `${ config . output } was built successfully.` ) ;
30
+ } ) ;
31
+
32
+ const toOpenAPI = ( params : { input : string ; template ?: OpenAPIV3_1 . Document | string } ) : string => {
15
33
const tree = getDirentTree ( params . input ) ;
16
34
17
35
const createFilePaths = ( tree : DirentTree ) : string [ ] => {
@@ -133,24 +151,3 @@ type AllMethods = [${paths.map((_, i) => `Methods${i}`).join(', ')}]`;
133
151
134
152
return JSON . stringify ( doc , null , 2 ) . replaceAll ( '#/definitions' , '#/components/schemas' ) ;
135
153
} ;
136
-
137
- export default ( configs ?: PartialConfig ) =>
138
- getConfig ( configs ) . forEach ( ( config ) => {
139
- const existingDoc : OpenAPIV3_1 . Document | undefined = existsSync ( config . output )
140
- ? JSON . parse ( readFileSync ( config . output , 'utf8' ) )
141
- : undefined ;
142
- const template : OpenAPIV3_1 . Document = {
143
- openapi : '3.1.0' ,
144
- info : {
145
- title : `${ config . output . split ( '/' ) . at ( - 1 ) ?. replace ( '.json' , '' ) } api` ,
146
- version : 'v0.0' ,
147
- } ,
148
- servers : config . baseURL ? [ { url : config . baseURL } ] : undefined ,
149
- ...existingDoc ,
150
- paths : { } ,
151
- components : { } ,
152
- } ;
153
-
154
- writeFileSync ( config . output , toOpenAPI ( { input : config . input , template } ) , 'utf8' ) ;
155
- console . log ( `${ config . output } was built successfully.` ) ;
156
- } ) ;
0 commit comments