1
1
import generate from '@babel/generator' ;
2
2
import * as t from '@babel/types' ;
3
- import { SchemaTSOptions } from 'schema-typescript' ;
4
3
import { generateTypeScriptTypes } from 'schema-typescript' ;
5
4
import { getTypeNameSafe , shouldInclude , toCamelCase , toPascalCase } from 'schema-typescript' ;
6
5
7
6
import { OpenAPIPathItem , OpenAPISpec , Operation , Parameter , Response } from './openapi.types' ;
7
+ import { OpenAPIOptions } from './types' ;
8
8
import { createPathTemplateLiteral } from './utils' ;
9
9
10
- export interface OpenAPIOptions extends SchemaTSOptions {
11
- version ?: 'v1' | 'v1beta1' | 'v2beta1' | 'v2beta2' ;
12
- mergedParams ?: boolean ;
13
- paths ?: {
14
- // Include/Exclude types
15
- include ?: string [ ] ;
16
- exclude ?: string [ ] ;
17
-
18
- includeTags ?: string [ ] ;
19
- excludeTags ?: string [ ] ;
20
-
21
- includeRequests ?: string [ ] ;
22
- excludeRequests ?: string [ ] ;
23
- }
24
- }
25
-
26
10
/**
27
11
includes: {
28
12
requests: ['patch', 'head', 'options', 'get', 'delete'],
@@ -86,11 +70,11 @@ const shouldIncludeOperation = (
86
70
const shouldIncludeByRequest = shouldInclude ( method , {
87
71
include : options . paths ?. includeRequests ?? [ ] ,
88
72
exclude : options . paths ?. excludeRequests ?? [ ]
89
- } )
73
+ } ) ;
90
74
91
75
if ( ! shouldIncludeByRequest ) return false ;
92
76
return true ;
93
- }
77
+ } ;
94
78
95
79
export const getApiTypeNameSafe = ( options : OpenAPIOptions , str : string ) : string => {
96
80
return getTypeNameSafe ( options . namingStrategy , str ) ;
@@ -135,14 +119,14 @@ export const getResponseType = (options: OpenAPIOptions, prop: Response) => {
135
119
// resolve $ref
136
120
if ( prop . schema ) {
137
121
if ( ! prop . schema . $ref ) {
138
- throw new Error ( 'no property set on open api parameter schema!' )
122
+ throw new Error ( 'no property set on open api parameter schema!' ) ;
139
123
}
140
124
const ref = prop . schema . $ref . split ( '/' ) ;
141
125
const definitionName = ref . pop ( ) ;
142
126
return t . tsTypeReference ( t . identifier ( getApiTypeNameSafe ( options , definitionName ) ) ) ;
143
127
}
144
128
return t . tsAnyKeyword ( ) ;
145
- }
129
+ } ;
146
130
147
131
export const getParameterType = ( options : OpenAPIOptions , prop : Parameter ) => {
148
132
if ( prop . type ) {
@@ -168,14 +152,14 @@ export const getParameterType = (options: OpenAPIOptions, prop: Parameter) => {
168
152
// resolve $ref
169
153
if ( prop . schema ) {
170
154
if ( ! prop . schema . $ref ) {
171
- throw new Error ( 'no property set on open api parameter schema!' )
155
+ throw new Error ( 'no property set on open api parameter schema!' ) ;
172
156
}
173
157
const ref = prop . schema . $ref . split ( '/' ) ;
174
158
const definitionName = ref . pop ( ) ;
175
159
return t . tsTypeReference ( t . identifier ( getApiTypeNameSafe ( options , definitionName ) ) ) ;
176
160
}
177
161
return t . tsAnyKeyword ( ) ;
178
- }
162
+ } ;
179
163
180
164
interface ParameterInterfaces {
181
165
query : Parameter [ ] ;
@@ -203,8 +187,8 @@ const initParams = (): ParameterInterfaces => {
203
187
path : [ ] ,
204
188
formData : [ ] ,
205
189
body : [ ]
206
- }
207
- }
190
+ } ;
191
+ } ;
208
192
209
193
210
194
export function generateOpenApiParams ( options : OpenAPIOptions , path : string , pathItem : OpenAPIPathItem ) : t . TSInterfaceDeclaration [ ] {
@@ -253,7 +237,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
253
237
p . optional = true ;
254
238
}
255
239
inner . push ( p ) ;
256
- } )
240
+ } ) ;
257
241
258
242
if ( ! options . mergedParams ) {
259
243
if ( paramType === 'body' ) {
@@ -272,7 +256,7 @@ export function generateOpenApiParams(options: OpenAPIOptions, path: string, pat
272
256
if ( inner . length ) {
273
257
props . push (
274
258
p
275
- )
259
+ ) ;
276
260
}
277
261
}
278
262
} else {
@@ -312,15 +296,15 @@ export function getOpenApiParams(options: OpenAPIOptions, path: string, pathItem
312
296
pathItem . parameters = pathItem . parameters ?? [ ] ;
313
297
const pathParms = pathItem . parameters ?. filter ( param => param . in === 'path' ) ?? [ ] ;
314
298
if ( pathParms . length !== pathInfo . params . length ) {
315
- const parameters = pathItem . parameters ?. filter ( param => param . in !== 'path' ) ?? [ ]
299
+ const parameters = pathItem . parameters ?. filter ( param => param . in !== 'path' ) ?? [ ] ;
316
300
pathInfo . params . forEach ( name => {
317
301
const found = pathParms . find ( param => param . name === name ) ;
318
302
parameters . push ( found ? found : {
319
303
name,
320
304
type : 'string' ,
321
305
required : true ,
322
306
in : 'path'
323
- } )
307
+ } ) ;
324
308
} ) ;
325
309
pathItem . parameters = parameters ;
326
310
}
@@ -358,7 +342,7 @@ export function getOpenApiParams(options: OpenAPIOptions, path: string, pathItem
358
342
if ( operation . parameters ) {
359
343
// Categorize parameters by 'in' field
360
344
operation . parameters . forEach ( param => {
361
- opParamMethod [ param . in ] . push ( param )
345
+ opParamMethod [ param . in ] . push ( param ) ;
362
346
} ) ;
363
347
}
364
348
@@ -373,13 +357,13 @@ export function generateOpenApiTypes(options: OpenAPIOptions, schema: OpenAPISpe
373
357
Object . entries ( schema . paths ) . forEach ( ( [ path , pathItem ] ) => {
374
358
interfaces . push ( ...generateOpenApiParams ( options , path , pathItem ) ) ;
375
359
} ) ;
376
- return interfaces . map ( i => t . exportNamedDeclaration ( i ) )
360
+ return interfaces . map ( i => t . exportNamedDeclaration ( i ) ) ;
377
361
}
378
362
379
363
const getOperationMethodName = ( operation : Operation , method : string , path : string ) => {
380
364
const methodName = operation . operationId || toCamelCase ( method + path . replace ( / \W / g, '_' ) ) ;
381
365
return methodName ;
382
- }
366
+ } ;
383
367
384
368
export function generateMethods ( options : OpenAPIOptions , schema : OpenAPISpec ) : t . ClassMethod [ ] {
385
369
const methods : t . ClassMethod [ ] = [ ] ;
@@ -398,7 +382,7 @@ export function generateMethods(options: OpenAPIOptions, schema: OpenAPISpec): t
398
382
399
383
const typeName = toPascalCase ( getOperationMethodName ( operation , method , path ) ) + 'Request' ;
400
384
const id = t . identifier ( 'params' ) ;
401
- id . typeAnnotation = t . tsTypeAnnotation ( t . tsTypeReference ( t . identifier ( typeName ) ) )
385
+ id . typeAnnotation = t . tsTypeAnnotation ( t . tsTypeReference ( t . identifier ( typeName ) ) ) ;
402
386
const params = [ id ] ;
403
387
404
388
const returnType = getOperationReturnType ( options , operation , method ) ;
@@ -491,7 +475,7 @@ export function generateOpenApiClient(options: OpenAPIOptions, schema: OpenAPISp
491
475
true
492
476
) ,
493
477
...generateMethods ( options , schema )
494
- ]
478
+ ] ;
495
479
496
480
const classBody = t . classBody ( [
497
481
t . classMethod (
0 commit comments