22import chalk from 'chalk'
33import fs from 'fs'
44import path from 'path'
5- import { generateApi } from 'swagger-typescript-api'
6- import prettierConfig from '@sparing-software/prettier-config'
7-
8- import { default as optimizeTypesUtil } from './optimizeTypes'
9-
10- export type Config = {
11- /**
12- * Http address of JSON OpenAPI schema to your API
13- * @required
14- */
15- url ?: string
16- /**
17- * Output directory for generated http service
18- *
19- * In order to help webpack automatically map aliases for generated file in Vue/React projects please use the following config: './src/service'
20- * @default './service'
21- */
22- outDir ?: string
23- /**
24- * Output filename (filename must be with .ts extension)
25- * @default '__generated-api.ts'
26- */
27- outFilename ?: string
28- /**
29- * List of paths to be excluded from generated api
30- * @example ['/users'] // all paths starting with /users
31- * @example [''] // all paths
32- */
33- exclude ?: string [ ]
34- /**
35- * List of paths to be included in the generated api, takes priority over excluded paths
36- *
37- * Helpful when you need to exclude all but a few paths
38- * @example ['/users'] // all paths starting with /users
39- */
40- include ?: string [ ]
41- /**
42- * Removes all type exports that have no references in endpoints (can slow down api generation)
43- * @default true
44- */
45- optimizeTypes ?: boolean
46- /**
47- * List of types unaffected by optimizeTypes
48- *
49- * Helpful when you need an exported type that isn't referenced in any endpoint
50- * @example ['WidgetResourcetypeEnum']
51- */
52- typeWhitelist ?: string [ ]
53- }
5+ import { type Config , generateFromConfig } from './generateFromConfig'
546
557async function main ( ) {
568 const CONFIG_PATH = path . resolve ( process . cwd ( ) , 'sparing-open-api.config.js' )
@@ -64,17 +16,9 @@ async function main() {
6416 return
6517 }
6618
67- const {
68- url,
69- outDir = './service/' ,
70- outFilename = '__generated-api.ts' ,
71- exclude = [ ] ,
72- include = [ ] ,
73- optimizeTypes = true ,
74- typeWhitelist = [ ]
75- } = ( await import ( CONFIG_PATH ) ) . default as Config
19+ const config = ( await import ( CONFIG_PATH ) ) . default as Config
7620
77- if ( ! url ) {
21+ if ( ! config . url ) {
7822 console . log (
7923 chalk . yellow (
8024 '"url" property in sparing-open-api.config.js is not defined. Service creation aborted!'
@@ -83,83 +27,8 @@ async function main() {
8327 return
8428 }
8529
86- const OUTPUT_PATH = path . resolve ( process . cwd ( ) , outDir )
87- const TEMPLATES_PATH = path . resolve ( __dirname , '../templates/' )
88-
89- generateApi ( {
90- name : outFilename ,
91- url,
92- httpClientType : 'axios' ,
93- templates : TEMPLATES_PATH ,
94- prettier : {
95- parser : 'typescript' ,
96- ...prettierConfig
97- } ,
98- generateUnionEnums : true ,
99- unwrapResponseData : true ,
100- // @ts -ignore
101- exclude,
102- // @ts -ignore
103- include,
104- hooks : {
105- onCreateRoute : data => {
106- // TODO Simplify types
107- const routeData = data as typeof data & {
108- responseBodySchema : { type : string }
109- routeParams : { query : object [ ] }
110- request: { query: { type: string ; name: string ; optional: boolean } }
111- }
112-
113- if ( routeData . request . method !== 'get' ) return routeData
114-
115- const type = `FetchKeys<${ routeData . responseBodySchema . type } >`
116-
117- routeData . routeParams . query . push ( {
118- name : 'fetchKeys' ,
119- required : false ,
120- in : 'query' ,
121- description : 'Keys to fetch from endpoint' ,
122- schema : { type } ,
123- type
124- } )
125-
126- if ( routeData . request . query ) {
127- const requestQuery = routeData . request . query
128- routeData . request . query = {
129- ...requestQuery ,
130- type : requestQuery . type . slice ( 0 , - 1 ) + 'fetchKeys?: T }'
131- }
132- } else {
133- routeData . request . query = {
134- name : 'query' ,
135- optional : true ,
136- type : `{ fetchKeys?: T }`
137- }
138- }
139-
140- return routeData
141- }
142- }
143- } ) . then ( async ( { files } ) => {
144- if ( ! fs . existsSync ( OUTPUT_PATH ) )
145- fs . mkdirSync ( OUTPUT_PATH , { recursive : true } )
146-
147- const [ firstFile ] = files
148-
149- if ( firstFile ) {
150- const { name, content } = firstFile
151- const fullPath = `${ OUTPUT_PATH } /${ name } `
152-
153- fs . writeFileSync ( fullPath , content )
154-
155- if ( optimizeTypes ) {
156- console . log ( '🤏 optimizing types' )
157- await optimizeTypesUtil ( fullPath , typeWhitelist )
158- }
159- }
160-
161- process . exit ( 0 )
162- } )
30+ await generateFromConfig ( config )
31+ process . exit ( 0 )
16332}
16433
16534main ( )
0 commit comments